Advertisement
Guest User

Roxanne Method

a guest
Sep 26th, 2015
126
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.14 KB | None
  1. from math import sqrt
  2. def isPrime(n):
  3.     if (n < 2):
  4.         return False
  5.     elif (n == 2 or n == 3):
  6.         return True
  7.     else:
  8.         for i in range(2,int(sqrt(n))+1):
  9.             if (n % i == 0):
  10.                 return False
  11.         return True
  12.  
  13. def getFactors(n):
  14.     f = []
  15.     for i in range(1,n+1):
  16.         if (n % i == 0):
  17.             f.append(i)
  18.     return f
  19.  
  20. def closestFactor(f,c):
  21.     diff = pow(c,3)
  22.     for i in range(0,len(f)):
  23.         if (abs(f[i] - c) < diff):
  24.             diff = abs(f[i] - c)
  25.             a = f[i]
  26.     return a
  27.    
  28. def roxanneMethod(num_candies):
  29.     factors = getFactors(num_candies)
  30.     cuberoot = pow(num_candies,1./3)
  31.     if (pow(int(cuberoot),3) == num_candies):
  32.         a = int(cuberoot)
  33.         b = a
  34.         c = a
  35.     else:
  36.         a = closestFactor(factors,cuberoot)
  37.         if (isPrime(int(num_candies/a))):
  38.             b = 1
  39.             c = num_candies / a
  40.         else:
  41.             squareroot = pow(num_candies/a,1./2)
  42.             if (pow(int(squareroot),2) == int(num_candies/a)):
  43.                 b = int(squareroot)
  44.                 c = b
  45.             else:
  46.                 factors = getFactors(num_candies/a)
  47.                 #print(num_candies /a )
  48.                 #print(factors
  49.                 b = closestFactor(factors,squareroot)
  50.                 c = int((num_candies/a)/b)
  51.         squareroot = pow(a * b, 1./2)
  52.         factors = getFactors(a * b)
  53.         a_star = closestFactor(factors,squareroot)
  54.         b_star = (a*b)/a_star
  55.         if (abs(a_star-b_star) < abs(a-b)):
  56.             a = a_star
  57.             b = b_star
  58.    
  59.         squareroot = pow(a * c, 1./2)
  60.         factors = getFactors(a * c)
  61.         a_star = closestFactor(factors,squareroot)
  62.         c_star = (a*c)/a_star
  63.         if (abs(a_star-c_star) < abs(a-c)):
  64.             a = a_star
  65.             c = c_star
  66.     l = [a,b,c]
  67.     return l
  68.  
  69. # num_c = int(input("How many candies do you want to package: "))
  70. # while (num_c < 1):
  71. #   num_c = int(input("How many candies do you want to package: "))
  72. #
  73. # ans = roxanneMethod(num_c)
  74. # print(ans)
  75.  
  76. u = int(input("Do you want to get a list of answers? Type 1. Do you want to manually try numbers? Type 2."))
  77. if (u == 1):
  78.     max = int(input("What number do you want to print up to?"))
  79.     for i in range(1,max + 1):
  80.         print("num candies = " + str(i) + " and Roxanne method gives " + str(roxanneMethod(i)))
  81. else:
  82.     while True:
  83.         choice = int(input("What number would you like to check? "))
  84.         print("The roxanne method returns: " + str(roxanneMethod(choice)))
Advertisement
RAW Paste Data Copied
Advertisement