How to resolve the algorithm Kaprekar numbers step by step in the Python programming language
How to resolve the algorithm Kaprekar numbers step by step in the Python programming language
Table of Contents
Problem Statement
A positive integer is a Kaprekar number if: Note that a split resulting in a part consisting purely of 0s is not valid, as 0 is not considered positive.
10000 (1002) splitting from left to right:
Generate and show all Kaprekar numbers less than 10,000.
Optionally, count (and report the count of) how many Kaprekar numbers are less than 1,000,000.
The concept of Kaprekar numbers is not limited to base 10 (i.e. decimal numbers); if you can, show that Kaprekar numbers exist in other bases too.
For this purpose, do the following:
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Kaprekar numbers step by step in the Python programming language
The first code snippet you provided is a Python function that takes a number n
and returns n
if it is a Kaprekar number. A Kaprekar number is a number whose square can be split into two positive-integer numbers whose sum is the original number again. For example, 9 is a Kaprekar number, because 9^2=81 and 8+1=9.
The function works by first converting n
to a string and then iterating over the string. For each iteration, it splits the string into two parts at the current index, and then converts the two parts back to integers. If the sum of the two integers is equal to n
, then the function returns n
. Otherwise, it continues to the next iteration.
The second code snippet you provided is a Python function that takes three arguments: a starting number m
, an ending number n
, and a base base
. It returns a list of all the Kaprekar numbers between m
and n
, inclusive, in the given base.
The function works by first converting m
and n
to strings in the given base. Then, it iterates over all the numbers between m
and n
, inclusive, and checks if each number is a Kaprekar number. If it is, it adds the number to the list. Finally, it returns the list of all the Kaprekar numbers.
The third code snippet you provided is a Python function that takes three arguments: a starting number m
, an ending number n
, and a base base
. It returns a list of all the Kaprekar numbers between m
and n
, inclusive, in the given base.
The function works by first converting m
and n
to strings in the given base. Then, it iterates over all the numbers between m
and n
, inclusive, and checks if each number is a Kaprekar number. If it is, it adds the number to the list. Finally, it returns the list of all the Kaprekar numbers.
The fourth code snippet you provided is a Python function that takes two arguments: a base Base
and a range of numbers Start
and End
. It returns a list of all the numbers in the given range that are not divisible by any of the numbers in the given base.
The function works by first creating a list of all the numbers in the given range. Then, it iterates over all the numbers in the given base and removes any number from the list that is divisible by the current number. Finally, it returns the list of all the numbers that are not divisible by any of the numbers in the given base.
The fifth code snippet you provided is a Python function that takes two arguments: a base Base
and a range of numbers Start
and End
. It returns a list of all the Kaprekar numbers in the given range in the given base.
The function works by first converting the start and end numbers to strings in the given base. Then, it iterates over all the numbers in the given range and checks if each number is a Kaprekar number. If it is, it adds the number to the list. Finally, it returns the list of all the Kaprekar numbers in the given range in the given base.
Source code in the python programming language
>>> def k(n):
n2 = str(n**2)
for i in range(len(n2)):
a, b = int(n2[:i] or 0), int(n2[i:])
if b and a + b == n:
return n
#return (n, (n2[:i], n2[i:]))
>>> [x for x in range(1,10000) if k(x)]
[1, 9, 45, 55, 99, 297, 703, 999, 2223, 2728, 4879, 4950, 5050, 5292, 7272, 7777, 9999]
>>> len([x for x in range(1,1000000) if k(x)])
54
>>>
def encode(n, base):
result = ""
while n:
n, d = divmod(n, base)
if d < 10:
result += str(d)
else:
result += chr(d - 10 + ord("a"))
return result[::-1]
def Kaprekar(n, base):
if n == '1':
return True
sq = encode((int(n, base)**2), base)
for i in range(1,len(sq)):
if (int(sq[:i], base) + int(sq[i:], base) == int(n, base)) and (int(sq[:i], base) > 0) and (int(sq[i:], base)>0):
return True
return False
def Find(m, n, base):
return [encode(i, base) for i in range(m,n+1) if Kaprekar(encode(i, base), base)]
m = int(raw_input('Where to start?\n'))
n = int(raw_input('Where to stop?\n'))
base = int(raw_input('Enter base:'))
KNumbers = Find(m, n, base)
for i in KNumbers:
print i
print 'The number of Kaprekar Numbers found are',
print len(KNumbers)
raw_input()
Base = 10
N = 6
Paddy_cnt = 1
for n in range(N):
for V in CastOut(Base,Start=Base**n,End=Base**(n+1)):
for B in range(n+1,n*2+2):
x,y = divmod(V*V,Base**B)
if V == x+y and 0<y:
print('{1}: {0}'.format(V, Paddy_cnt))
Paddy_cnt += 1
break
Base = 16
N = 4
Paddy_cnt = 1
for V in CastOut(Base,Start=1,End=Base**N):
for B in range(1,N*2-1):
x,y = divmod(V*V,Base**B)
if V == x+y and 0<y:
print('{1}: {0:x}'.format(V, Paddy_cnt))
Paddy_cnt += 1
break
You may also check:How to resolve the algorithm String length step by step in the UNIX Shell programming language
You may also check:How to resolve the algorithm Empty program step by step in the Pike programming language
You may also check:How to resolve the algorithm Smarandache prime-digital sequence step by step in the Julia programming language
You may also check:How to resolve the algorithm Jump anywhere step by step in the V (Vlang) programming language
You may also check:How to resolve the algorithm Thue-Morse step by step in the Kotlin programming language