How to resolve the algorithm Sequence of primes by trial division step by step in the Kotlin programming language

Published on 22 June 2024 08:30 PM

How to resolve the algorithm Sequence of primes by trial division step by step in the Kotlin programming language

Table of Contents

Problem Statement

Generate a sequence of primes by means of trial division.

Trial division is an algorithm where a candidate number is tested for being a prime by trying to divide it by other numbers. You may use primes, or any numbers of your choosing, as long as the result is indeed a sequence of primes. The sequence may be bounded (i.e. up to some limit), unbounded, starting from the start (i.e. 2) or above some given value. Organize your function as you wish, in particular, it might resemble a filtering operation, or a sieving operation. If you want to use a ready-made is_prime function, use one from the Primality by trial division page (i.e., add yours there if it isn't there already).

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Sequence of primes by trial division step by step in the Kotlin programming language

1. Introduction:

This Kotlin code determines whether a given integer n is a prime number and prints all prime numbers below 2000.

2. isPrime Function:

The isPrime function checks if the input integer n is prime. It uses a probabilistic algorithm that is fast but not guaranteed to be correct. Here's how it works:

  • If n is less than 2, it's not prime because 0 and 1 are not prime.
  • If n is even (except for 2), it's not prime.
  • If n is divisible by 3 (except for 3), it's not prime.
  • The main loop starts with d = 5 and increments by 2 and 4 in each iteration. The loop checks if n is divisible by d or d + 2. If any of these divisions result in 0, n is not prime.
  • If the loop completes without finding any divisors, n is prime.

3. main Function:

The main function is the entry point of the program. It uses a for loop to iterate through numbers from 3 to 1999 in steps of 2 (i.e., only odd numbers). For each i, it checks if it's prime using the isPrime function. If i is prime, it prints its value and increments a counter. When the counter reaches 15, it starts a new line.

4. Output:

The program outputs all prime numbers below 2000 in a multi-column format, with 15 primes printed on each line.

Source code in the kotlin programming language

// version 1.0.6

fun isPrime(n: Int): Boolean {
    if (n < 2) return false 
    if (n % 2 == 0) return n == 2
    if (n % 3 == 0) return n == 3
    var d : Int = 5
    while (d * d <= n) {
        if (n % d == 0) return false
        d += 2
        if (n % d == 0) return false
        d += 4
    }
    return true
}

fun main(args: Array<String>) {
    // print all primes below 2000 say
    var count = 1
    print("    2")
    for (i in 3..1999 step 2)
        if (isPrime(i)) {
            count++ 
            print("%5d".format(i))
            if (count % 15 == 0) println()
        }
}


  

You may also check:How to resolve the algorithm Zero to the zero power step by step in the ColdFusion programming language
You may also check:How to resolve the algorithm Additive primes step by step in the Phix programming language
You may also check:How to resolve the algorithm Spiral matrix step by step in the Ring programming language
You may also check:How to resolve the algorithm Last Friday of each month step by step in the Logo programming language
You may also check:How to resolve the algorithm Hello world/Text step by step in the CoffeeScript programming language