How to resolve the algorithm Super-Poulet numbers step by step in the Nim programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Super-Poulet numbers step by step in the Nim programming language
Table of Contents
Problem Statement
A super-Poulet number is a Poulet number (or Fermat pseudoprime to base 2) whose every divisor d evenly divides 2d − 2.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Super-Poulet numbers step by step in the Nim programming language
Source code in the nim programming language
import std/[strformat, strutils]
proc powMod*(a, n, m: int): int =
## Return "a^n mod m".
var a = a mod m
var n = n
if a > 0:
result = 1
while n > 0:
if (n and 1) != 0:
result = (result * a) mod m
n = n shr 1
a = (a * a) mod m
func isPrime(n: Natural): bool =
## Return true if "n" is prime.
if n < 2: return false
if (n and 1) == 0: return n == 2
if n mod 3 == 0: return n == 3
var k = 5
var delta = 2
while k * k <= n:
if n mod k == 0: return false
inc k, delta
delta = 6 - delta
result = true
iterator divisors(n: Positive): int =
## Yield the divisors of "n", except 1.
yield n
var d = 2
while d * d <= n:
if n mod d == 0:
let q = n div d
yield d
if q != d:
yield q
inc d
func isSuperPouletNumber(n: int): bool =
## Return true is "x" is a Fermat pseudoprime to base "a".
if n.isPrime or powMod(2, n - 1, n) != 1: return false
for d in n.divisors:
if powMod(2, d, d) != 2:
return false
result = true
var n = 2
var count = 0
while true:
if n.isSuperPouletNumber:
inc count
if count <= 20:
stdout.write &"{n:5}"
stdout.write if count mod 5 == 0: '\n' else: ' '
if count == 20: echo()
elif n > 1_000_000:
echo &"First super-Poulet number greater than one million is {insertSep($n)} at index {count}."
break
inc n
You may also check:How to resolve the algorithm Calendar - for REAL programmers step by step in the J programming language
You may also check:How to resolve the algorithm Hello world/Graphical step by step in the AutoIt programming language
You may also check:How to resolve the algorithm Brownian tree step by step in the Sidef programming language
You may also check:How to resolve the algorithm Introspection step by step in the TI-89 BASIC programming language
You may also check:How to resolve the algorithm Leap year step by step in the zkl programming language