Project Euler in C
All Files Functions Macros Pages
Macros | Functions
0004.c File Reference

Largest palindromic number that is the product of two 3-digit positive integers. More...

#include <stdio.h>
#include <string.h>

Go to the source code of this file.

Macros

#define LTOA_BUFFER_SIZE   32
 The size of the string buffer for the "ltoa" (long to ascii) conversion.
 

Functions

int is_palindromic (const long n)
 Determines if an integer is palindromic.
 
void reverse (char *str)
 Reverses a string in-place.
 
int main (void)
 

Detailed Description

Largest palindromic number that is the product of two 3-digit positive integers.

https://projecteuler.net/problem=4

Definition in file 0004.c.

Macro Definition Documentation

◆ LTOA_BUFFER_SIZE

#define LTOA_BUFFER_SIZE   32

The size of the string buffer for the "ltoa" (long to ascii) conversion.

The maximum number of digits in a positive long is D = floor(log10(LONG_MAX)) + 1. The buffer size must be >= D + 2, including the minus sign and a terminating '\0'.

FIXME: This should not be a fixed size.

Definition at line 18 of file 0004.c.

Function Documentation

◆ is_palindromic()

int is_palindromic ( const long  n)

Determines if an integer is palindromic.

Parameters
nAn integer >= 0.

Definition at line 52 of file 0004.c.

52 {
53 char str[LTOA_BUFFER_SIZE];
54 char reversed[LTOA_BUFFER_SIZE];
55
56 /* Converts the number to a string. */
57 sprintf(str, "%ld", n);
58 /*
59 * Or, in C99:
60 * const int length = snprintf(NULL, 0, "%ld", n);
61 * char *str = malloc(length + 1);
62 * sprintf(str, "%ld", n);
63 * ...
64 * free(str);
65 */
66
67 /* Duplicates the string and reverses it. */
68 strcpy(reversed, str);
69 reverse(reversed);
70
71 /* Returns whether the reverse is equal to the original. */
72 return strcmp(reversed, str) == 0;
73}
void reverse(char *)
Reverses a string in-place.
Definition 0004.c:78
#define LTOA_BUFFER_SIZE
The size of the string buffer for the "ltoa" (long to ascii) conversion.
Definition 0004.c:18

◆ main()

int main ( void  )

Definition at line 23 of file 0004.c.

23 {
24 long max_palindrome = -1;
25
26 /*
27 * Loops through 900^2 = 810000 numbers, the Cartesian square of 3-digit
28 * numbers. This is inefficient. Even in case a Cartesian product is really
29 * needed, something like `std::views::cartesian_product` in C++23 (or
30 * `itertools.product` in Python) is more efficient than a nested `for`.
31 */
32 int i;
33 int j;
34 for (i = 100; i <= 999; i++) {
35 for (j = 100; j <= 999; j++) {
36 const long product = i * j;
37 if (is_palindromic(product) && product > max_palindrome) {
38 max_palindrome = product;
39 }
40 }
41 }
42
43 printf("%ld\n", max_palindrome);
44
45 return 0;
46}
int is_palindromic(const long)
Determines if an integer is palindromic.
Definition 0004.c:52

◆ reverse()

void reverse ( char *  str)

Reverses a string in-place.

Definition at line 78 of file 0004.c.

78 {
79 size_t length;
80 size_t i;
81 size_t j;
82
83 /* If the string is empty, does nothing. */
84 if (!*str) {
85 return;
86 }
87
88 length = strlen(str);
89 for (i = 0, j = length - 1; i < j; i++, j--) {
90 const char tmp = str[i];
91 str[i] = str[j];
92 str[j] = tmp;
93 }
94}