How to resolve the algorithm Inconsummate numbers in base 10 step by step in the Action! 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 Action! 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 Action! programming language

Source code in the action! programming language

;;; find some incomsummate numbers: integers that cannot be expressed as
;;;      an integer divided by the sum of its digits

PROC Main()

  CARD i, tn, hn, th, tt, sumD, d, n, dRatio, count, maxSum, v, maxNumber

  ; table of numbers that can be formed by n / digit sum n
  DEFINE MAX_C = "999"
  BYTE ARRAY consummate(MAX_C+1)
  FOR i = 0 TO MAX_C DO
    consummate( i ) = 0
  OD

  ; calculate the maximum number we must consider
  v = MAX_C / 10;
  maxSum = 9;
  WHILE v > 0 DO
    maxSum ==+ 9
    v      ==/ 10
  OD
  maxNumber = maxSum * MAX_C

  ; construct the digit sums of the numbers up to maxNumber
  ; and find the consumate numbers, we start the loop from 10 to avoid
  ; having to deal with 0-9
  consummate( 1 ) = 1
  tn = 1 hn = 0 th = 0 tt = 0
  FOR n = 10 TO maxNumber STEP 10 DO
    sumD = tt + th + hn + tn
    FOR d = n TO n + 9 DO
      IF d MOD sumD = 0 THEN
        ; d is comsummate
        dRatio = d / sumD
        IF dRatio <= MAX_C THEN
          consummate( dRatio ) = 1
        FI
      FI
      sumD ==+ 1
    OD
    tn ==+ 1
    IF tn > 9 THEN
      tn = 0
      hn ==+ 1
      IF hn > 9 THEN
        hn = 0
        th ==+ 1
        IF th > 9 THEN
          th = 0
          tt ==+ 1
        FI
      FI
    FI
  OD

  count = 0
  PrintE( "The first 50 inconsummate numbers:" )
  i = 0
  WHILE i < MAX_C AND count < 50 DO
    i ==+ 1
    IF consummate( i ) = 0 THEN
      count ==+ 1
      Put(' )
      IF i <   10 THEN Put(' ) FI
      IF i <  100 THEN Put(' ) FI
      IF i < 1000 THEN Put(' ) FI
      PrintC( i )
      IF count MOD 10 = 0 THEN PutE() FI
    FI
  OD

RETURN

  

You may also check:How to resolve the algorithm Comments step by step in the Intercal programming language
You may also check:How to resolve the algorithm Show the epoch step by step in the NetRexx programming language
You may also check:How to resolve the algorithm Four bit adder step by step in the 11l programming language
You may also check:How to resolve the algorithm Harshad or Niven series step by step in the Miranda programming language
You may also check:How to resolve the algorithm Loops/Foreach step by step in the Vim Script programming language