Advertisement
banovski

Project Euler, Problem #8, C

Dec 23rd, 2021 (edited)
1,360
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.67 KB | None | 0 0
  1. /* The four adjacent digits in the 1000-digit number
  2.  * 7316717653133062491922511967442657474235534919493496983520312774506
  3.  * 3262395783180169848018694788518438586156078911294949545950173795833
  4.  * 1952853208805511125406987471585238630507156932909632952274430435576
  5.  * 6896648950445244523161731856403098711121722383113622298934233803081
  6.  * 3533627661428280644448664523874930358907296290491560440772390713810
  7.  * 5158593079608667017242712188399879790879227492190169972088809377665
  8.  * 7273330010533678812202354218097512545405947522435258490771167055601
  9.  * 3604839586446706324415722155397536978179778461740649551492908625693
  10.  * 2197846862248283972241375657056057490261407972968652414535100474821
  11.  * 6637048440319989000889524345065854122758866688116427171479924442928
  12.  * 2308634656748139191231628245861786645835912456652947654568284891288
  13.  * 3142607690042242190226710556263211111093705442175069416589604080719
  14.  * 8403850962455444362981230987879927244284909188845801561660979191338
  15.  * 7549920052406368991256071760605886116467109405077541002256983155200
  16.  * 05593572972571636269561882670428252483600823257530420752963450
  17.  * that have the greatest product are: 9 × 9 × 8 × 9 = 5832. Find the
  18.  * thirteen adjacent digits in the 1000-digit number that have the
  19.  * greatest product. What is the value of this product?
  20.  */
  21.  
  22. #include <stdio.h>
  23.  
  24. int main()
  25. {
  26.      char source_sequence[] = "7316717653133062491922511967442657474235534919493\
  27. 49698352031277450632623957831801698480186947885184385861560789112949495459501737\
  28. 95833195285320880551112540698747158523863050715693290963295227443043557668966489\
  29. 50445244523161731856403098711121722383113622298934233803081353362766142828064444\
  30. 86645238749303589072962904915604407723907138105158593079608667017242712188399879\
  31. 79087922749219016997208880937766572733300105336788122023542180975125454059475224\
  32. 35258490771167055601360483958644670632441572215539753697817977846174064955149290\
  33. 86256932197846862248283972241375657056057490261407972968652414535100474821663704\
  34. 84403199890008895243450658541227588666881164271714799244429282308634656748139191\
  35. 23162824586178664583591245665294765456828489128831426076900422421902267105562632\
  36. 11111093705442175069416589604080719840385096245544436298123098787992724428490918\
  37. 88458015616609791913387549920052406368991256071760605886116467109405077541002256\
  38. 98315520005593572972571636269561882670428252483600823257530420752963450";
  39.  
  40.      char checked_sequence[14];
  41.      long int accumulator;
  42.      long int max_value = 1;
  43.      short int i;
  44.      char j;
  45.  
  46.      for(i = 0; i < 987; i++)
  47.      {
  48.           accumulator = 1;
  49.           for(j = 0; j < 13; j++)
  50.           {
  51.                switch (source_sequence[i + j])
  52.                {
  53.                case '0': accumulator *= 0; break;
  54.                case '1': accumulator *= 1; break;
  55.                case '2': accumulator *= 2; break;
  56.                case '3': accumulator *= 3; break;
  57.                case '4': accumulator *= 4; break;
  58.                case '5': accumulator *= 5; break;
  59.                case '6': accumulator *= 6; break;
  60.                case '7': accumulator *= 7; break;
  61.                case '8': accumulator *= 8; break;
  62.                case '9': accumulator *= 9; break;
  63.                }
  64.           }
  65.           if(accumulator > max_value)
  66.           {
  67.                max_value = accumulator;
  68.                for(j = 0; j < 13; j++)
  69.                     checked_sequence[j] = source_sequence[i + j];
  70.           }
  71.      }
  72.      printf("The sequence is %s, the product is %ld\n",
  73.             checked_sequence, max_value);
  74.      return(0);
  75. }
  76.  
  77. /* The sequence is 5576689664895, the product is 23514624000 */
  78.  
  79. /* real 0m0,003s
  80.  * user 0m0,000s
  81.  * sys  0m0,002s */
  82.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement