How to resolve the algorithm Attractive numbers step by step in the AutoHotkey programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Attractive numbers step by step in the AutoHotkey programming language

Table of Contents

Problem Statement

A number is an   attractive number   if the number of its prime factors (whether distinct or not) is also prime.

The number   20,   whose prime decomposition is   2 × 2 × 5,   is an   attractive number   because the number of its prime factors   (3)   is also prime.

Show sequence items up to   120.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Attractive numbers step by step in the AutoHotkey programming language

Source code in the autohotkey programming language

AttractiveNumbers(n){
    c := prime_numbers(n).count()
    if c = 1
        return
    return isPrime(c)
}
isPrime(n){
    return (prime_numbers(n).count() = 1)
}
prime_numbers(n) {
    if (n <= 3)
        return [n]
    ans := []
    done := false
    while !done 
    {
        if !Mod(n,2){
            ans.push(2)
            n /= 2
            continue
        }
        if !Mod(n,3) {
            ans.push(3)
            n /= 3
            continue
        }
        if (n = 1)
            return ans
 
        sr := sqrt(n)
        done := true
        ; try to divide the checked number by all numbers till its square root.
        i := 6
        while (i <= sr+6){
            if !Mod(n, i-1) { ; is n divisible by i-1?
                ans.push(i-1)
                n /= i-1
                done := false
                break
            }
            if !Mod(n, i+1) { ; is n divisible by i+1?
                ans.push(i+1)
                n /= i+1
                done := false
                break
            }
            i += 6
        }
    }
    ans.push(n)
    return ans
}


c:= 0
loop
{
    if AttractiveNumbers(A_Index)
        c++, result .= SubStr("  " A_Index, -2) . (Mod(c, 20) ? " " : "`n")
    if A_Index = 120
        break
}
MsgBox, 262144, ,% result
return


  

You may also check:How to resolve the algorithm Count in octal step by step in the Run BASIC programming language
You may also check:How to resolve the algorithm Semiprime step by step in the Kotlin programming language
You may also check:How to resolve the algorithm 100 doors step by step in the bc programming language
You may also check:How to resolve the algorithm 100 doors step by step in the Ol programming language
You may also check:How to resolve the algorithm Palindrome dates step by step in the FreeBASIC programming language