Advertisement
Guest User

Untitled

a guest
Feb 29th, 2020
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.02 KB | None | 0 0
  1. def first():
  2. res_array = []
  3. f = open('liczby.txt', 'r')
  4. for x in f:
  5. h = int(x)
  6. while h % 3 == 0:
  7. h = h / 3
  8. if h == 1:
  9. res_array.append(x)
  10. f.close()
  11. return res_array
  12.  
  13.  
  14. def second():
  15. res_array = []
  16. f = open('liczby.txt', 'r')
  17. for x in f:
  18. res = 0
  19. for i in x:
  20. if not i == '\n':
  21. res = res + factorial(int(i))
  22. if int(res) == int(x):
  23. res_array.append(x)
  24. f.close()
  25. return res_array
  26.  
  27.  
  28. def third():
  29. f = open('liczby.txt', 'r')
  30. biggest_counter = 0
  31. last_pos = 0
  32. loop_counter = 0
  33. actual_counter = 0
  34. gcd_memory = 0
  35. biggest_gcd = 0
  36. y = int(f.readline())
  37. for x in f:
  38. loop_counter += 1
  39. if gcd(x, y) == gcd_memory:
  40. actual_counter += 1
  41.  
  42. else:
  43. if actual_counter > biggest_counter:
  44. biggest_counter = actual_counter
  45. last_pos = loop_counter
  46. biggest_gcd = gcd_memory
  47. actual_counter = 0
  48. gcd_memory = gcd(x, y)
  49. y = x
  50. res_array = [last_pos + 1, biggest_counter, biggest_gcd]
  51. f.close()
  52. return res_array
  53.  
  54.  
  55. def factorial(x):
  56. if x == 1:
  57. return x
  58. elif x > 1:
  59. x = factorial(x - 1) * x
  60. return x
  61. else:
  62. return 0
  63.  
  64.  
  65. def gcd(x, y):
  66. rest = int(x) % int(y)
  67. last_rest = rest
  68. while not rest == 0:
  69. x = y
  70. y = rest
  71. rest = int(x) % int(y)
  72. if not rest == 0:
  73. last_rest = rest
  74. return int(last_rest)
  75.  
  76.  
  77. def main():
  78. f = open('wynik4.txt', 'a')
  79. ex_first = first()
  80. ex_second = second()
  81. ex_third = third()
  82. f.write("Zad 4.1\n")
  83. for x in ex_first:
  84. f.write(x)
  85. f.write("Zad 4.2\n")
  86. for x in ex_second:
  87. f.write(x)
  88. f.write("Zad 4.3\n")
  89. for x in ex_third:
  90. f.write(str(x) + "\n")
  91. f.close()
  92.  
  93.  
  94. if __name__ == "__main__":
  95. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement