How to resolve the algorithm Twin primes step by step in the Kotlin programming language

Published on 22 June 2024 08:30 PM

How to resolve the algorithm Twin primes step by step in the Kotlin programming language

Table of Contents

Problem Statement

Twin primes are pairs of natural numbers   (P1  and  P2)   that satisfy the following:

Write a program that displays the number of pairs of twin primes that can be found under a user-specified number (P1 < user-specified number & P2 < user-specified number).

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Twin primes step by step in the Kotlin programming language

Kotlin Code Explanation:

This Kotlin code is designed to find and count pairs of twin prime numbers (prime numbers that differ by 2) within a given range. Here's a detailed explanation:

Main Function:

  • The main() function is the entry point of the program.
  • It starts by prompting the user to enter the maximum size (upper bound) of the search range.
  • It then initializes the search range:
    • max stores the user-provided upper bound.
    • counter keeps track of the number of twin prime pairs found.
    • x represents the current number being checked for primality.

Prime Number Checking:

  • The findPrime() function is used to check if a given number x is prime.
  • It uses a simple primality test: it checks if x is divisible by any number in the range [2, sqrt(x)].
  • If no divisors are found, x is considered prime; otherwise, it's not.

Twin Prime Pair Search:

  • The while loop iterates through numbers starting from 3 (x) up to the specified max value.
  • For each x, it calculates the upper bound for the primality test using x.sqrt().add(BigInteger.ONE) (to prevent overflow when squaring the number).
  • It checks two conditions:
    • If x + 2 is within the search range (x.add(BigInteger.TWO) <= max), it's a potential candidate as the second prime in the pair.
    • It checks if both x and x + 2 are prime by calling the findPrime() function.
  • If both conditions are true, it increments the counter to track the found twin prime pair.
  • x is then incremented by 1 to check the next potential pair.

Output:

  • After the loop completes, the program prints the total number of twin prime pairs found within the specified search range.

Source code in the kotlin programming language

import java.math.BigInteger
import java.util.*

fun main() {
    val input = Scanner(System.`in`)
    println("Search Size: ")
    val max = input.nextBigInteger()
    var counter = 0
    var x = BigInteger("3")
    while (x <= max) {
        val sqrtNum = x.sqrt().add(BigInteger.ONE)
        if (x.add(BigInteger.TWO) <= max) {
            counter += if (findPrime(
                    x.add(BigInteger.TWO),
                    x.add(BigInteger.TWO).sqrt().add(BigInteger.ONE)
                ) && findPrime(x, sqrtNum)
            ) 1 else 0
        }
        x = x.add(BigInteger.ONE)
    }
    println("$counter twin prime pairs.")
}

fun findPrime(x: BigInteger, sqrtNum: BigInteger?): Boolean {
    var divisor = BigInteger.TWO
    while (divisor <= sqrtNum) {
        if (x.remainder(divisor).compareTo(BigInteger.ZERO) == 0) {
            return false
        }
        divisor = divisor.add(BigInteger.ONE)
    }
    return true
}


  

You may also check:How to resolve the algorithm Substring/Top and tail step by step in the Locomotive Basic programming language
You may also check:How to resolve the algorithm Numerical integration step by step in the SequenceL programming language
You may also check:How to resolve the algorithm File input/output step by step in the LabVIEW programming language
You may also check:How to resolve the algorithm Execute SNUSP step by step in the AutoHotkey programming language
You may also check:How to resolve the algorithm Pascal matrix generation step by step in the PARI/GP programming language