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

Published on 12 May 2024 09:40 PM

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

Table of Contents

Problem Statement

An Achilles number is a number that is powerful but imperfect. Named after Achilles, a hero of the Trojan war, who was also powerful but imperfect.

A positive integer n is a powerful number if, for every prime factor p of n, p2 is also a divisor. In other words, every prime factor appears at least squared in the factorization. All Achilles numbers are powerful. However, not all powerful numbers are Achilles numbers: only those that cannot be represented as mk, where m and k are positive integers greater than 1.

A strong Achilles number is an Achilles number whose Euler totient (𝜑) is also an Achilles number.

108 is a powerful number. Its prime factorization is 22 × 33, and thus its prime factors are 2 and 3. Both 22 = 4 and 32 = 9 are divisors of 108. However, 108 cannot be represented as mk, where m and k are positive integers greater than 1, so 108 is an Achilles number. 360 is not an Achilles number because it is not powerful. One of its prime factors is 5 but 360 is not divisible by 52 = 25. Finally, 784 is not an Achilles number. It is a powerful number, because not only are 2 and 7 its only prime factors, but also 22 = 4 and 72 = 49 are divisors of it. Nonetheless, it is a perfect power; its square root is an even integer, so it is not an Achilles number.

500 = 22 × 53 is a strong Achilles number as its Euler totient, 𝜑(500), is 200 = 23 × 52 which is also an Achilles number.

Let's start with the solution:

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

Source code in the wren programming language

import "./math" for Int
import "./seq" for Lst
import "./fmt" for Fmt

var maxDigits = 8
var limit = 10.pow(maxDigits)
var c = Int.primeSieve(limit-1, false)

var isPerfectPower = Fn.new { |n|
    if (n == 1) return true
    var x = 2
    while (x * x <= n) {
        var y = 2
        var p = x.pow(y)
        while (p > 0 && p <= n) {
            if (p == n) return true
            y = y + 1
            p = x.pow(y)
        }
        x = x + 1
    }
    return false
}

var isPowerful = Fn.new { |n|
    while (n % 2 == 0) {
        var p = 0
        while (n % 2 == 0) {
            n = (n/2).floor
            p = p + 1
        }
        if (p == 1) return false
    }
    var f = 3
    while (f * f <= n) {
        var p = 0
        while (n % f == 0) {
            n = (n/f).floor
            p = p + 1
        }
        if (p == 1) return false
        f = f + 2
    }
    return n == 1
}

var isAchilles = Fn.new { |n| c[n] && isPowerful.call(n) && !isPerfectPower.call(n) }

var isStrongAchilles = Fn.new { |n|
    if (!isAchilles.call(n)) return false
    var tot = Int.totient(n)
    return isAchilles.call(tot)
}

System.print("First 50 Achilles numbers:")
var achilles = []
var count = 0
var n = 2
while (count < 50) {
    if (isAchilles.call(n)) {
        achilles.add(n)
        count = count + 1
    }
    n = n + 1
}
Fmt.tprint("$4d", achilles, 10)

System.print("\nFirst 30 strong Achilles numbers:")
var strongAchilles = []
count = 0
n = achilles[0]
while (count < 30) {
    if (isStrongAchilles.call(n)) {
        strongAchilles.add(n)
        count = count + 1
    }
    n = n + 1
}
Fmt.tprint("$5d", strongAchilles, 10)

System.print("\nNumber of Achilles numbers with:")
var pow = 10
for (i in 2..maxDigits) {
    var count = 0
    for (j in pow..pow*10-1) {
        if (isAchilles.call(j)) count = count + 1
    }
    System.print("%(i) digits: %(count)")
    pow = pow * 10
}


import "./set" for Set
import "./seq" for Lst
import "./math" for Int
import "./fmt" for Fmt

var pps = Set.new()

var getPerfectPowers = Fn.new { |maxExp|
    var upper = 10.pow(maxExp)
    for (i in 2..upper.sqrt.floor) {
        var p = i
        while ((p = p * i) < upper) pps.add(p)
    }
}

var getAchilles = Fn.new { |minExp, maxExp|
    var lower = 10.pow(minExp)
    var upper = 10.pow(maxExp)
    var achilles = Set.new() // avoids duplicates
    for (b in 1..upper.cbrt.floor) {
        var b3 = b * b * b
        for (a in 1..upper.sqrt.floor) {
            var p = b3 * a * a
            if (p >= upper) break
            if (p >= lower) {
                if (!pps.contains(p)) achilles.add(p)
            }
        }
    }
    return achilles
}

var maxDigits = 15
getPerfectPowers.call(maxDigits)

var achillesSet = getAchilles.call(1, 5) // enough for first 2 parts
var achilles = achillesSet.toList
achilles.sort()

System.print("First 50 Achilles numbers:")
Fmt.tprint("$4d", achilles.take(50), 10)

System.print("\nFirst 30 strong Achilles numbers:")
var strongAchilles = []
var count = 0
var n = 0
while (count < 30) {
    var tot = Int.totient(achilles[n])
    if (achillesSet.contains(tot)) {
        strongAchilles.add(achilles[n])
        count = count + 1
    }
    n = n + 1
}
Fmt.tprint("$5d", strongAchilles, 10)

System.print("\nNumber of Achilles numbers with:")
for (d in 2..maxDigits) {
    var ac = getAchilles.call(d-1, d).count
    Fmt.print("$2d digits: $d", d, ac)
}


  

You may also check:How to resolve the algorithm 24 game/Solve step by step in the Nim programming language
You may also check:How to resolve the algorithm Count the coins step by step in the EchoLisp programming language
You may also check:How to resolve the algorithm Averages/Root mean square step by step in the Euphoria programming language
You may also check:How to resolve the algorithm Digital root step by step in the Crystal programming language
You may also check:How to resolve the algorithm 24 game step by step in the Ada programming language