biggest number handled by c++ ?

Pages: 12
Problem number 8 on Project Euler:
Find the greatest product of five consecutive digits in the 1000-digit number.

73167176531330624919225119674426574742355349194934
96983520312774506326239578318016984801869478851843
85861560789112949495459501737958331952853208805511
12540698747158523863050715693290963295227443043557
66896648950445244523161731856403098711121722383113
62229893423380308135336276614282806444486645238749
30358907296290491560440772390713810515859307960866
70172427121883998797908792274921901699720888093776
65727333001053367881220235421809751254540594752243
52584907711670556013604839586446706324415722155397
53697817977846174064955149290862569321978468622482
83972241375657056057490261407972968652414535100474
82166370484403199890008895243450658541227588666881
16427171479924442928230863465674813919123162824586
17866458359124566529476545682848912883142607690042
24219022671055626321111109370544217506941658960408
07198403850962455444362981230987879927244284909188
84580156166097919133875499200524063689912560717606
05886116467109405077541002256983155200055935729725
71636269561882670428252483600823257530420752963450

My code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
using namespace std;
main ()
{long number=7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450;
long p=1, five_d, max=0;

while (number!=0)
{
    five_d=number%100000;
    number/=100000;
    
  while(five_d!=0)
  {
      p*=five_d%10;
      five_d/=10;
  }

  if(p>max)
  max=p;
}

cout<<endl<<max;
}


The warnings:
D:\CodeBlocks\programe\main.cpp|4|warning: integer constant is too large for its type|
D:\CodeBlocks\programe\main.cpp|4|warning: this decimal constant is unsigned only in ISO C90|
D:\CodeBlocks\programe\main.cpp|4|warning: integer constant is too large for 'long' type|
D:\CodeBlocks\programe\main.cpp||In function 'int main()':|
D:\CodeBlocks\programe\main.cpp|4|warning: overflow in implicit constant conversion|
||=== Build finished: 0 errors, 4 warnings ===|

So what is the biggest number usable by c++ language?
Ignoring all the arbitrary precision stuff, the largest unsigned integer C++ can handle is 18,446,744,073,709,551,616, which is a long long int.

Good luck with your problem. :)

EDIT: Originally had a hint, but removed it in case the OP didn't want it.

-Albatross
Last edited on
short answer:
Its depends on your compiler

Slightly longer answer:
All numbers are just bits and the max number you can have will be dependent on the max number of bits that data type provides.

Below are common assamputions that most programmers hope compiler writers make with regards to data type's max number tolerance:

char = 8bits = 2^8
short = 16bits = 2^16
int = 32bits = 2^32
...

Last edited on
Thank you both.
@Albatross I didn't played with it enough, it's to early for hints. I have a few ideas of my own (rubbish or not) that I plan to test tomorrow, and if whatever I come up with won't work, I'll ask for some tips 'n hints.
Till then, have a good night. :)
...On another note once you've learnt that numbers are just "magic tricks" (which is what programming is at heart (the art of creating illusions)), there is theoretically no limit to the number data type you can create. Though you'll have to decide what you wants your bits (them 1s and 0s) to mean, which is all quite easy once you know how.

this is my theoretical super number class:

class Cbigassnumber
{
private:
//this is the class that would allow me to accept a super number without breaking the memory
Chandlesupanumbers m_qGetmyDamnnumber;

public:

void get_number_subroutine();

void manipulate_number_options_subroutine();

void output_number();

//other details omitted, but that goes to show you that what you see on the surface is a nice
// comfy illusion that is revealed when bugs occur and all the fancy wizardry starts to lie there naked
...

};
Last edited on
It's easy! Make the 1000-digit number a string!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
using namespace std;
int main ()
{string number="7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450";
long p=1, five_d, max=0, i;
const char* a=number.c_str();
for (p=0; p<995; p++)
{
      five_d=1;
      for (i=p; i<p+5; i++)
      {
            five_d*=(int(a[i])-48);
            }
       if (five_d>max) max=five_d;
       }
cout<<endl<<max;
system ("pause");
return 0;
}
@viliml
Please read the OP's last post. I'm pretty sure it says "...It's to early for hints. I have a few ideas of my own (rubbish or not) that I plan to test tomorrow, and if whatever I come up with won't work, I'll ask for some tips 'n hints."

