How to resolve the algorithm Carmichael 3 strong pseudoprimes step by step in the Kotlin programming language

Published on 22 June 2024 08:30 PM

How to resolve the algorithm Carmichael 3 strong pseudoprimes step by step in the Kotlin programming language

Table of Contents

Problem Statement

A lot of composite numbers can be separated from primes by Fermat's Little Theorem, but there are some that completely confound it. The   Miller Rabin Test   uses a combination of Fermat's Little Theorem and Chinese Division Theorem to overcome this. The purpose of this task is to investigate such numbers using a method based on   Carmichael numbers,   as suggested in   Notes by G.J.O Jameson March 2010.

Find Carmichael numbers of the form: where   (Prime1 < Prime2 < Prime3)   for all   Prime1   up to   61. (See page 7 of   Notes by G.J.O Jameson March 2010   for solutions.)

For a given

P r i m

e

1

{\displaystyle Prime_{1}}

Chernick's Carmichael numbers

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Carmichael 3 strong pseudoprimes step by step in the Kotlin programming language

Extension Function isPrime:

This function extends the Int type by adding a method isPrime that checks if the integer is prime. It uses the following logic:

  • If the integer is 2, it's prime.
  • If the integer is less than or equal to 1 or divisible by 2, it's not prime.
  • Otherwise, it checks if any integers between 3 and the square root of the integer (excluding even numbers) divide the integer evenly. If so, it's not prime. If not, it's prime.

mod Function:

This function calculates the modulo of two integers n and m. It ensures that the result is positive by adding m to the result if it's negative.

main Function:

The main function iterates through integers from 3 to 61 and checks if each is prime. For each prime number p1, it calculates g as h3 + p1, where h3 is an integer between 2 and p1. It then iterates through integers d from 1 to g and checks the following conditions:

  • (g * (p1 - 1)) % d == 0: This checks if d is a factor of g * (p1 - 1).
  • mod(-p1 * p1, h3) == d % h3: This checks if d is a specific residue when -p1 * p1 is divided by h3.

If these conditions are met, it calculates q as 1 + (p1 - 1) * g / d and checks if it's prime. If so, it calculates r as 1 + (p1 * q / h3) and checks if it's prime. Finally, it checks if (q * r) % (p1 - 1) == 1. If all these conditions are met, it prints the values of p1, q, and r.

Source code in the kotlin programming language

fun Int.isPrime(): Boolean {
    return when {
        this == 2 -> true
        this <= 1 || this % 2 == 0 -> false
        else -> {
            val max = Math.sqrt(toDouble()).toInt()
            (3..max step 2)
                .filter { this % it == 0 }
                .forEach { return false }
            true
        }
    }
}

fun mod(n: Int, m: Int) = ((n % m) + m) % m

fun main(args: Array<String>) {
    for (p1 in 3..61) {
        if (p1.isPrime()) {
            for (h3 in 2 until p1) {
                val g = h3 + p1
                for (d in 1 until g) {
                    if ((g * (p1 - 1)) % d == 0 && mod(-p1 * p1, h3) == d % h3) {
                        val q = 1 + (p1 - 1) * g / d
                        if (q.isPrime()) {
                            val r = 1 + (p1 * q / h3)
                            if (r.isPrime() && (q * r) % (p1 - 1) == 1) {
                                println("$p1 x $q x $r")
                            }
                        }
                    }
                }
            }
        }
    }
}


  

You may also check:How to resolve the algorithm Move-to-front algorithm step by step in the jq programming language
You may also check:How to resolve the algorithm Get system command output step by step in the Lingo programming language
You may also check:How to resolve the algorithm Secure temporary file step by step in the C++ programming language
You may also check:How to resolve the algorithm String case step by step in the jq programming language
You may also check:How to resolve the algorithm Anagrams/Deranged anagrams step by step in the Ring programming language