GeorgeDI

BiggestPrimeNum

Nov 12th, 2022
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.42 KB | None | 0 0
  1. #Solution:
  2.  
  3.  
  4.  
  5. def BiggestPrimeNumberSOE(num):
  6. prime = [True for i in range(num+1)]
  7. # boolean array
  8. p = 2
  9. while (p * p <= num):
  10.  
  11. # If prime[p] is not changed, then it is a prime
  12. if (prime[p] == True):
  13.  
  14. # Updating all multiples of p
  15. for i in range(p * p, num+1, p):
  16. prime[i] = False
  17. p += 1
  18.  
  19. # Print all prime numbers
  20. list = []
  21. for p in range(2, num+1):
  22. if prime[p]:
  23. list.append(p)
  24. print(max(list))
  25.  
  26.  
  27. # ==
  28. if __name__ == '__main__':
  29. num = int(input())
  30.  
  31. BiggestPrimeNumberSOE(num)
  32.  
  33. # # Driver Program
  34. # n = int(input())
  35. # list = []
  36. # for n in range(n+1):
  37. # if (isPrime(n)):
  38. # list.append(n)
  39. # else:
  40. # continue
  41. # print(max(list))
  42.  
  43.  
  44. #add variable where the primes are added in list? and then I search for the bigger number
  45.  
  46.  
  47. # Description of the task
  48. '''Biggest Prime Number
  49. Description
  50. Write a program that finds all prime numbers in the range [1 ... N]. Use the Sieve of Eratosthenes algorithm. The program should print the biggest prime number which is <= N.
  51.  
  52. Input
  53. On the first line you will receive the number N
  54. Output
  55. Print the biggest prime number which is <= N
  56. Constraints
  57. 2 <= N <= 10 000 000
  58. Sample tests
  59. Input Output
  60. 13 13
  61. 126 113
  62. 26 23
  63. '''
  64.  
  65.  
  66.  
  67. #Resources and tests
  68.  
  69. '''
  70. def BiggestPrimeNumberSOE(num):
  71. prime = [True for i in range(num+1)]
  72. # boolean array
  73. p = 2
  74. while (p * p <= num):
  75.  
  76. # If prime[p] is not changed, then it is a prime
  77. if (prime[p] == True):
  78.  
  79. # Updating all multiples of p
  80. for i in range(p * p, num+1, p):
  81. prime[i] = False
  82. p += 1
  83.  
  84. # Print all prime numbers
  85. for p in range(2, num+1):
  86. if prime[p]:
  87. print(p)
  88.  
  89.  
  90. # ==
  91. if __name__ == '__main__':
  92. num = int(input())
  93. print(num)
  94. BiggestPrimeNumberSOE(num)'''
  95.  
  96.  
  97.  
  98. # def isPrime(n, i = 2):
  99.  
  100. # # Base cases
  101. # if (n <= 2):
  102. # return True if(n == 2) else False
  103. # if (n % i == 0):
  104. # return False
  105. # if (i * i > n):
  106. # return True
  107.  
  108. # # Check for next divisor
  109. # return isPrime(n, i + 1)
  110.  
  111.  
  112. # # Driver Program
  113. # n = int(input())
  114. # list = []
  115. # for n in range(n+1):
  116. # if (isPrime(n)):
  117. # list.append(n)
  118. # else:
  119. # continue
  120. # print(max(list))
  121.  
Tags: python
Advertisement
Add Comment
Please, Sign In to add comment