-
Notifications
You must be signed in to change notification settings - Fork 0
/
prob8.c
40 lines (33 loc) · 1.58 KB
/
prob8.c
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 <stdio.h>
int main(){
printf("13. Find the thirteen adjacent digits in the 1000-digit number that have the \
greatest product.\n");
const char str[] = "73167176531330624919225119674426574742355349194934969835203127745063262\
39578318016984801869478851843858615607891129494954595017379583319528532088055111254069874\
71585238630507156932909632952274430435576689664895044524452316173185640309871112172238311\
36222989342338030813533627661428280644448664523874930358907296290491560440772390713810515\
85930796086670172427121883998797908792274921901699720888093776657273330010533678812202354\
21809751254540594752243525849077116705560136048395864467063244157221553975369781797784617\
40649551492908625693219784686224828397224137565705605749026140797296865241453510047482166\
37048440319989000889524345065854122758866688116427171479924442928230863465674813919123162\
82458617866458359124566529476545682848912883142607690042242190226710556263211111093705442\
17506941658960408071984038509624554443629812309878799272442849091888458015616609791913387\
54992005240636899125607176060588611646710940507754100225698315520005593572972571636269561\
882670428252483600823257530420752963450";
int arr[1000], i, j, ptr=0;
size_t prod, max=0;
for(i=0; i<1000; i++)
arr[i] = str[i] - '0';
//1000 - 12 = 988
for(i=0; i<988; i++){
prod=1;
for(j=i; j<13+i; j++)
prod *= arr[j];
ptr = (prod>max) ? i : ptr;
max = (prod>max) ? prod : max;
}
const char *s = str+ptr;
printf("%lu is the greatest product, produced by the sequence %.*s, \
starting at index %d.\n", max, 13, s, ptr);
return 0;
}