How to resolve the algorithm Totient function step by step in the Nim programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Totient function step by step in the Nim programming language
Table of Contents
Problem Statement
The totient function is also known as:
The totient function:
If the totient number (for N) is one less than N, then N is prime.
Create a totient function and: Show all output here.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Totient function step by step in the Nim programming language
Source code in the nim programming language
import strformat
func totient(n: int): int =
var tot = n
var nn = n
var i = 2
while i * i <= nn:
if nn mod i == 0:
while nn mod i == 0:
nn = nn div i
dec tot, tot div i
if i == 2:
i = 1
inc i, 2
if nn > 1:
dec tot, tot div nn
tot
echo " n φ prime"
echo "---------------"
var count = 0
for n in 1..25:
let tot = totient(n)
let isPrime = n - 1 == tot
if isPrime:
inc count
echo fmt"{n:2} {tot:2} {isPrime}"
echo ""
echo fmt"Number of primes up to {25:>6} = {count:>4}"
for n in 26..100_000:
let tot = totient(n)
if tot == n - 1:
inc count
if n == 100 or n == 1000 or n mod 10_000 == 0:
echo fmt"Number of primes up to {n:>6} = {count:>4}"
You may also check:How to resolve the algorithm Brace expansion step by step in the Tailspin programming language
You may also check:How to resolve the algorithm Sorting algorithms/Patience sort step by step in the ATS programming language
You may also check:How to resolve the algorithm Integer sequence step by step in the PicoLisp programming language
You may also check:How to resolve the algorithm 99 bottles of beer step by step in the Ecstasy programming language
You may also check:How to resolve the algorithm Primes: n*2^m+1 step by step in the Sidef programming language