How to resolve the algorithm Totient function step by step in the Miranda programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Totient function step by step in the Miranda 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 Miranda programming language

Source code in the miranda programming language

main :: [sys_message]
main = [Stdout (lay (map showline [1..25])),
        Stdout (lay (map countprimes (25:map (10^) [2..5])))]

countprimes :: num->[char]
countprimes n = "There are " ++ show amount ++ " primes up to " ++ show n
                where amount = #filter prime [2..n]

showline :: num->[char]
showline n = "phi(" ++ show n ++ ") = " ++ show (totient n) ++ ", " ++ kind
             where kind = "prime", if prime n
                        = "composite", otherwise

prime :: num->bool
prime n = totient n = n - 1

totient :: num->num
totient n = loop n n (2:[3, 5..])
            where loop tot n (d:ds)
                   = tot, if n<=1
                   = tot - tot div n, if d*d > n
                   = loop tot n ds, if n mod d ~= 0
                   = loop (tot - tot div d) (remfac n d) ds, otherwise
                  remfac n d = n, if n mod d ~= 0
                             = remfac (n div d) d, otherwise

  

You may also check:How to resolve the algorithm Loops/Continue step by step in the Oforth programming language
You may also check:How to resolve the algorithm Playing cards step by step in the K programming language
You may also check:How to resolve the algorithm Keyboard macros step by step in the Scala programming language
You may also check:How to resolve the algorithm Inheritance/Single step by step in the FreeBASIC programming language
You may also check:How to resolve the algorithm Empty program step by step in the SNOBOL4 programming language