How to resolve the algorithm Casting out nines step by step in the Python programming language
How to resolve the algorithm Casting out nines step by step in the Python 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 Python programming language
Casting Out Nines
This program implements the Casting Out Nines algorithm to find numbers for which the sum of the digits (cast out nines) is a multiple of a given base.
Key Concepts:
- Casting Out Nines: A mathematical technique that uses the remainder when a number is divided by 9 to determine the remainder when the sum of its digits is divided by 9.
- Base: The base in which the casting out is performed.
- Range: The range of numbers to be checked.
Implementation:
1. Find Numbers for the Base-1:
ran = [y for y in range(Base-1) if y%(Base-1) == (y*y)%(Base-1)]
This list ran
contains numbers that, when squared, produce the same remainder when divided by (Base-1)
as the original number.
2. Set Initial Values:
x,y = divmod(Start, Base-1)
x
and y
represent the quotient and remainder when Start
is divided by (Base-1)
.
3. Loop through Numbers in the Range:
while True:
for n in ran:
k = (Base-1)*x + n
if k < Start:
continue
if k > End:
return
yield k
- The loop generates numbers
k
within the specified range. - The numbers are generated by adding an element from
ran
to the product of(Base-1)
andx
. - The loop terminates if
k
goes beyond the given range.
4. Yielding Numbers:
yield k
The yield
keyword generates and returns the current k
value as part of an iterator.
5. Usage:
for V in CastOut(Base=16,Start=1,End=255):
print(V, end=' ')
This example demonstrates the use of the generator by printing numbers from 1 to 255 that cast out nines in base 16 (hexadecimal).
Source code in the python programming language
# Casting out Nines
#
# Nigel Galloway: June 27th., 2012,
#
def CastOut(Base=10, Start=1, End=999999):
ran = [y for y in range(Base-1) if y%(Base-1) == (y*y)%(Base-1)]
x,y = divmod(Start, Base-1)
while True:
for n in ran:
k = (Base-1)*x + n
if k < Start:
continue
if k > End:
return
yield k
x += 1
for V in CastOut(Base=16,Start=1,End=255):
print(V, end=' ')
You may also check:How to resolve the algorithm AKS test for primes step by step in the OCaml programming language
You may also check:How to resolve the algorithm Hello world/Text step by step in the langur programming language
You may also check:How to resolve the algorithm Move-to-front algorithm step by step in the Julia programming language
You may also check:How to resolve the algorithm Dutch national flag problem step by step in the Factor programming language
You may also check:How to resolve the algorithm First class environments step by step in the J programming language