Can you find the smallest number that fulfills the following criteria?
1. It can only contain digits of 3, 5 and 7
2. The sum of the digits can be divided by 3, 5 and 7 without remainder
3. The number can be divided by 3, 5 and 7 without remainder
product([], 1).
product([H|T], X) :- product(T, Y), X is H * Y.
divmod(N, N, Q, 0, A) :- succ(A, Q).
divmod(N, D, Q, N, Q) :- D > N.
divmod(N, D, Q, R, A) :- D < N, plus(T1, D, N), succ(A, A2), divmod(T1, D, Q, R, A2).
Brute force solution. The answer is indeed 33577577777777775.
#include <stdio.h>
/* this is a horrible coding practice. never do this */
static unsigned long long digit_sum = 0;
/* generates 357-digits-only numbers in ascending order */
unsigned long long next(unsigned long long n) {
unsigned long long p = 10;
while (1) {
switch (n / p % 10) {
case 3: digit_sum += 2; return (n + 2 * p);
case 5: digit_sum += 2; return (n + 2 * p);
case 7: digit_sum -= 4; n -= 4 * p;
}
p *= 10;
if (p > n) { digit_sum += 3; return (n + 3 * p); }
}
}
int main(void) {
/* start where the digit sum is first 105 */
/* this number was brute forced earlier */
unsigned long long x = 33377777777777775;
digit_sum = 105;
while (1) {
if (digit_sum == 105 && x % 105 == 0) {
printf("%llu %llu\n", x, digit_sum);
break;
}
x = next(x);
}
return 0;
}
int test_one(unsigned long long x);
int test_two(unsigned long long x);
int test_three(unsigned long long x);
int main() {
unsigned long long i = (unsigned long long) 33577577777777775.0;
do {
if(test_one(i)) {
i++;
continue;
}
if(test_two(i)) {
i++;
continue;
}
if(test_three(i)) {
i++;
continue;
}
break;
} while(1);
printf("%lld\n", i);
}
int test_one(unsigned long long x) {
do {
switch(x % 10) {
case 3:
case 5:
case 7:
break;
default:
return(1);
}
} while(x/=10 > 1);
return(0);
}
int test_two(unsigned long long x) {
int sum = 0;
do {
sum += x % 10;
} while(x/=10 > 1);
return((sum%3) + (sum%5) + (sum%7));
}
int test_three(unsigned long long x) {
return((x%3) + (x%5) + (x%7));
}
Name:
Anonymous2012-10-27 2:33
>>28
- why is the long a decimal?
- your 3 conditionals in the first do-while do the exact same goddamn thing
- the logic of test_one is all kinds of fucked up
Name:
Anonymous2012-10-27 2:41
>>29
Compiler wouldn't let me enter it without the decimal because it was too big. Had to cast it. Can you elaborate at all on the messed up logic?
Name:
Anonymous2012-10-27 4:02
>>30
Try unsigned long long int i = 33577577777777775ULL;
instead newfriend.
Also use "%llu" instead of "%lld" since the former is for unsigned and the latter is for signed numbers.