From 828b649e2f8b36b3703574b9423a8eff2212b953 Mon Sep 17 00:00:00 2001 From: ctruchi Date: Fri, 7 Feb 2025 08:28:27 +0400 Subject: [PATCH] tp2 solution --- .../kotlin/fmt/kotlin/fundamentals/Tp2.kt | 29 ++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/tp2/src/main/kotlin/fmt/kotlin/fundamentals/Tp2.kt b/tp2/src/main/kotlin/fmt/kotlin/fundamentals/Tp2.kt index c3e7993..76bcc95 100644 --- a/tp2/src/main/kotlin/fmt/kotlin/fundamentals/Tp2.kt +++ b/tp2/src/main/kotlin/fmt/kotlin/fundamentals/Tp2.kt @@ -3,6 +3,33 @@ package fmt.kotlin.fundamentals class Tp2 { fun getFirstPrimeNumbers(nbToFind: Int): List { - return arrayListOf() + val primeNumbers = arrayListOf() + + // 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 } } \ No newline at end of file