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 828b649
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion tp2/src/main/kotlin/fmt/kotlin/fundamentals/Tp2.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,33 @@ package fmt.kotlin.fundamentals
class Tp2 {

fun getFirstPrimeNumbers(nbToFind: Int): List<Int> {
return arrayListOf<Int>()
val primeNumbers = arrayListOf<Int>()

// Start with 2 as 0 and 1 are not prime numbers
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
}
}

0 comments on commit 828b649

Please sign in to comment.