How to resolve the algorithm Perfect totient numbers step by step in the Wren programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Perfect totient numbers step by step in the Wren programming language

Table of Contents

Problem Statement

Generate and show here, the first twenty Perfect totient numbers.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Perfect totient numbers step by step in the Wren programming language

Source code in the wren programming language

var totient = Fn.new { |n|
    var tot = n
    var i = 2
    while (i*i <= n) {
        if (n%i == 0) {
            while(n%i == 0) n = (n/i).floor
            tot = tot - (tot/i).floor
        }
        if (i == 2) i = 1
        i = i + 2
    }
    if (n > 1) tot = tot - (tot/n).floor
    return tot
}

var perfect = []
var n = 1
while (perfect.count < 20) {
    var tot = n
    var sum = 0
    while (tot != 1) {
        tot = totient.call(tot)
        sum = sum + tot
    }
    if (sum == n) perfect.add(n)
    n = n + 2
}
System.print("The first 20 perfect totient numbers are:")
System.print(perfect)

  

You may also check:How to resolve the algorithm Order two numerical lists step by step in the Joy programming language
You may also check:How to resolve the algorithm Conway's Game of Life step by step in the R programming language
You may also check:How to resolve the algorithm Halt and catch fire step by step in the F# programming language
You may also check:How to resolve the algorithm FASTA format step by step in the FreeBASIC programming language
You may also check:How to resolve the algorithm Playing cards step by step in the Go programming language