1

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

I have 2 question regarding this problem.

  1. How do I store this number?
  2. How do I access individual digits?
Amit Tiwari
  • 294
  • 1
  • 4
  • 13

7 Answers7

3

Store it as a string. Access the digits as characters by index.

recursive
  • 83,943
  • 34
  • 151
  • 241
2

The big integer multiplication problem. You can save the integer as char literal with an array. For example, 123 we have char a[]={1, 2, 3}. Then you can access each digit with array index.

Summer_More_More_Tea
  • 12,740
  • 12
  • 51
  • 83
1

I think, better to write this number in file, next use fgets function (copy one string from file in new buff string) make a solution

1

There is no need to store it anywhere in your program. Copy the number into a file, remove all newlines. In the program, use getchar() 1000 times (or until EOF) to read the next digit from the standard input. Use shell redirection to feed the file into the program.

./euler8 < euler8.txt
  • that's a good idea too.. but we can do it the other way round using char *. I do not have good knowledge on using files as inputs and other related things. It would be nice if you could point me to some reference regarding the file I/O. – Amit Tiwari Dec 31 '11 at 17:10
1

If you can get it into an array of integers, this will probably make the problem easiest.

Then your get_prod_of_five_consec_digits(start_index, array_of_ints) function will be able to have a reasonable signature and give better code separation.

skatenerd
  • 60
  • 7
  • 1
    Forget an array of integers. Buffer 5 characters and subtract `'0'` from incoming. – Dave Dec 31 '11 at 18:03
0

In C/C++

You can store as a string, like:

char arr[] = "73167176531330624919225119674426574742355349194934"
             "96983520312774506326239578318016984801869478851843"
             "85861560789112949495459501737958331952853208805511"
             "12540698747158523863050715693290963295227443043557"
             "66896648950445244523161731856403098711121722383113"
             "62229893423380308135336276614282806444486645238749"
             "30358907296290491560440772390713810515859307960866"
             "70172427121883998797908792274921901699720888093776"
             "65727333001053367881220235421809751254540594752243"
             "52584907711670556013604839586446706324415722155397"
             "53697817977846174064955149290862569321978468622482"
             "83972241375657056057490261407972968652414535100474"
             "82166370484403199890008895243450658541227588666881"
             "16427171479924442928230863465674813919123162824586"
             "17866458359124566529476545682848912883142607690042"
             "24219022671055626321111109370544217506941658960408"
             "07198403850962455444362981230987879927244284909188"
             "84580156166097919133875499200524063689912560717606"
             "05886116467109405077541002256983155200055935729725"
             "71636269561882670428252483600823257530420752963450";

Here, we can also write this string in one line but that cover alot horizontal space

And access as

      arr[0] - '0' => 7,
      arr[1] - '0' => 3,...

Here we subtract '0' from array just to convert it into int.

link:Why does subtracting '0' in C result in the number that the char is representing?

Orion
  • 248
  • 3
  • 10
0

You could store the number in a character array/String variable. Manipulating the resulting string and performing arithmetic operations on it is left to you. Its a good idea to create a class (/datatype) to deal with such big numbers later on. Here is a sample class created by ACRush.

Vishnu
  • 2,024
  • 3
  • 28
  • 34
  • 3
    Why do you need a big number class for this? As I understand the problem the largest possible number is 9^5 which fits easily into an int. – Duck Dec 30 '11 at 16:54
  • There is no need to use big integers. –  Dec 30 '11 at 17:38
  • "How do I store this number? How do I access individual digits?" was what he asked. Hence the answer. But yes, the solution to this particular problem will not need the use of big-integers. – Vishnu Dec 30 '11 at 17:46