- Trending Categories
- Data Structure
- Operating System
- C Programming
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to find Kaprekar numbers within a given range using Python?
A modified Kaprekar number is a positive whole number n with d digits, such that when we split its square into two pieces - a right hand piece r with d digits and a left hand piece l that contains the remaining d or d−1 digits, the sum of the pieces is equal to the original number (i.e. l + r = n).
You can find Kaprekar numbers within a given range by testing each number for the given condition in the given range.
def print_Kaprekar_nums(start, end): for i in range(start, end + 1): # Get the digits from the square in a list: sqr = i ** 2 digits = str(sqr) # Now loop from 1 to length of the number - 1, sum both sides and check length = len(digits) for x in range(1, length): left = int("".join(digits[:x])) right = int("".join(digits[x:])) if (left + right) == i: print("Number: " + str(i) + "Left: " + str(left) + " Right: " + str(right)) print_Kaprekar_nums(150, 8000)
This will give the output −
Number: 297Left: 88 Right: 209 Number: 703Left: 494 Right: 209 Number: 999Left: 998 Right: 1 Number: 1000Left: 1000 Right: 0 Number: 2223Left: 494 Right: 1729 Number: 2728Left: 744 Right: 1984 Number: 4879Left: 238 Right: 4641 Number: 4950Left: 2450 Right: 2500 Number: 5050Left: 2550 Right: 2500 Number: 5292Left: 28 Right: 5264 Number: 7272Left: 5288 Right: 1984 Number: 7777Left: 6048 Right: 1729
- Python - Find the number of prime numbers within a given range of numbers
- PHP program to find the sum of odd numbers within a given range
- Golang Program to Print Odd Numbers Within a Given Range
- Python Generate random numbers within a given range and store in a list
- How to print array elements within a given range using Numpy?
- Python program to generate random numbers within a given range and store in a list?
- How to create a numpy array within a given range?
- Java program to generate random numbers within a given range and store in a list
- Python Program to replace list elements within a range with a given number
- Program to find bitwise AND of range of numbers in given range in Python
- Find elements within range in numpy in Python
- Program to find number of pairs where elements square is within the given range in Python
- Java Program to create random BigInteger within a given range