Also, I don't/didn't see a proper header for using that class that you suggested using. :(

-Albatross
Last edited on
@viliml string would be really dangerous since they can rely on the heap. (they create new variables at run time to accommodate what you send to them)
Initially I thought about making that number a character string(like char s[1000], and take from it 10 characters at a time, convert them into numbers (atoi()) and make the product. But it didn't seemed to work, and there where some bits I didn't know how to write, and the char s[1000] couldn't handle the number anyway.
Then like in Viliml's program I used a character string with * (pointer) and this accepted the number. Sadly my main guidebook for syntax and other code resources is my classbook along with my manuals, which are not very advanced, so there are many things i don't know (like using classes as Blessman11 suggested). Here is what I've done:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <string.h>
using namespace std;
int main ()
{
    char *s="7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450";
long p=1; int max=0; int aux, i;
  for(i=0;i<=1000;i++)
 {
  p*=s[i];

  if(i%4==0 && i!=0)

 {aux=p;
  p=1;

  if(aux>max)
  max=aux;
 }

 }
 cout<<endl<<max;
}


The output is obviously not the result I'm looking for, because it has many more digits than the product of any five one digit numbers.
I this happens because elements from character strings don't multiply like vector elements.
Initially I thought about making that number a character string(like char s[1000], and take from it 10 characters at a time, convert them into numbers (atoi()) and make the product. But it didn't seemed to work, and there where some bits I didn't know how to write, and the char s[1000] couldn't handle the number anyway.
Then like in Viliml's program I used a character string with * (pointer) and this accepted the number. Sadly my main guidebook for syntax and other code resources is my classbook along with my manuals, which are not very advanced, so there are many things i don't know (like using classes as Blessman11 suggested).

*This is what I wrote in the post above.
I saved the number into a string and then tried to convert one element of the string at a time into a number but I get some errors. Can someone help?
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <stdlib.h>
using namespace std;
int main ()
{
   char *s="7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450";

long p=1; int max=0; int aux, i, n;
  for(i=0;i<=1000;i++)
 { n=atoi(s[i]);
 cout<<n;}}


D:\CodeBlocks\programe\1_20_euler.cpp||In function 'int main()':|
D:\CodeBlocks\programe\1_20_euler.cpp|6|warning: deprecated conversion from string constant to 'char*'|
D:\CodeBlocks\programe\1_20_euler.cpp|10|error: invalid conversion from 'char' to 'const char*'|
D:\CodeBlocks\programe\1_20_euler.cpp|10|error: initializing argument 1 of 'int atoi(const char*)'|
||=== Build finished: 2 errors, 1 warnings ===|
First create a sequence (a vector or an array) of 1000 integers from the digits of the number. Then find the the greatest product of five consecutive integers in the sequence.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
const char* const digits =
    "73167176531330624919225119674426574742355349194934"
    "96983520312774506326239578318016984801869478851843"
    "85861560789112949495459501737958331952853208805511"
    "12540698747158523863050715693290963295227443043557"
    "66896648950445244523161731856403098711121722383113"
    "62229893423380308135336276614282806444486645238749"
    "30358907296290491560440772390713810515859307960866"
    "70172427121883998797908792274921901699720888093776"
    "65727333001053367881220235421809751254540594752243"
    "52584907711670556013604839586446706324415722155397"
    "53697817977846174064955149290862569321978468622482"
    "83972241375657056057490261407972968652414535100474"
    "82166370484403199890008895243450658541227588666881"
    "16427171479924442928230863465674813919123162824586"
    "17866458359124566529476545682848912883142607690042"
    "24219022671055626321111109370544217506941658960408"
    "07198403850962455444362981230987879927244284909188"
    "84580156166097919133875499200524063689912560717606"
    "05886116467109405077541002256983155200055935729725"
    "71636269561882670428252483600823257530420752963450" ;

std::vector<int> seq ;
for( std::size_t i = 0 ; i < sizeof(digits)-1 ; ++i )
       seq.push_back( digits[i] - '0' ) ;

// now Find the greatest product of five consecutive integers in the sequence 
atoi() expects a string of those characters. Here are two options I'd suggest.

#1: Use string::substr() to get a one character long snippet of your string and use string::c_str to get its C-string equivalent.
http://cplusplus.com/reference/string/string/substr/

#2: Just subtract the ASCII code for '0' from your character, skip the atoi(), and you'll have your digit. :)

JLBorges illustrated #2 for you. :)

-Albatross
I tried to understand #2 but failed miserably. I tried to use #1 but got some errors, and had no idea why. Now I did this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
 #include <iostream>
#include <string.h>
#include <stdlib.h>
using namespace std;

int main () //just wanted to display each digit, and after that, using atoi, i could solve it
{int n,i;
 const char * str="73167176531330624919225119674426574742355349194934"
    "96983520312774506326239578318016984801869478851843"
    "85861560789112949495459501737958331952853208805511"
    "12540698747158523863050715693290963295227443043557"
    "66896648950445244523161731856403098711121722383113"
    "62229893423380308135336276614282806444486645238749"
    "30358907296290491560440772390713810515859307960866"
    "70172427121883998797908792274921901699720888093776"
    "65727333001053367881220235421809751254540594752243"
    "52584907711670556013604839586446706324415722155397"
    "53697817977846174064955149290862569321978468622482"
    "83972241375657056057490261407972968652414535100474"
    "82166370484403199890008895243450658541227588666881"
    "16427171479924442928230863465674813919123162824586"
    "17866458359124566529476545682848912883142607690042"
    "24219022671055626321111109370544217506941658960408"
    "07198403850962455444362981230987879927244284909188"
    "84580156166097919133875499200524063689912560717606"
    "05886116467109405077541002256983155200055935729725"
    "71636269561882670428252483600823257530420752963450" ;
    char* s;
for(i=0; i<=1000;i++)
{
strncpy(s,str+i,i+1);
cout<<s<<endl;
}
} //but instead of displaying each digit it shows this, down below
 






7
31
167
6717    
71765   
176531  
7653133 
65313306
531330624
3133062491@
13306249192
330624919225É▲4
3062491922511▲4
062491922511964
624919225119674
2491922511967442

Process returned -1073741819 (0xC0000005) execution time : 0.070 s
Press any key to continue.
Instead of showing one digit at a time, at each i++, the program shows one more digit, so when the number is getting too large all those strange symbols appear.
Never mind
strncpy(s,str+i,1);
Still you never allocated space for 's'.
By the way in 'abcdefg'
bcdef are 5 consecutive elements.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
#include <string.h>
#include <stdlib.h>
using namespace std;

main ()
{long n,i,p=1,max=0;
 const char * str="73167176531330624919225119674426574742355349194934"
    "96983520312774506326239578318016984801869478851843"
    "85861560789112949495459501737958331952853208805511"
    "12540698747158523863050715693290963295227443043557"
    "66896648950445244523161731856403098711121722383113"
    "62229893423380308135336276614282806444486645238749"
    "30358907296290491560440772390713810515859307960866"
    "70172427121883998797908792274921901699720888093776"
    "65727333001053367881220235421809751254540594752243"
    "52584907711670556013604839586446706324415722155397"
    "53697817977846174064955149290862569321978468622482"
    "83972241375657056057490261407972968652414535100474"
    "82166370484403199890008895243450658541227588666881"
    "16427171479924442928230863465674813919123162824586"
    "17866458359124566529476545682848912883142607690042"
    "24219022671055626321111109370544217506941658960408"
    "07198403850962455444362981230987879927244284909188"
    "84580156166097919133875499200524063689912560717606"
    "05886116467109405077541002256983155200055935729725"
    "71636269561882670428252483600823257530420752963450" ;
    char* s;
for(i=0; i<=1000;i++)
{
strncpy(s,str+i,1);
p*=atoi(s);
if(i%4==0 && i!=0)
  { if(p>max)
max=p;
p=1;
  }
}
cout<<max;
}


This code is almost right. It's just the fact that it does the product of the first 5 and then it does the product of the numbers from 6 to10 etc. when it should remove the first digit and do the product of the first 5 again... I'll look into it more.
I've researched a bit on google and it seems that I could solve this much easier with string instead of char like Albatross suggested, I'll just have to find a way around the errors I get... in fact I just need to learn how to use strings. We'll looks like I know what I'll be doing tonight. :)
Yes, a string and you won't have to convert it to an int either, if you work at the bit level, keeping in mind that in 8-bits:

11110000 > 00001111 as is 11100000 to 00000111 and 11000000/00000011...etc. You should get the picture.

Off hand though, did anyone else giggle a bit when they saw that 100-digit number trying to squeeze into an int? No offense to your inexperience catalin. It was just funny to me.
Last edited on
Pages: 12