How to resolve the algorithm Casting out nines step by step in the Ring programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Casting out nines step by step in the Ring programming language

Table of Contents

Problem Statement

Write a procedure (say

c o 9

( x )

{\displaystyle {\mathit {co9}}(x)}

) which implements Casting Out Nines as described by returning the checksum for

x

{\displaystyle x}

. Demonstrate the procedure using the examples given there, or others you may consider lucky. Note that this function does nothing more than calculate the least positive residue, modulo 9. Many of the solutions omit Part 1 for this reason. Many languages have a modulo operator, of which this is a trivial application. With that understanding, solutions to Part 1, if given, are encouraged to follow the naive pencil-and-paper or mental arithmetic of repeated digit addition understood to be "casting out nines", or some approach other than just reducing modulo 9 using a built-in operator. Solutions for part 2 and 3 are not required to make use of the function presented in part 1. Notwithstanding past Intel microcode errors, checking computer calculations like this would not be sensible. To find a computer use for your procedure: Demonstrate that your procedure can be used to generate or filter a range of numbers with the property

c o 9

( k )

c o 9

(

k

2

)

{\displaystyle {\mathit {co9}}(k)={\mathit {co9}}(k^{2})}

and show that this subset is a small proportion of the range and contains all the Kaprekar in the range. Considering this MathWorld page, produce a efficient algorithm based on the more mathematical treatment of Casting Out Nines, and realizing: Demonstrate your algorithm by generating or filtering a range of numbers with the property

k % (

B a s e

− 1 )

(

k

2

) % (

B a s e

− 1 )

{\displaystyle k%({\mathit {Base}}-1)==(k^{2})%({\mathit {Base}}-1)}

and show that this subset is a small proportion of the range and contains all the Kaprekar in the range.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Casting out nines step by step in the Ring programming language

Source code in the ring programming language

# Project : Casting out nines

co9(1, 10, 99, [1,9,45,55,99])
co9(1, 10, 1000, [1,9,45,55,99,297,703,999])

func co9(start,base,lim,kaprekars)
c1=0
c2=0
s = []
for k = start to lim
     c1 = c1 + 1
      if k % (base-1) = (k*k) % (base-1) 
         c2 = c2 + 1
         add(s,k)
      ok
next
msg = "Valid subset" + nl
for i = 1 to len(kaprekars) 
     if not find(s,kaprekars[i])
       msg = "***Invalid***" + nl
       exit
     ok
next
showarray(s)
see "Kaprekar numbers:" + nl
showarray(kaprekars)
see msg
see "Trying " + c2 + " numbers instead of " + c1 + " saves " + (100-(c2/c1)*100) + "%" + nl + nl

func showarray(vect)
        see "{"
        svect = ""
        for n = 1 to len(vect)
              svect = svect + vect[n] + ", "
        next
        svect = left(svect, len(svect) - 2)
        see svect + "}" + nl

  

You may also check:How to resolve the algorithm Pell's equation step by step in the REXX programming language
You may also check:How to resolve the algorithm Reverse a string step by step in the Ursala programming language
You may also check:How to resolve the algorithm Guess the number/With feedback step by step in the ooRexx programming language
You may also check:How to resolve the algorithm Active object step by step in the Tcl programming language
You may also check:How to resolve the algorithm Van Eck sequence step by step in the MACRO-11 programming language