Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit ea36915

Browse files
Merge pull request TheAlgorithms#711 from pomkarnath98/master
Added ProjectEuler problem 8 with passed test cases
2 parents 15835ed + b38a94d commit ea36915

File tree

2 files changed

+130
-0
lines changed

2 files changed

+130
-0
lines changed

‎Project-Euler/Problem8.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Problem: https://projecteuler.net/problem=8
2+
3+
const largestAdjacentNumber = (grid, consecutive) => {
4+
grid = grid.split('\n').join('')
5+
const splitedGrid = grid.split('\n')
6+
let largestProd = 0
7+
8+
for (const row in splitedGrid) {
9+
const currentRow = splitedGrid[row].split('').map(x => Number(x))
10+
11+
for (let i = 0; i < currentRow.length - consecutive; i++) {
12+
const combine = currentRow.slice(i, i + consecutive)
13+
14+
if (!combine.includes(0)) {
15+
const product = combine.reduce(function (a, b) {
16+
return a * b
17+
})
18+
19+
if (largestProd < product) largestProd = product
20+
}
21+
}
22+
}
23+
return largestProd
24+
}
25+
26+
export { largestAdjacentNumber }

‎Project-Euler/test/Problem8.test.js

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
import { largestAdjacentNumber } from '../Problem8'
2+
3+
const grid1 = `73167176531330624919225119674426574742355349194934
4+
96983520312774506326239578318016984801869478851843
5+
85861560789112949495459501737958331952853208805511
6+
12540698747158523863050715693290963295227443043557
7+
66896648950445244523161731856403098711121722383113
8+
38991007832084782777189757278819047278899274961949
9+
30358907296290491560440772390713810515859307960866
10+
70172427121883998797908792274921901699720888093776
11+
65727333001053367881220235421809751254540594752243
12+
82834772897718748193457134085784719857638187485919
13+
53697817977846174064955149290862569321978468622482
14+
83972241375657056057490261407972968652414535100474
15+
82166370484403199890008895243450658541227588666881
16+
83247832478757285710958372981577583275832789327921
17+
17866458359124566529476545682848912883142607690042
18+
24219022671055626321111109370544217506941658960408
19+
84327878357761783787589375857378271083787811983779
20+
84580156166097919133875499200524063689912560717606
21+
05886116467109405077541002256983155200055935729725
22+
82347875831098357801578571807585817518287829189189`
23+
24+
const grid2 = `73167176531330624919225119674426574742355349194934
25+
96983520312774506326239578318016984801869478851843
26+
85861560789112949495459501737958331952853208805511
27+
12540698747158523863050715693290963295227443043557
28+
66896648950445244523161731856403098711121722383113
29+
62229893423380308135336276614282806444486645238749
30+
30358907296290491560440772390713810515859307960866
31+
70172427121883998797908792274921901699720888093776
32+
65727333001053367881220235421809751254540594752243
33+
52584907711670556013604839586446706324415722155397
34+
53697817977846174064955149290862569321978468622482
35+
83972241375657056057490261407972968652414535100474
36+
82166370484403199890008895243450658541227588666881
37+
16427171479924442928230863465674813919123162824586
38+
17866458359124566529476545682848912883142607690042
39+
24219022671055626321111109370544217506941658960408
40+
07198403850962455444362981230987879927244284909188
41+
84580156166097919133875499200524063689912560717606
42+
05886116467109405077541002256983155200055935729725
43+
71636269561882670428252483600823257530420752963450`
44+
45+
const grid3 = `89125732138957892357892768971807934878999818278898
46+
48327483578957875827583295789187588875238579887789
47+
74358275778171870973857835738758275210873583758279
48+
81347834738473878758758373857893758787772897580719
49+
81232847857895758758987587501087575387538183787098
50+
17047878395783578750837100983787587582797837508298
51+
42894789325732857893759187987487489748377578791989
52+
82147328972387832578327581919827382758932789798289
53+
83243289473847328974832947832748932472387895738978
54+
84738294738973289578753287582375238957573297892398
55+
29383748329748397483274832748327477575018978975289
56+
48327483758375846372864736476478364783647463278787
57+
73281473847832974328758975890189373857875875895898
58+
74328978748329789357389578329758329758937893758979
59+
81738957389579287598217589127589375893275987359889
60+
71890743894732897510875895783297591085738975837897
61+
10783974839479879857895789758975981735870175835789
62+
01494787857897583758975849758475107589754897589789
63+
09939858758919788017587897587387585775289757982898
64+
74718478978758758975897589789789798789178957789789`
65+
66+
const grid4 = `99999999999999999999999999999999999999999999999999
67+
99999999999999999999999999999999999999999999999999
68+
99999999999999999999999999999999999999999999999999
69+
99999999999999999999999999999999999999999999999999
70+
99999999999999999999999999999999999999999999999999
71+
99999999999999999999999999999999999999999999999999
72+
99999999999999999999999999999999999999999999999999
73+
99999999999999999999999999999999999999999999999999
74+
99999999999999999999999999999999999999999999999999
75+
99999999999999999999999999999999999999999999999999
76+
99999999999999999999999999999999999999999999999999
77+
99999999999999999999999999999999999999999999999999
78+
99999999999999999999999999999999999999999999999999
79+
99999999999999999999999999999999999999999999999999
80+
99999999999999999999999999999999999999999999999999
81+
99999999999999999999999999999999999999999999999999
82+
99999999999999999999999999999999999999999999999999
83+
99999999999999999999999999999999999999999999999999
84+
99999999999999999999999999999999999999999999999999
85+
99999999999999999999999999999999999999999999999999`
86+
87+
describe('checkLargestAdjacentNumberProduct', () => {
88+
it('Random Example', () => {
89+
const RESULT = largestAdjacentNumber(grid1, 13)
90+
expect(RESULT).toBe(23514624000)
91+
})
92+
it('Problem statement Example', () => {
93+
const RESULT = largestAdjacentNumber(grid2, 13)
94+
expect(RESULT).toBe(23514624000)
95+
})
96+
it('Random Example 2', () => {
97+
const RESULT = largestAdjacentNumber(grid3, 13)
98+
expect(RESULT).toBe(580717154304)
99+
})
100+
it('Example with all 9', () => {
101+
const RESULT = largestAdjacentNumber(grid4, 13)
102+
expect(RESULT).toBe(2541865828329)
103+
})
104+
})

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /