spadewarrior
July 13th, 2008, 05:49 PM
Anyone had a go at this one?
http://projecteuler.net/index.php?section=problems&id=164
I started a python program for it but it's so slow I decided to rewrite it in C. Unfortunately I can't work out how to use 20-digit numbers in C - nothing seems to be big enough to hold them.
Any ideas on how to get my amateurish code running (a lot) faster?
Python:
def calc(string):
count = 0
temp = 0
j = 0
while j <= len(string) - 3 and temp == 0:
h = j + 1
k = j + 2
if (int(string[j]) + int(string[h]) + int(string[k])) > 9:
temp +=1
j += 1
if temp == 0:
count += 1
print string, "OK!"
return count
def makeList(n, MAX):
count = 0
while n <= MAX:
if calc(str(n)) == 1:
count += 1
n += 1
return count
n = 10000000000000000000
MAX = 99999999999999999999
answer = makeList(n, MAX)
print "The answer is", answer
C:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
long calc(char * p);
unsigned long long int makeList(long n, long MAX);
int main(void)
{
unsigned long long int n = 1000000000ULL;
unsigned long long int MAX = 9999999999ULL;
long answer = makeList(n, MAX);
printf ("\n%ld\n", answer);
return 0;
}
unsigned long long int makeList(long n, long MAX)
{
unsigned long long int count = 0ULL;
char numStr[100];
char *p = numStr;
while (n < MAX)
{
sprintf( numStr, "%ld", n);
p = numStr;
if( calc( p ) == 1)
{
count ++;
}
n++;
}
return count;
}
long calc(char *p)
{
unsigned long long int count = 0ULL;
int temp = 0;
int j = 0;
int len = strlen(p);
int a, b, c;
while ( j <= (len - 3) && temp == 0)
{
sscanf(&p[j], "%1d%1d%1d", &a, &b, &c);
if ( a + b+ c > 9 )
{
temp++;
}
j++; // move through the string
}
if ( temp == 0 )
{
count++;
printf( "\n%s OK!", p);
}
return count;
}
If I try to use 20-digit numbers in the C program I get these warnings:
euler164.c:11: warning: this decimal constant is unsigned only in ISO C90
euler164.c:11: warning: integer constant is too large for ‘long’ type
euler164.c:12:33: warning: integer constant is too large for its type
euler164.c:12: warning: integer constant is too large for ‘long’ type
Thanks for reading.
http://projecteuler.net/index.php?section=problems&id=164
I started a python program for it but it's so slow I decided to rewrite it in C. Unfortunately I can't work out how to use 20-digit numbers in C - nothing seems to be big enough to hold them.
Any ideas on how to get my amateurish code running (a lot) faster?
Python:
def calc(string):
count = 0
temp = 0
j = 0
while j <= len(string) - 3 and temp == 0:
h = j + 1
k = j + 2
if (int(string[j]) + int(string[h]) + int(string[k])) > 9:
temp +=1
j += 1
if temp == 0:
count += 1
print string, "OK!"
return count
def makeList(n, MAX):
count = 0
while n <= MAX:
if calc(str(n)) == 1:
count += 1
n += 1
return count
n = 10000000000000000000
MAX = 99999999999999999999
answer = makeList(n, MAX)
print "The answer is", answer
C:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
long calc(char * p);
unsigned long long int makeList(long n, long MAX);
int main(void)
{
unsigned long long int n = 1000000000ULL;
unsigned long long int MAX = 9999999999ULL;
long answer = makeList(n, MAX);
printf ("\n%ld\n", answer);
return 0;
}
unsigned long long int makeList(long n, long MAX)
{
unsigned long long int count = 0ULL;
char numStr[100];
char *p = numStr;
while (n < MAX)
{
sprintf( numStr, "%ld", n);
p = numStr;
if( calc( p ) == 1)
{
count ++;
}
n++;
}
return count;
}
long calc(char *p)
{
unsigned long long int count = 0ULL;
int temp = 0;
int j = 0;
int len = strlen(p);
int a, b, c;
while ( j <= (len - 3) && temp == 0)
{
sscanf(&p[j], "%1d%1d%1d", &a, &b, &c);
if ( a + b+ c > 9 )
{
temp++;
}
j++; // move through the string
}
if ( temp == 0 )
{
count++;
printf( "\n%s OK!", p);
}
return count;
}
If I try to use 20-digit numbers in the C program I get these warnings:
euler164.c:11: warning: this decimal constant is unsigned only in ISO C90
euler164.c:11: warning: integer constant is too large for ‘long’ type
euler164.c:12:33: warning: integer constant is too large for its type
euler164.c:12: warning: integer constant is too large for ‘long’ type
Thanks for reading.