Welcome to HBH! If you had an account on hellboundhacker.org you will need to reset your password using the Lost Password system before you will be able to login.

c++ large numbers


p4plus2's Avatar
Member
0 0

The following program I am trying to create is supposed to find the answer to 2^1000. The problem is that I get a massively wrong answer that is about 100 numbers longer than it should be. My attempt at the problem was by far not the most straight forward method, but at the time seemed like it would be interesting to try. My point in posting is to ask if anybody can see a mistake that I made or whether I should simply scrap this and start over at square one.

My program:

See working code a few posts down. See working code a few posts down.

#include <iostream>
int numb[500] = {0};
int sum = 0;
int main(int argc, char **argv)
{
	numb[499] = 1;								//set to 1 so we actually get an answer
	for(int i = 0; i < 1000; i++){					//loop for number of squaring interations
		for(int j = 499; j >= 0; j--){				//number of elements to scan through
			if(numb[j] >= 5){					//check if we will have to carry
				if(numb[j-1] != 9){				//check if there if double carry(not loop)
					numb[j-1]++;				//increment the place above the carry if no double carry
				}else{						//start double carry code
					numb[j-1] = 0;				//set 9 to 0 for the carry
					for(int k = j-2; k >=0; k--){	//loop through untill there is no more double carry
						if(numb[k] != 9){		//check for more double carry(loop)
							numb[k]++;			//increment k if no more double carry(finishing the j-1)
							break;			//break out of the loop
						}else{				//more double carry
							numb[k] = 0;		//set k to zero
						}					//end double carry check(loop)
					}						//end double carry loop
				}							//end double carry check(not loop)
				numb[j] *= 2;					//double j
				numb[j] -= 10;					//subtract 10 to remove all carry over
			}else{							//start no carry
				numb[j] *= 2;					//simply double
			}								//end of all carry checking
		}									//end loop for doubling each digit
	}										//end all 1000 iterations.
	for(int i = 0; i < 500; i++){
		std::cout<<numb[i];
	}
	std::cout<<std::endl;
	return 0;
}

Something odd about my output is that all numbers are even or zero as if the carry is handled wrong(which it probably is). But I commented it heavy so figuring out what I was trying to do shouldn't be hard.

My program's output:

24406404260220488888248284264884886088080862828400802642082240068684624686206464042080628066684800486044644288864262284286404820828822664288862248868640800828004206420404042464624208602806484204020426480460608622044222806240024228444862666028680406044888286242000688884082040088602446260622042080460266266060264026240820624608246046264268040668664282202422264600068620200400088486686046222280628640222266826068022460684202006204446

The correct output should be:

10715086071862673209484250490600018105614048117055336074437503883703510511249361224931983788156958581275946729175531468251871452856923140435984577574698574803934567774824230985421074605062371141877954182153046474983581941267398767559165543946077062914571196477686542167660429831652624386837205668069376

EDIT: I just realized what went wrong. I will post the code here as soon as I come up with a work around.(go figure I realize the problem just after posting.)


ghost's Avatar
0 0

Holy shit, I know I dont comment near enough in my code, but wow… overdone much?


ghost's Avatar
0 0

Fix'd. If you want to compare code, send me a PM as I kinda just rewrote it.


stealth-'s Avatar
Ninja Extreme
0 0

stdio wrote: Holy shit, I know I dont comment near enough in my code, but wow… overdone much?

Some people comment alot when they first start out in a programming language. I did that with python, my first language. Even print statements had comments for me back then XD


p4plus2's Avatar
Member
0 0

stdio wrote: Holy shit, I know I dont comment near enough in my code, but wow… overdone much?

I actually wrote this with no comments, but I commented this before posting so my logic behind what I was doing was clear.

@com As for the rewritten code I found my problem, I was adding the carry at the wrong spot being dumb. My problem was that I added the carry before the multiplication when it should be after, I should have paid more attention to something like that. After I get it working I will PM you so that I can compare my code for slowdowns and such.

@moshbat I need the full number not just the notation version.

EDIT:

working code

#include <iostream>
int numb[500] = {0};
int carry, sum = 0;
int main(int argc, char **argv)
{
	numb[499] = 1;
	for(int i = 0; i < 1000; i++){
		for(int j = 499; j >= 0; j--){
			numb[j] <<= 1;
			numb[j] += carry;
			carry = 0;
			if(numb[j] >= 10){
				numb[j] -= 10;
				carry = 1;
			}
		}
	}
	for(int i = 0; i < 500; i++){
		std::cout<<numb[i];
	}
	std::cout <<std::endl;
	return 0;
}