Thread: Long (really long) number

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    13

    Long (really long) number

    Indeed.
    So, as probably someone understood from my previous post, I'm trying to work my way out of the easiest problems listed in projecteuler.net, in an attempt to improve my coding skills and my knowledge of the C language. Anyway, I'm stuck again, after I completed a couple of other problems since your last help, and here's why.

    The point of the code that follows is to calculate a really big sum: one hundred 50-digits numbers have to be summed. I thought a bit about it and I came to this conclusion: I could copy-paste the values in a file, and from there I could loop as follows: read one string, convert it to a integer, sum and then proceed to the next value. Perhaps you already spotted the problem - I can't find a way to store the value, 'cause nothing I've found is big enough for a 50-digits number. The sad thing is that it dawned me all together when I've found this. If I understood it correctly, it says that the maximum value that could be stored is 18,446,744,073,709,551,615, an unsigned long long. Which is exactly what I'm using, and that's why when I convert from string to integer I receive unpleasant results.

    Code:
    /*Work out the first ten digits of the sum of the following one-hundred 50-digit numbers.
    
     *SEE FILE ATTACHED** */
    
    #include<stdio.h>
    #include<stdint.h>
    #include<stdlib.h>
    
    int main(int argc, char *argv[]) {
        int i;
        uintmax_t pivot=0, sum=0;
        char ascii_num[50], ascii_sum[sizeof(uintmax_t)*8]; //this last one is gonna be 64 
        FILE *fp;
    
        //File opening
        fp=fopen("problem13.txt", "r");
        if (fp==NULL) {
            printf("Error: could not open file problem13.txt.\nPress any key to exit.\n");
            getch();
            return (1);
        }
        
        while (fgets(ascii_num, 50, fp) != NULL) {
            pivot = atoll(ascii_num);
            sum +=pivot;
        }
        
        itoa(sum, ascii_sum, 10);
        
        for(i=0; i<10; i++) {
            printf("%d", ascii_sum[i]);
        }
        
        fclose(fp);
        
        printf("\nPress any key to continue.\n");
        getch();
        return (0);
    }
    I tried in several different ways to work around the conversion from string to integer - atoll is just the last one in chronological order.
    My main point is: is this algorithm flawed, or can this work out the way I want? I can see at least one other problem down the road (that atoi down there, for the second conversion), so I'm not really sure this is the best way to do it. Actually, I'm sure this is not it - should I rethink my solution? Any ideas?
    Last edited by Doriän; 09-05-2007 at 05:12 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. sorting using pointer to pointer not working
    By eager2no in forum C Programming
    Replies: 17
    Last Post: 09-21-2008, 12:52 AM
  3. Nim Trainer
    By guesst in forum Game Programming
    Replies: 3
    Last Post: 05-04-2008, 04:11 PM
  4. Quick, Partiotion, and Insertion Sort
    By silicon in forum C++ Programming
    Replies: 0
    Last Post: 05-18-2005, 08:47 PM
  5. can someone check this out and let me know ?
    By javaz in forum C Programming
    Replies: 5
    Last Post: 01-21-2002, 02:13 PM