object Main { /** * Checks if a number is prime using optimized trial division * @param n The number to check * @return true if prime, false otherwise */ else { // Check divisors up to sqrt(n), skipping even numbers (3 to maxDivisor by 2).forall(n % _ != 0) } } /** * Generates prime numbers up to a limit using Sieve of Eratosthenes * @param limit Upper bound for prime generation * @return Sequence of primes up to limit */ else { for { i <- 2 to math.sqrt(limit).toInt j <- i * i to limit by i } } // Test individual numbers println("Prime checks:") testNumbers.foreach { n => println(f"$n%4d is prime: ${isPrime(n)}%5s") } // Generate primes up to 100 println("\nPrimes up to 100:") println(primesUpTo100.mkString(", ")) } }
Standard input is empty
Prime checks: 2 is prime: true 3 is prime: true 17 is prime: true 25 is prime: false 997 is prime: true 1000 is prime: false Primes up to 100: 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