Last active
August 29, 2015 13:57
-
-
Save Half-Shot/9750186 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/env python3 | |
#Written by Will Hunt and adapted from http://rosettacode.org/wiki/Kaprekar_numbers#Python | |
#24/3/14 | |
#Used to calculate the kaprekar | |
def kaprekar(n): | |
n2 = str(n**2) #Raise n to a power of 2. | |
for i in range(len(n2)): #for each digit in n2 | |
a, b = int(n2[:i] or 0), int(n2[i:]) #get the first digit (or 0 if it does not exist) and the last digit. | |
if b and a + b == n: #If it is a kaprekar number | |
return n #Return it. | |
if __name__ == "__main__": | |
lower = int(input("Enter a lower bound positive number:")) | |
upper = int(input("Enter a upper bound positive number:")) | |
nlist = [] | |
for n in range(lower,upper): | |
number = kaprekar(n) | |
if(number): | |
nlist.append(kaprekar(n)) | |
print("Your kaprekar numbers are: ",nlist) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment