Jump to content

Project Euler problem number 8, program only returning 0.

Go to solution Solved by madknight3,

Thanks a lot.  I'm sorry but how would the last part, "int intNum = element - '0';" work in my specific context?  I'm sorry if this is  a dumb question, I'm new at this.

 

It might help if you tried printing out stuff to see how it looks. So lets do an example with these two variables. 

char two = '2';char zero = '0';

To print out the characters, you would do the following

printf("%c", two); // prints 2 printf("%c", zero); //prints 0

When working with ASCII characters, see the following table, each character has associated values. The "Dec" column is the value in our number system. So

A = 65, B = 66, C = 67, etc

0 = 48, 1 = 49, 2 = 50, etc

a = 97, b = 98, c = 99, etc

 

So getting back to the example, we can see these characters ASCII values by printing them like so

printf("%d", two); // prints 50printf("%d", zero); // prints 48

When we combine characters with operators like addition and subtraction, C will use their ASCII values to perform the computation. Since the characters 0-9 are in order, their ASCII values can be used to convert the character of a number into the actual value of that number by subtracting the character 0 from it.

'2' - '0' = 2// because50 - 48 = 2'9' - '0' = 9//because57 - 48 = 9

Knowing this, you should be able to change your program to be correct.

I'm doing problem #8 in project euler in C and I keep getting 0 for my answer.  Any help would be appreciated.  Thanks!

#include<stdio.h>#include <stdlib.h>#include <errno.h>int main(void){        char num[] = "7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450";        long long product = 0;        long long temp = 0;        for (int x = 0; x < 987; x++)        {                       temp = 0;                for (int j = x; j < x + 13; j++)                {                        char element = num[j];                        char* point = &element;                        int intNum = atoi(point);                        temp *= intNum;                //      printf("%i ", intNum);                }                if (temp > product){                        product = temp; }        }          printf("%lli \n", product);}  
Link to post
Share on other sites

All good mate. Site doesn't really give you a straightforward way to get to the questions unless you've registered. Anywho, I haven't worked with C# or C++, just wanted to give you a tip to get better / any answers. Sorry if I came off rude!

Link to post
Share on other sites

All good mate. Site doesn't really give you a straightforward way to get to the questions unless you've registered. Anywho, I haven't worked with C# or C++, just wanted to give you a tip to get better / any answers. Sorry if I came off rude!

It's cool!

Link to post
Share on other sites

Edit:  I don't get 0, but I don't get the right answer either :(

You're only checking horizontally, you also need to check vertically and possibly diagonally and maybe even in random snake like shapes. 

1474412270.2748842

Link to post
Share on other sites

You're only checking horizontally, you also need to check vertically and possibly diagonally and maybe even in random snake like shapes. 

Oh ok.  This question is worded much worse than the others.  So are you saying that the final answer could be in any shape?  Like 1/2 of them are horizontal and a 1/4 vertical and 1/4 diagonal?  That would make this way harder.

Link to post
Share on other sites

Oh ok.  This question is worded much worse than the others.  So are you saying that the final answer could be in any shape?  Like 1/2 of them are horizontal and a 1/4 vertical and 1/4 diagonal?  That would make this way harder.

I don't know I haven't done the problem, but those would be considered adjacent. It's worded pretty badly.

1474412270.2748842

Link to post
Share on other sites

If memory serves, this one only requires checking horizontally. It's a 1000 digit number, not a two dimensional array. There's another similar problem (#11) which does use the two dimensional array and requires more than just horizontal checking, but this one is the simpler version.

 

If you're not getting the answer, it's probably just because your code is incorrect. 

Link to post
Share on other sites

                        char element = num[j];                        char* point = &element;                        int intNum = atoi(point);

this bit of code is probably not giving you the expected result: atoi doesn't take a char as an argument, but it takes a string (a char*). when you pass it a char* like that you have no way to tell the function that you only want to 'translate' one digit.

so this happens:

char *myString = "12345";char element = myString[1]; // this is '2';char* point = &element; // this is equivalent to char* point = "2345"int intNum = atoi(point); // 2345

the easy way to convert a char to a number is this:

int intNum = element - '0';

this works because character literals are just numeric constants (coded in ASCII), and all the numeric constants for 0-9 have consecutive codes

 

edit: you only need to check horizontally, because it's a 1000 digit number, so there is no such thing as "the digit below this digit"

Link to post
Share on other sites

this bit of code is probably not giving you the expected result: atoi doesn't take a char as an argument, but it takes a string (a char*). when you pass it a char* like that you have no way to tell the function that you only want to 'translate' one digit.

so this happens:

char *myString = "12345";char element = myString[1]; // this is '2';char* point = &element; // this is equivalent to char* point = "2345"int intNum = atoi(point); // 2345

the easy way to convert a char to a number is this:

int intNum = element - '0';

this works because character literals are just numeric constants (coded in ASCII), and all the numeric constants for 0-9 have consecutive codes

 

edit: you only need to check horizontally, because it's a 1000 digit number, so there is no such thing as "the digit below this digit"

Thanks a lot.  I'm sorry but how would the last part, "int intNum = element - '0';" work in my specific context?  I'm sorry if this is  a dumb question, I'm new at this.

Link to post
Share on other sites

Thanks a lot.  I'm sorry but how would the last part, "int intNum = element - '0';" work in my specific context?  I'm sorry if this is  a dumb question, I'm new at this.

 

It might help if you tried printing out stuff to see how it looks. So lets do an example with these two variables. 

char two = '2';char zero = '0';

To print out the characters, you would do the following

printf("%c", two); // prints 2 printf("%c", zero); //prints 0

When working with ASCII characters, see the following table, each character has associated values. The "Dec" column is the value in our number system. So

A = 65, B = 66, C = 67, etc

0 = 48, 1 = 49, 2 = 50, etc

a = 97, b = 98, c = 99, etc

 

So getting back to the example, we can see these characters ASCII values by printing them like so

printf("%d", two); // prints 50printf("%d", zero); // prints 48

When we combine characters with operators like addition and subtraction, C will use their ASCII values to perform the computation. Since the characters 0-9 are in order, their ASCII values can be used to convert the character of a number into the actual value of that number by subtracting the character 0 from it.

'2' - '0' = 2// because50 - 48 = 2'9' - '0' = 9//because57 - 48 = 9

Knowing this, you should be able to change your program to be correct.

Link to post
Share on other sites

It might help if you tried printing out stuff to see how it looks. So lets do an example with these two variables. 

char two = '2';char zero = '0';

To print out the characters, you would do the following

printf("%c", two); // prints 2 printf("%c", zero); //prints 0

When working with ASCII characters, see the following table, each character has associated values. The "Dec" column is the value in our number system. So

A = 65, B = 66, C = 67, etc

0 = 48, 1 = 49, 2 = 50, etc

a = 97, b = 98, c = 99, etc

 

So getting back to the example, we can see these characters ASCII values by printing them like so

printf("%d", two); // prints 50printf("%d", zero); // prints 48

When we combine characters with operators like addition and subtraction, C will use their ASCII values to perform the computation. Since the characters 0-9 are in order, their ASCII values can be used to convert the character of a number into the actual value of that number by subtracting the character 0 from it.

'2' - '0' = 2// because50 - 48 = 2'9' - '0' = 9//because57 - 48 = 9

Knowing this, you should be able to change your program to be correct.

Oh ok I get it now.  For some reason I was reading the minus sign as a dash, leading to my confusion.  Thanks.

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×