Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #O(n)
- def crazy_modulo(n,mod):
- i = 0
- while(n>0):
- n-=1
- i+=1
- if(i==mod):
- i=0
- return i
- # Random tidbit, according to a random reddit math lecture, supposedly, the average number of prime factors is ~O(log(n)), i shall use log(n)log(n) as the bound for all factors, which may be wrong, but i believe would err on the low side, as there is a set picking problem for combinations of prime factors to make non-prime factors but bounded not by the number of factors selected, but by the final product's size. I think a 2 factor selection would give you the squaring, and the actual bound would be some other odd exponent bounded by some function of N.
- #O(n^2) + (non-significant) O(log(n)^2*log(log(n)))
- def compute_all_factors(n):
- i = n
- factor_list = []
- while(i>0):
- if(crazy_modulo(n,i)==0):
- factor_list.append(i) # log(n)^2 times, cost(log(log(n)))
- i-=1
- return factor_list
- # First pass + Filtering Pass
- #O(n^2) + O((log(n)^2)*n^2)
- def compute_prime_factors(n):
- factor_list = compute_all_factors(n)
- prime_factor_list = []
- for factor in factor_list:
- if(len(compute_all_factors(factor))==2): #O(n^2) + (non-significant) O(log(n)^2*log(log(n)))
- prime_factor_list.append(factor)
- return prime_factor_list
- def fact_match(subset,superset):
- is_subset = True
- for i in subset:
- is_found = False
- for j in superset:
- if(i==j):
- is_found = True
- is_subset = is_subset and is_found
- return is_subset
- # Overall most significant factor is O((log(n)^2)*n^3), though for small N, the lesser order terms are non-trivial by comparison.
- # The lesser order terms are however quite hideous and i dont feel like trying to figure them all out.
- def FizzBuzz(fizz,buzz,n):
- full_saying = ""
- i = 1
- fizzfact = compute_prime_factors(fizz)
- buzzfact = compute_prime_factors(buzz)
- while(i<=n):
- say = ""
- i_fact = compute_prime_factors(i)
- if(fact_match(fizzfact, i_fact)):
- say+="fizz"
- if(fact_match(buzzfact, i_fact)):
- say+="buzz"
- if(len(say)==0):
- say = "{}".format(i)
- full_saying += " "+say
- i+=1
- return full_saying
- print(FizzBuzz(3,5,15))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement