Thread: Unsigned long

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    5

    Question Unsigned long

    I just wrote a program which calculate fibonacci numbers. My problem is that i cant get passed the unsigned long long limit (18,446,744,073,709,551,615). So my question is IS there ANY possible way to overcome this limit???
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <stdbool.h>
    
    
    
    
    int long long fibonacci(int i, bool t_f);
    
    
    int main(){
        int numeric_input = 0;
        bool save_to_file = 0;
        char character_input;
    
    
        while (1){
            printf("\ndo you want to save the results to a file?");
            printf("\ny for yes an n for no:_\b");
            scanf("%c", &character_input);
            if ((character_input == 'y') || (character_input == 'Y')){
                save_to_file = 1;
                break;
            }
            else if ((character_input == 'n') || (character_input == 'N')){
                break;
            }
            else if (character_input == '\n'){
            }
            else {
                printf("\nwrong input please try again!");
            }
        }
        printf ("\nenter how many times you want to run the fibonacci: ");
        scanf ("%d", &numeric_input);
        printf("your outcome is: %llu", fibonacci(numeric_input, save_to_file));
    
    
        return 0;
    }
    
    
    int long long fibonacci(int value, bool save_to_file){
        unsigned long long i = 0, a = 1, b = 1;
        unsigned long long total = 0;
        FILE *file_open;
    
    
        if (save_to_file == 1){
            char file_dir[255];
    
    
            printf("\nenter the destination and the name of the file you want to save:\n");
            scanf("%s", &file_dir);
            file_open = fopen64(file_dir, "w");
        }
    
    
        for (i = 0; i < value; i++){
    
    
            if (i % 10 == 0)
                fprintf(file_open, "\n");
    
    
            if (save_to_file == 1)
                fprintf(file_open, "%llu,  ", a);
    
    
            total = a + b;
            a = b;
            b = total;
        }
        fclose(file_open);
        return total;
    }

  2. #2
    Registered User
    Join Date
    Sep 2012
    Posts
    357
    Yes.
    Either use an external library or write functions that deal with big numbers yourself.

    You may want to google for "arbitrary precision arithmetic".

    Writing your own functions that deal with numbers as if they were strings is not difficult (especially seeing that for Fibonacci calculations, summing is enough).

  3. #3
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    You can take a look at The GNU MP Bignum Library
    Devoted my life to programming...

  4. #4
    Registered User
    Join Date
    Sep 2012
    Posts
    5
    Thanks guys i will take a look and if i find any other difficulty i will ask again

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    In this instance it would probably be best to just write a function that performs arithmetic on numeric strings.
    i.e. something you can call like this:
    Code:
    char buf[BUFLEN];
    Add("13", "21", buf, BUFLEN);
    ASSERT(strcmp(buf, "34") == 0);
    Note that you may find it easiest to calculate the result as a reversed string and then reverse that back to the right way around afterwards.
    Last edited by iMalc; 09-22-2012 at 03:35 PM.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    Registered User
    Join Date
    Sep 2012
    Posts
    5
    iMalc the truth is that i am having a hard time with the GNU library so i like your way but first time i see a code like yours... :/ is there any documentations for that, tutorials, etc...

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Angelo Uknown
    i am having a hard time with the GNU library
    In what way are you having a hard time with it?

    Quote Originally Posted by Angelo Uknown
    i like your way but first time i see a code like yours... :/ is there any documentations for that, tutorials, etc...
    iMalc was suggesting that you write your own function because such numeric string addition, if that is all that you need and if it will be fast enough, is just a matter of a simple loop or two.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    What they're chin wagging about is, if you have two char arrays, with the end of string char (true strings in C), then reverse each one, including the end of string char, it's pretty easy to add up any size number you want.

    12345\0 becomes \054321
    98765\0 becomes \056789 +
    ======
    1111110 and add a new \0 onto the left (highest index)

    Add from the 0 to index while(array[i]), and now reverse that string, and you have your sum.

  9. #9
    Registered User
    Join Date
    Sep 2012
    Posts
    5
    Quote Originally Posted by laserlight View Post
    In what way are you having a hard time with it?
    i am no a linux user and so i needed to download Cygwin but i cant use it nor find any good instructions

  10. #10
    Registered User
    Join Date
    Sep 2012
    Posts
    5
    Quote Originally Posted by Adak View Post

    12345\0 becomes \054321
    98765\0 becomes \056789 +
    ======
    1111110 and add a new \0 onto the left (highest index)

    Add from the 0 to index while(array[i]), and now reverse that string, and you have your sum.
    so is this a string array or a numerical array?

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Angelo Uknown
    i am no a linux user and so i needed to download Cygwin but i cant use it nor find any good instructions
    You do not need Cygwin. Did you read the instructions provided on the website?

    Quote Originally Posted by Angelo Uknown
    so is this a string array or a numerical array?
    A numeric string.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You can use either - char's can be used as just "small ints", if you like. I used the end of string char, but you could use any char as a marker for the end of digits.

    Try it, and post up if you get stuck. It's really not as difficult as it sounds, if you do it by hand (with a short number), a time or two. Note the pattern you use to do it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem while printing unsigned long long int
    By Dedalus in forum C Programming
    Replies: 3
    Last Post: 03-08-2012, 04:44 AM
  2. Replies: 1
    Last Post: 10-11-2010, 01:53 AM
  3. unsigned long long division print
    By Kempelen in forum C Programming
    Replies: 4
    Last Post: 01-30-2009, 10:03 AM
  4. unsigned long long to string conversion
    By Wiretron in forum C++ Programming
    Replies: 6
    Last Post: 12-21-2007, 04:02 AM
  5. Converting an unsigned int to to unsigned long long (64 bit)
    By ninjacookies in forum C Programming
    Replies: 18
    Last Post: 02-11-2005, 12:09 PM

Tags for this Thread