Hello
My problem is that I have to solve a cryptarithm (assign a unique digit from 0 to 1 for each number) where:
O N E + O N E = T W O
S E V E N is prime
E I G H T is a perfect cube
and none of these numbers can start with a 0.
I have already designed the program but it goes into some sort of continuous loop and doesn't assign the proper values to O N E and TWO. I would be really grateful if someone can take a look and advise me in any way at all. Thanks a lot guys.
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <math.h>
/*
ONE+ONE = TWO
NINE = PERFECT SQUARE
SEVEN = PRIME NUMBER
* The least possible value for ONE is 120,
* The greatest possible value for ONE is 987 */
// N cannot be equal to 0 because NINE cannot start with a 0
#define MIN_ONE 120
#define MAX_ONE 987
/* The least possible value for NINE is 1023,
* and the square root of 1023 is 31.98.
* The greatest possible value for NINE is 9876,
* and the square root of 9876 is 99.37. */
#define MIN_NINE 32
#define MAX_NINE 99
bool is_prime(int);
int main(void) {
// this part is for ONE
int k;
for (k = MIN_ONE; k <= MAX_ONE; ++k) {
int one = k;
int bits_one[10] = {0};
int o = one / 100;
/* We memorise o. */
bits_one[o] = 1;
int n = one / 10 % 10;
/* seven has to be prime hence n cannot be even & nine does not start with 0, so n cannot be 0. */
if (n == 0 && n % 2 == 0)
continue;
/* bits_one[n] becomes equal to 1 if i has not been seen, and to 2 otherwise. */
if (++bits_one[n] == 2)
continue;
int e = one % 10;
/* bits_one[e] becomes equal to 1 if n has not been seen, and to 2 otherwise. */
if (++bits_one[e] == 2)
continue;
// this part is for NINE
int m;
for (m = MIN_NINE; m <= MAX_NINE; ++m) {
/* If the current attempt at setting NINE to a suitable value eventually fails
* then we need to remember the current value of ONE. */
int bits_one_nine[10];
int x;
for (x = 1; x < 10; ++x)
bits_one_nine[x] = bits_one[x];
int nine = m * m;
int n = nine / 1000;
/* bits_one_nine[n] becomes equal to 1 if n has not been seen, and to 2 otherwise. */
if (++bits_one_nine[n] == 2)
continue;
int i = nine / 100 % 10;
/* bits_one_nine[i] becomes equal to 1 if i has not been seen, and to 2 otherwise. */
if (++bits_one_nine[i] == 2)
continue;
n = nine / 10 % 10;
/* bits_one_nine[n] becomes equal to 1 if n has not been seen, and to 2 otherwise. */
if (++bits_one_nine[n] == 2)
continue;
int e = nine % 10;
/* bits_one_nine[n] becomes equal to 1 if n has not been seen, and to 2 otherwise. */
if (bits_one_nine[e])
continue;
int s = o;
int v = i;
int two = one + one;
int seven = 10000 * s + 1000 * e + 100 * v + 10 * e + n;
if (is_prime(seven))
printf("one = %d, two = %d nine = %d and seven = %d is a solution.\n", one, two, nine, seven);
getchar();
}
}
return EXIT_SUCCESS;
getchar();
}
bool is_prime(int n) {
/* Returns false if n is even.*/
if ((n % 2) == 0)
return false;
/* If n is not prime then it has a factor at most equal to its square root. */
int max = sqrt(n);
/* Returns false if n is divisible by an odd number. */
int k;
for (k = 3; k <= max; k += 2) {
if (n % k == 0)
return false;
}
return true;