Skip to content

Commit

Permalink
tp2 solution
Browse files Browse the repository at this point in the history
  • Loading branch information
ctruchi committed Feb 7, 2025
1 parent 073e9e8 commit cc86582
Show file tree
Hide file tree
Showing 2 changed files with 178 additions and 1 deletion.
69 changes: 68 additions & 1 deletion tp2/src/main/kotlin/fmt/kotlin/fundamentals/Tp2.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,75 @@
package fmt.kotlin.fundamentals

import kotlin.text.Typography.prime

class Tp2 {

fun getFirstPrimeNumbers(nbToFind: Int): List<Int> {
return arrayListOf<Int>()
// The list containing all the prime numbers we found
// It is a val because we will modify the array but not build a new array each time we add something to it
val primeNumbers = arrayListOf<Int>()

// Start with 2 as 0 and 1 are not prime numbers
// n is a var because we will change it at the end of each loop to test the next number
var n = 2

// Iterate while we don't have enough prime numbers in the list we want to return
while (primeNumbers.size < nbToFind) {
var prime = true

// Iterate over every number until the one we want to test to see if it can be divided
for (i in 2..n - 1) {
if (n % i == 0) {
prime = false
// It if can be divided once, no need to test other numbers
break
}
}

// Add it to the list we want to return, if we did not find a divisor
if (prime) {
primeNumbers.add(n)
}

// We try the next number
n++
}

return primeNumbers
}

fun getFirstPrimeNumbersBetter(nbToFind: Int): List<Int> {
// The list containing all the prime numbers we found
// It is a val because we will modify the array but not build a new array each time we add something to it
val primeNumbers = arrayListOf<Int>()

// Start with 2 as 0 and 1 are not prime numbers
// n is a var because we will change it at the end of each loop to test the next number
var n = 2

// Iterate while we don't have enough prime numbers in the list we want to return
while (primeNumbers.size < nbToFind) {
// Add it to the list we want to return, if we did not find a divisor
if (isPrime(n, primeNumbers)) {
primeNumbers.add(n)
}

// We try the next number
n++
}

return primeNumbers
}

private fun isPrime(n: Int, primeNumbers: List<Int>): Boolean {
for (i in primeNumbers) {
if (canBeDivided(n, i)) {
// It if can be divided once, no need to test other numbers
return false
}
}
return true
}

private fun canBeDivided(n: Int, i: Int) = n % i == 0
}
110 changes: 110 additions & 0 deletions tp2/src/test/kotlin/fmt/kotlin/fundamentals/Tp2Test.kt
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,116 @@ class Tp2Test {
)
)
}

@Test
fun `should return 100 first prime numbers better`() {
val numbers = tp2.getFirstPrimeNumbersBetter(100)

expectThat(numbers).isEqualTo(
listOf(
2,
3,
5,
7,
11,
13,
17,
19,
23,
29,
31,
37,
41,
43,
47,
53,
59,
61,
67,
71,
73,
79,
83,
89,
97,
101,
103,
107,
109,
113,
127,
131,
137,
139,
149,
151,
157,
163,
167,
173,
179,
181,
191,
193,
197,
199,
211,
223,
227,
229,
233,
239,
241,
251,
257,
263,
269,
271,
277,
281,
283,
293,
307,
311,
313,
317,
331,
337,
347,
349,
353,
359,
367,
373,
379,
383,
389,
397,
401,
409,
419,
421,
431,
433,
439,
443,
449,
457,
461,
463,
467,
479,
487,
491,
499,
503,
509,
521,
523,
541
)
)
}
}

}

0 comments on commit cc86582

Please sign in to comment.