Advertisement
Guest User

terrible_fizzbuzz

a guest
Sep 24th, 2021
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.32 KB | None | 0 0
  1.  
  2. #O(n)
  3. def crazy_modulo(n,mod):
  4. i = 0
  5. while(n>0):
  6. n-=1
  7. i+=1
  8. if(i==mod):
  9. i=0
  10. return i
  11.  
  12.  
  13. # 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.
  14. #O(n^2) + (non-significant) O(log(n)^2*log(log(n)))
  15. def compute_all_factors(n):
  16. i = n
  17. factor_list = []
  18. while(i>0):
  19. if(crazy_modulo(n,i)==0):
  20. factor_list.append(i) # log(n)^2 times, cost(log(log(n)))
  21. i-=1
  22. return factor_list
  23.  
  24. # First pass + Filtering Pass
  25. #O(n^2) + O((log(n)^2)*n^2)
  26. def compute_prime_factors(n):
  27. factor_list = compute_all_factors(n)
  28. prime_factor_list = []
  29. for factor in factor_list:
  30. if(len(compute_all_factors(factor))==2): #O(n^2) + (non-significant) O(log(n)^2*log(log(n)))
  31. prime_factor_list.append(factor)
  32. return prime_factor_list
  33.  
  34. def fact_match(subset,superset):
  35. is_subset = True
  36. for i in subset:
  37. is_found = False
  38. for j in superset:
  39. if(i==j):
  40. is_found = True
  41. is_subset = is_subset and is_found
  42. return is_subset
  43.  
  44. # Overall most significant factor is O((log(n)^2)*n^3), though for small N, the lesser order terms are non-trivial by comparison.
  45. # The lesser order terms are however quite hideous and i dont feel like trying to figure them all out.
  46. def FizzBuzz(fizz,buzz,n):
  47. full_saying = ""
  48. i = 1
  49. fizzfact = compute_prime_factors(fizz)
  50. buzzfact = compute_prime_factors(buzz)
  51. while(i<=n):
  52. say = ""
  53. i_fact = compute_prime_factors(i)
  54. if(fact_match(fizzfact, i_fact)):
  55. say+="fizz"
  56. if(fact_match(buzzfact, i_fact)):
  57. say+="buzz"
  58. if(len(say)==0):
  59. say = "{}".format(i)
  60. full_saying += " "+say
  61. i+=1
  62. return full_saying
  63.  
  64.  
  65. print(FizzBuzz(3,5,15))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement