How to resolve the algorithm Abundant, deficient and perfect number classifications step by step in the Wren programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Abundant, deficient and perfect number classifications step by step in the Wren programming language

Table of Contents

Problem Statement

These define three classifications of positive integers based on their   proper divisors. Let   P(n)   be the sum of the proper divisors of   n   where the proper divisors are all positive divisors of   n   other than   n   itself.

6   has proper divisors of   1,   2,   and   3. 1 + 2 + 3 = 6,   so   6   is classed as a perfect number.

Calculate how many of the integers   1   to   20,000   (inclusive) are in each of the three classes. Show the results here.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Abundant, deficient and perfect number classifications step by step in the Wren programming language

Source code in the wren programming language

import "./math" for Int, Nums

var d = 0
var a = 0
var p = 0
for (i in 1..20000) {
    var j = Nums.sum(Int.properDivisors(i))
    if (j < i) {
        d = d + 1
    } else if (j == i) {
        p = p + 1
    } else {
        a = a + 1
    }
}
System.print("There are %(d) deficient numbers between 1 and 20000")
System.print("There are %(a) abundant numbers  between 1 and 20000")
System.print("There are %(p) perfect numbers between 1 and 20000")


var maxNumber      = 20000
var abundantCount  = 0
var deficientCount = 0
var perfectCount   = 0

var pds = []
pds.add(0) // element 0
pds.add(0) // element 1
for (i in 2..maxNumber) {
    pds.add(1)
}
for (i in 2..maxNumber) {
    var j = i + i
    while (j <= maxNumber) {
        pds[j] = pds[j] + i
        j = j + i
    }
}
for (n in 1..maxNumber) {
    var pdSum = pds[n]
    if (pdSum < n) {
        deficientCount = deficientCount + 1
    } else if (pdSum == n) {
        perfectCount = perfectCount + 1
    } else { // pdSum >  n
        abundantCount = abundantCount + 1
    }
}

System.print("Abundant : %(abundantCount)")
System.print("Deficient: %(deficientCount)")
System.print("Perfect  : %(perfectCount)")


  

You may also check:How to resolve the algorithm IBAN step by step in the Lobster programming language
You may also check:How to resolve the algorithm Plot coordinate pairs step by step in the Icon and Unicon programming language
You may also check:How to resolve the algorithm Cumulative standard deviation step by step in the EasyLang programming language
You may also check:How to resolve the algorithm Here document step by step in the Applesoft BASIC programming language
You may also check:How to resolve the algorithm Semordnilap step by step in the Clojure programming language