How to resolve the algorithm Inconsummate numbers in base 10 step by step in the AppleScript programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Inconsummate numbers in base 10 step by step in the AppleScript programming language

Table of Contents

Problem Statement

A consummate number is a non-negative integer that can be formed by some integer N divided by the the digital sum of N.

47 is a consummate number. On the other hand, there are integers that can not be formed by a ratio of any integer over its digital sum. These numbers are known as inconsummate numbers.

62 is an inconsummate number. There is no integer ratio of an integer to its digital sum that will result in 62. The base that a number is expressed in will affect whether it is inconsummate or not. This task will be restricted to base 10.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Inconsummate numbers in base 10 step by step in the AppleScript programming language

Source code in the applescript programming language

on inconsummateNumbers(numberRequired)
    set limit to numberRequired * 1000 -- Seems to be enough.
    script o
        property lst : makeList(limit, true)
    end script
    
    repeat with n from 11 to limit -- 1 to 10 obviously can't be inconsummate.
        set digitSum to n mod 10
        set temp to n div 10
        repeat while (temp > 9)
            set digitSum to digitSum + temp mod 10
            set temp to temp div 10
        end repeat
        set digitSum to digitSum + temp
        
        tell (n div digitSum) to if (it = n / digitSum) then set o's lst's item it to false
    end repeat
    
    set i to 0
    repeat with n from 11 to limit
        if (o's lst's item n) then
            set i to i + 1
            set o's lst's item i to n
            if (i = numberRequired) then return o's lst's items 1 thru i
        end if
    end repeat
    
    return {}
end inconsummateNumbers

on makeList(limit, filler)
    if (limit < 1) then return {}
    script o
        property lst : {filler}
    end script
    
    set counter to 1
    repeat until (counter + counter > limit)
        set o's lst to o's lst & o's lst
        set counter to counter + counter
    end repeat
    if (counter < limit) then set o's lst to o's lst & o's lst's items 1 thru (limit - counter)
    return o's lst
end makeList

on join(lst, delim)
    set astid to AppleScript's text item delimiters
    set AppleScript's text item delimiters to delim
    set txt to lst as text
    set AppleScript's text item delimiters to astid
    return txt
end join

on task()
    script o
        property inconsummates : inconsummateNumbers(100000)
    end script
    set output to {"First 50 inconsummate numbers:"}
    set row to {}
    repeat with i from 1 to 50
        set row's end to text -5 thru -1 of ("   " & o's inconsummates's item i)
        if (i mod 10 = 0) then
            set output's end to join(row, "")
            set row to {}
        end if
    end repeat
    set output's end to linefeed & "1,000th inconsummate number: " & o's inconsummates's item 1000
    set output's end to "10,000th inconsummate number: " & o's inconsummates's item 10000
    set output's end to "100,000th inconsummate number: " & o's inconsummates's end
    return join(output, linefeed)
end task

task()


"First 50 inconsummate numbers:
   62   63   65   75   84   95  161  173  195  216
  261  266  272  276  326  371  372  377  381  383
  386  387  395  411  416  422  426  431  432  438
  441  443  461  466  471  476  482  483  486  488
  491  492  493  494  497  498  516  521  522  527

1,000th inconsummate number: 6996
10,000th inconsummate number: 59853
100,000th inconsummate number: 536081"


  

You may also check:How to resolve the algorithm FizzBuzz step by step in the IS-BASIC programming language
You may also check:How to resolve the algorithm IBAN step by step in the Common Lisp programming language
You may also check:How to resolve the algorithm Truncatable primes step by step in the Haskell programming language
You may also check:How to resolve the algorithm Find the last Sunday of each month step by step in the JavaScript programming language
You may also check:How to resolve the algorithm Loops/For with a specified step step by step in the AutoHotkey programming language