Skip to content

Commit cc86582

Browse files
committed
tp2 solution
1 parent 073e9e8 commit cc86582

File tree

2 files changed

+178
-1
lines changed
  • tp2/src
    • main/kotlin/fmt/kotlin/fundamentals
    • test/kotlin/fmt/kotlin/fundamentals

2 files changed

+178
-1
lines changed
Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,75 @@
11
package fmt.kotlin.fundamentals
22

3+
import kotlin.text.Typography.prime
4+
35
class Tp2 {
46

57
fun getFirstPrimeNumbers(nbToFind: Int): List<Int> {
6-
return arrayListOf<Int>()
8+
// The list containing all the prime numbers we found
9+
// It is a val because we will modify the array but not build a new array each time we add something to it
10+
val primeNumbers = arrayListOf<Int>()
11+
12+
// Start with 2 as 0 and 1 are not prime numbers
13+
// n is a var because we will change it at the end of each loop to test the next number
14+
var n = 2
15+
16+
// Iterate while we don't have enough prime numbers in the list we want to return
17+
while (primeNumbers.size < nbToFind) {
18+
var prime = true
19+
20+
// Iterate over every number until the one we want to test to see if it can be divided
21+
for (i in 2..n - 1) {
22+
if (n % i == 0) {
23+
prime = false
24+
// It if can be divided once, no need to test other numbers
25+
break
26+
}
27+
}
28+
29+
// Add it to the list we want to return, if we did not find a divisor
30+
if (prime) {
31+
primeNumbers.add(n)
32+
}
33+
34+
// We try the next number
35+
n++
36+
}
37+
38+
return primeNumbers
39+
}
40+
41+
fun getFirstPrimeNumbersBetter(nbToFind: Int): List<Int> {
42+
// The list containing all the prime numbers we found
43+
// It is a val because we will modify the array but not build a new array each time we add something to it
44+
val primeNumbers = arrayListOf<Int>()
45+
46+
// Start with 2 as 0 and 1 are not prime numbers
47+
// n is a var because we will change it at the end of each loop to test the next number
48+
var n = 2
49+
50+
// Iterate while we don't have enough prime numbers in the list we want to return
51+
while (primeNumbers.size < nbToFind) {
52+
// Add it to the list we want to return, if we did not find a divisor
53+
if (isPrime(n, primeNumbers)) {
54+
primeNumbers.add(n)
55+
}
56+
57+
// We try the next number
58+
n++
59+
}
60+
61+
return primeNumbers
62+
}
63+
64+
private fun isPrime(n: Int, primeNumbers: List<Int>): Boolean {
65+
for (i in primeNumbers) {
66+
if (canBeDivided(n, i)) {
67+
// It if can be divided once, no need to test other numbers
68+
return false
69+
}
70+
}
71+
return true
772
}
73+
74+
private fun canBeDivided(n: Int, i: Int) = n % i == 0
875
}

tp2/src/test/kotlin/fmt/kotlin/fundamentals/Tp2Test.kt

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,116 @@ class Tp2Test {
122122
)
123123
)
124124
}
125+
126+
@Test
127+
fun `should return 100 first prime numbers better`() {
128+
val numbers = tp2.getFirstPrimeNumbersBetter(100)
129+
130+
expectThat(numbers).isEqualTo(
131+
listOf(
132+
2,
133+
3,
134+
5,
135+
7,
136+
11,
137+
13,
138+
17,
139+
19,
140+
23,
141+
29,
142+
31,
143+
37,
144+
41,
145+
43,
146+
47,
147+
53,
148+
59,
149+
61,
150+
67,
151+
71,
152+
73,
153+
79,
154+
83,
155+
89,
156+
97,
157+
101,
158+
103,
159+
107,
160+
109,
161+
113,
162+
127,
163+
131,
164+
137,
165+
139,
166+
149,
167+
151,
168+
157,
169+
163,
170+
167,
171+
173,
172+
179,
173+
181,
174+
191,
175+
193,
176+
197,
177+
199,
178+
211,
179+
223,
180+
227,
181+
229,
182+
233,
183+
239,
184+
241,
185+
251,
186+
257,
187+
263,
188+
269,
189+
271,
190+
277,
191+
281,
192+
283,
193+
293,
194+
307,
195+
311,
196+
313,
197+
317,
198+
331,
199+
337,
200+
347,
201+
349,
202+
353,
203+
359,
204+
367,
205+
373,
206+
379,
207+
383,
208+
389,
209+
397,
210+
401,
211+
409,
212+
419,
213+
421,
214+
431,
215+
433,
216+
439,
217+
443,
218+
449,
219+
457,
220+
461,
221+
463,
222+
467,
223+
479,
224+
487,
225+
491,
226+
499,
227+
503,
228+
509,
229+
521,
230+
523,
231+
541
232+
)
233+
)
234+
}
125235
}
126236

127237
}

0 commit comments

Comments
 (0)