How to resolve the algorithm Totient function step by step in the Scala programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Totient function step by step in the Scala 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 Scala programming language
Source code in the scala programming language
@tailrec
def gcd(a: Int, b: Int): Int = if(b == 0) a else gcd(b, a%b)
def totientLaz(num: Int): Int = LazyList.range(2, num).count(gcd(num, _) == 1) + 1
def totientPrd(num: Int): Int = {
@tailrec
def dTrec(f: Int, n: Int): Int = if(n%f == 0) dTrec(f, n/f) else n
@tailrec
def tTrec(ac: Int, i: Int, n: Int): Int = if(n != 1){
if(n%i == 0) tTrec(ac*(i - 1)/i, i + 1, dTrec(i, n))
else tTrec(ac, i + 1, n)
}else{
ac
}
tTrec(num, 2, num)
}
@tailrec
def scrub(f: Long, num: Long): Long = if(num%f == 0) scrub(f, num/f) else num
def totientLazPrd(num: Long): Long = LazyList.iterate((num, 2: Long, num)){case (ac, i, n) => if(n%i == 0) (ac*(i - 1)/i, i + 1, scrub(i, n)) else (ac, i + 1, n)}.find(_._3 == 1).get._1
You may also check:How to resolve the algorithm Pi step by step in the Lasso programming language
You may also check:How to resolve the algorithm Factorial base numbers indexing permutations of a collection step by step in the Go programming language
You may also check:How to resolve the algorithm Super-d numbers step by step in the Quackery programming language
You may also check:How to resolve the algorithm Fibonacci sequence step by step in the WebAssembly programming language
You may also check:How to resolve the algorithm Horner's rule for polynomial evaluation step by step in the Go programming language