How to resolve the algorithm Weird numbers step by step in the Kotlin programming language

Published on 22 June 2024 08:30 PM

How to resolve the algorithm Weird numbers step by step in the Kotlin programming language

Table of Contents

Problem Statement

In number theory, a weird number is a natural number that is abundant but not semiperfect (and therefore not perfect either). In other words, the sum of the proper divisors of the number (divisors including 1 but not itself) is greater than the number itself (the number is abundant), but no subset of those divisors sums to the number itself (the number is not semiperfect). For example:

Find and display, here on this page, the first 25 weird numbers.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Weird numbers step by step in the Kotlin programming language

The provided Kotlin code defines functions to calculate the divisors of a number, determine if a number is abundant, determine if a number is semiperfect, perform a modified Sieve of Eratosthenes, and find the first 25 weird numbers. Here's a breakdown:

  1. divisors Function:

    • Takes an integer n as input.
    • Initializes two mutable lists: divs to store found divisors and divs2 to store the complement of divisors.
    • Iterates from 2 to the square root of n to find divisors.
    • If n is divisible by i, both i and n/i are added to the divisors lists.
    • After the loop, divs2 is appended with the reverse of divs.
    • Returns the combined list of divisors.
  2. abundant Function:

    • Takes an integer n and a list of its divisors divs.
    • Checks if the sum of the divisors is greater than n.
    • If so, returns true, indicating that the number is abundant. Otherwise, returns false.
  3. semiperfect Function:

    • Takes an integer n and a list of its divisors divs.
    • Recursively determines if n can be expressed as the sum of distinct divisors.
    • The function explores different combinations and returns true if n is semiperfect. Otherwise, it returns false.
  4. sieve Function:

    • Takes an integer limit as input.
    • Initializes a Boolean array w of size limit to track the status of numbers.
    • Iterates through even numbers from 2 to limit.
    • For each number i, it calculates its divisors and checks if it's abundant.
    • If i is abundant, it marks all its multiples as abundant and semiperfect in the w array.
    • Returns the Boolean array w.
  5. main Function:

    • Calls the sieve function with a limit of 17000 and stores the result in the w array.
    • Initializes a counter count to 0 and a maximum count max to 25.
    • Prints a message about finding the first 25 weird numbers.
    • Iterates through even numbers n starting from 2.
    • For each n, it checks if it's not marked as abundant or semiperfect in the w array.
    • If n is weird, it prints it and increments the counter.
    • Once 25 weird numbers are found, it prints a newline.

The key concept of the code is to determine if a number is weird. In mathematics, a weird number is an even positive integer that is not abundant and not semiperfect. The code uses a slightly modified Sieve of Eratosthenes to mark all abundant and semiperfect numbers up to a limit, and then it iterates through even numbers to find the first 25 weird numbers.

Source code in the kotlin programming language

// Version 1.3.21

fun divisors(n: Int): List<Int> {
    val divs = mutableListOf(1)
    val divs2 = mutableListOf<Int>()
    var i = 2
    while (i * i <= n) {
        if (n % i == 0) {
            val j = n / i
            divs.add(i)
            if (i != j) divs2.add(j)
        }
        i++
    }
    divs2.addAll(divs.asReversed())
    return divs2
}

fun abundant(n: Int, divs: List<Int>) = divs.sum() > n

fun semiperfect(n: Int, divs: List<Int>): Boolean {
    if (divs.size > 0) {
        val h = divs[0]
        val t = divs.subList(1, divs.size)
        if (n < h) {
            return semiperfect(n, t)
        } else {
            return n == h || semiperfect(n-h, t) || semiperfect(n, t)
        }
    } else {
        return false
    }
}

fun sieve(limit: Int): BooleanArray {
    // false denotes abundant and not semi-perfect.
    // Only interested in even numbers >= 2
    val w = BooleanArray(limit)
    for (i in 2 until limit step 2) {
        if (w[i]) continue
        val divs = divisors(i)
        if (!abundant(i, divs)) {
            w[i] = true
        } else if (semiperfect(i, divs)) {
            for (j in i until limit step i) w[j] = true
        }
    }
    return w
}

fun main() {
    val w = sieve(17000)
    var count = 0
    val max = 25
    println("The first 25 weird numbers are:")
    var n = 2
    while (count < max) {
        if (!w[n]) {
            print("$n ")
            count++
        }
        n += 2
    }
    println()
}


  

You may also check:How to resolve the algorithm Averages/Mean angle step by step in the 11l programming language
You may also check:How to resolve the algorithm Additive primes step by step in the C programming language
You may also check:How to resolve the algorithm Optional parameters step by step in the R programming language
You may also check:How to resolve the algorithm Vector products step by step in the Mathematica/Wolfram Language programming language
You may also check:How to resolve the algorithm Matrix multiplication step by step in the Klong programming language