computing a HUUUGEE value...

This is a discussion on computing a HUUUGEE value... within the C Programming forums, part of the General Programming Boards category; I am trying to compute a really HUUUGE value... (i am taking 1 and doubling it 64 times) When the ...

  1. #1
    Registered User moonwalker's Avatar
    Join Date
    Jul 2002
    Posts
    282

    computing a HUUUGEE value...

    I am trying to compute a really HUUUGE value...
    (i am taking 1 and doubling it 64 times)


    When the game of chess was invented in India, the king really liked the game, and thought it would be really useful in teaching war strategy. He offered to give the inventor whatever he wanted. The inventor asked that one grain of wheat be put on the first square, two on the second, four on the third, and so forth, doubling the number of grains on each square until all squares had been covered. At first the king was shocked by the inventor's demand for so little. But, then the king came to understand that there was not enough wheat in the entire world to cover all the squares on the chess board as the inventor had asked.


    Just out of curiosity, i am trying to compute that value..

    i used long int but it couldn't
    get past 31st square..

    here's my code:
    Code:
    #include <stdio.h>
    
    int main()
    {
    	int i;
    	long int val = 1;
    
    	FILE *fp = fopen("results.txt", "w");
    
    	for (i = 1; i <= 64; i++)
    	{
    		fprintf(fp, "%2d Square %ld Grains\n", i, val);
    		val *= 2;			
    	}
    	
    }
    and yeah, i need to check for NULL at fopen... too lazy to do that..
    (I am just trying to save it to a text file to see the results
    clearly)

    How do i make it work for much much bigger values? (just to
    see what the answer will be for the 64th square )

    Thanks.
    Last edited by moonwalker; 08-20-2002 at 09:29 PM.

  2. #2
    moi
    moi is offline
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    64th square: 9223372036854775808

    total of all squares: 18446744073709551615
    hello, internet!

  3. #3
    Registered User sean345's Avatar
    Join Date
    Mar 2002
    Posts
    346
    This way worked for me
    Code:
    #include <stdio.h>
    
    int main()
    {
    	int i;
    	double val = 1.0;
    
    	for (i = 1; i <= 64; i++)
    	{
    		printf("%d Square %g\n", i, val);
    		val *= 2;
    	}
    	return 0;
    }
    - Sean
    If cities were built like software is built, the first woodpecker to come along would level civilization.
    Black Frog Studios

  4. #4
    Registered User moonwalker's Avatar
    Join Date
    Jul 2002
    Posts
    282

    hmm

    thanks... what does %g mean ? is it just for double ? or .. ?

    it's showing 9.22337e+18

    how do i get the number without the e+ form ?
    Last edited by moonwalker; 08-20-2002 at 09:47 PM.

  5. #5
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,220
    i think 9.22337e+18 means
    9.22337*10^18

    therefore 9223370000000000000

  6. #6
    Registered User moonwalker's Avatar
    Join Date
    Jul 2002
    Posts
    282

    yeah

    yeah, i know that...
    i was asking ... how do i make it show
    9223372036854775808 ???

  7. #7
    Registered User sean345's Avatar
    Join Date
    Mar 2002
    Posts
    346
    I believe you can show it that way using %.0f

    - Sean
    If cities were built like software is built, the first woodpecker to come along would level civilization.
    Black Frog Studios

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    When the game of chess was invented in India, the king really liked the game, and thought it would be really useful in teaching war strategy. He offered to give the inventor whatever he wanted. The inventor asked that one grain of wheat be put on the first square, two on the second, four on the third, and so forth, doubling the number of grains on each square until all squares had been covered. At first the king was shocked by the inventor's demand for so little. But, then the king came to understand that there was not enough wheat in the entire world to cover all the squares on the chess board as the inventor had asked.
    This is an old story. I've heard this done in both a Chinese and Japanese variety. In the Japanese (and likely Chinese) versions however, they use rice instead of wheat.

    To make it show a number that huge, you basicly need to use a string and do digit by digit multiplication on the string, adding a character when you need to. Then use whatever method you want to display it.

    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Registered User moonwalker's Avatar
    Join Date
    Jul 2002
    Posts
    282

    hmm

    thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help me pls...About computing a grade
    By Sunday in forum C Programming
    Replies: 2
    Last Post: 11-03-2007, 01:41 PM
  2. Computing Degree Question
    By cjwenigma in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 09-17-2007, 02:06 PM
  3. small Distributed computing utilies
    By vinit in forum C Programming
    Replies: 2
    Last Post: 04-17-2006, 04:37 AM
  4. computing time elapsed?
    By redemption in forum C Programming
    Replies: 2
    Last Post: 09-09-2002, 07:55 AM
  5. Online news about the computing world...
    By Nutshell in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 02-15-2002, 08:16 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21