File tree Expand file tree Collapse file tree 3 files changed +40
-0
lines changed Expand file tree Collapse file tree 3 files changed +40
-0
lines changed Original file line number Diff line number Diff line change 45
45
* [ Problem7] ( ./Maths/ProjectEuler/Problem7.php )
46
46
* [ Problem8] ( ./Maths/ProjectEuler/Problem8.php )
47
47
* [ Problem9] ( ./Maths/ProjectEuler/Problem9.php )
48
+ * [ Problem10] ( ./Maths/ProjectEuler/Problem10.php )
48
49
49
50
## Searches
50
51
* [ Binarysearch] ( ./Searches/BinarySearch.php )
Original file line number Diff line number Diff line change
1
+ <?php
2
+ /*
3
+ * This function solves the problem 10 of the Project Euler.
4
+ *
5
+ * Problem description:
6
+ * The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
7
+ * Find the sum of all the primes below two million.
8
+ */
9
+
10
+ /**
11
+ * @return int
12
+ */
13
+ function problem10 (): int
14
+ {
15
+ //Using Sieve of Erastothenes
16
+ //@see https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes
17
+
18
+ $ n = 2000000 ;
19
+ $ isPrime = [];
20
+
21
+ for ($ i = 2 ; $ i <= $ n ; $ i ++){
22
+ $ isPrime [$ i ] = $ i ;
23
+ }
24
+
25
+ for ($ i = 2 ; $ i *$ i <= $ n ; $ i ++){
26
+ if (isset ($ isPrime [$ i ])){
27
+ for ( $ j = $ i ; $ i *$ j <= $ n ; $ j ++)
28
+ unset($ isPrime [$ i *$ j ]);
29
+ }
30
+ }
31
+
32
+ return array_sum ($ isPrime );
33
+ }
Original file line number Diff line number Diff line change 12
12
require_once __DIR__ . '/../../Maths/ProjectEuler/Problem7.php ' ;
13
13
require_once __DIR__ . '/../../Maths/ProjectEuler/Problem8.php ' ;
14
14
require_once __DIR__ . '/../../Maths/ProjectEuler/Problem9.php ' ;
15
+ require_once __DIR__ . '/../../Maths/ProjectEuler/Problem10.php ' ;
15
16
16
17
class ProjectEulerTest extends TestCase
17
18
{
@@ -61,4 +62,9 @@ public function testProblem9(): void
61
62
$ this ->assertSame (31875000 , problem9 ());
62
63
}
63
64
65
+ public function testProblem10 (): void
66
+ {
67
+ $ this ->assertSame (142913828922 , problem10 ());
68
+ }
69
+
64
70
}
You can’t perform that action at this time.
0 commit comments