1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
|
class NombrePremier{
fun primeNumber() {
val maxPrime: Int = 46_348
var isPrime = mutableListOf<Boolean>()
// var isPrime = mutableListOf<Boolean>(false, false)
var primes = mutableListOf<Int>()
fun boucleTrue() {
try {
for (index in 0..maxPrime) {
isPrime.add(true)
}
isPrime[0] = false
isPrime[1] = false
println("✅ Successfully... Fun boucleTrue")
println("✅ Size of isPrime: ${isPrime.size}")
}
catch(ex: Exception) {
println("🛑 Exception message from fun boucleTrue(): 🛑 $ex.message")
}
}
boucleTrue()
fun boucleFalse() {
try {
for (i in 2..maxPrime) {
if (isPrime[i]) { /* the same: replace this (isPrime[i] == true) */
var j = i * i
while (j <= maxPrime) {
//println("i: $i j: $j")
isPrime[j] = false
//println("i2: $i & j2: $j")
j += i
//println("i3: $i & j3: $j")
}
//println("i avant add i: $i")
primes.add(i)
//println("i après add i: $i")
}
}
fun infos() {
println("✅ Successfully... Fun boucleFalse")
println("✅ All Primes Numbers: $primes")
println("✅ Numbers of primes number: ${primes.size}")
println("✅ Last Prime Number: ${primes.last()}")
}
infos()
}
catch(ex: Exception) {
println("🛑 Exception message from fun boucleFalse(): 🛑 $ex.message")
}
}
boucleFalse()
fun writeToFile() {
try {
// using java class java.io.PrintWriter
val writer = PrintWriter("files/JCfile.txt")
writer.append(primes.toString())
writer.println()
writer.close()
// using java class java.io.File
val fileName = "files/data.txt"
var file = File(fileName)
// create a new file
val isNewFileCreated: Boolean = file.createNewFile()
if (isNewFileCreated) {
println("✅ $fileName is created successfully.")
} else {
println("✅ $fileName already exists.")
}
file.appendText(primes.toString())
}
catch (ex: Exception) {
println("🛑 Exception message from fun writeToFile(): 🛑 $ex.message")
}
}
writeToFile()
}
} |
Partager