Last active
November 23, 2015 11:20
-
-
Save thepulkitagarwal/8b54de31c9d6042cad0a to your computer and use it in GitHub Desktop.
Input: Any fraction between 0 and 1, Output: A set of distinct natural numbers, the sum of reciprocals of these numbers is the input
This file contains hidden or 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
from fractions import gcd | |
def addToList(denominators, q): | |
if(q in denominators): | |
addToList(denominators, q+1) | |
addToList(denominators, q*(q+1)) | |
else: | |
denominators.append(q) | |
def breakIntoFractions(denominators, p, q): | |
if(p == 1): | |
addToList(denominators, q) | |
else: | |
addToList(denominators, q) | |
breakIntoFractions(denominators, p-1, q) | |
def main(p, q): | |
if(p < q and p != 0): | |
denominators = [] | |
div = gcd(p, q) | |
breakIntoFractions(denominators, p/div, q/div) | |
print "(" + str(p) + "/" + str(q) + "): " + str(denominators) | |
else: | |
print "The fraction should be between 0 and 1." | |
p = int(raw_input("Enter numerator: ")) | |
q = int(raw_input("Enter denominator: ")) | |
main(p, q) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment