Advertisement
Python253

Primal_Rage

Mar 6th, 2019
552
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.14 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. import time                           # ONLY used for speed testing.
  4. import math                           # For use of modulus 'mod' and square root 'sqrt'.
  5.  
  6. """ A mildly convoluted, jumbled mess to check if the value of 'p' is both
  7. a Prime Number and a Palindromic Number. Time included for speed tests. """
  8.  
  9. n = 0                #   <---         <--- Main int. Increase +1 per cycle
  10. # z = 41                              <--- Constant Variable
  11. # p = n**2 - n + z                    <--- Main Equation
  12. """  ^^^ Moved above Maths to 'is_prime'  ^^^ """
  13.  
  14. def is_prime(p):
  15.   """ Return 'True' if 'p' IS a prime number and False if 'p' is NOT prime. """
  16.   if p == 1:
  17.     return False                      # 1 is NOT a prime number!
  18.           # Check if EVEN and NOT 2, (3, 5 and 11 as exceptions) '1' is NOT prime.
  19.   if p == 2:
  20.     return True
  21.   if p > 2 and p % 2 == 0 or p % 3 == 0 or p % 5 == 0 or p % 11 == 0:
  22.     return False
  23.           # Check if EVEN (p > 2) can it be divided by 3, 5 or 11?
  24.   max_div = math.floor(math.sqrt(p))  #<--- Python uses mod from the floor
  25.   for d in range(3, 1 + max_div, 2):  #<--- Skip ALL EVEN numbers
  26.     if p % d == 0:
  27.       return False
  28.   return True
  29.  
  30. def is_pal():  
  31.                                       # Test: IS 'p' Prime & Palindromic?
  32.                                       # Yes == True | No == False
  33.   for n in range(0, 10000):
  34.     z = 41                            # Constant Variable
  35.     p = n**2 - n + z                  # Equation Variable
  36.     is_prime(p)                       # Check if 'p' is prime
  37.     if (is_prime(p) == True):         #<--- Test if Prime
  38.       if (str(p) == str(p)[::-1]):    #<--- Test if Palindrome
  39.         print("\n","(",n,")","P =",p)
  40.  
  41. """ ========= TEST: Time Function ========= """
  42. def test_time():
  43.   t0 = time.time()                    #<--- time 0
  44.   for p in range(1,10000):          # Check if prime up to 'Some Number'
  45.     is_prime(p)                       # Initialize is_prime Test for 'p'.
  46.   t1 = time.time()                    #<--- time 1
  47.   print("Time Required:", t1 - t0)    # Output Time (Compare Time 0 & Time 1).
  48.  
  49. def run():
  50.   is_pal()
  51.   test_time()                         # Test Time Required to complete task.
  52.  
  53. run()                                 # Initialize the script.
  54.  
  55. """ ========= RESULTS ========= """
  56. # OUTPUT:
  57. #Python 3.6.1 (default, Dec 2015, 13:05:11)
  58. #[GCC 4.8.2] on linux
  59. # ...
  60. # ( 10 ) P = 131
  61. #
  62. # ( 11 ) P = 151
  63. #
  64. # ( 17 ) P = 313
  65. #
  66. # ( 19 ) P = 383
  67. #
  68. # ( 28 ) P = 797
  69. #
  70. # ( 1814 ) P = 3288823
  71. #
  72. # ( 2738 ) P = 7493947
  73. """ ========= Time Required ========= """
  74. #
  75. # Range:  n @ 10000 p @ 10000           <--- 10K!
  76. # 0.007488250732421875 ~ 0.008120298385620117
  77. #
  78. # Range:  n @ 10000 p @ 100000          <--- 100K!
  79. # 0.2168869972229004 ~ 0.21737384796142578
  80. #
  81. # Range:  n @ 10000 p @ 1000000         <--- 1 Million!
  82. # 5.324736595153809 ~ 5.35823392868042
  83. #
  84. # Range:  n @ 10000 p @ 10000000        <--- 10 Million!
  85. # 188.01961374282837 ~ 207.1309757232666
  86. #
  87. # Range:  n @ 10000 p @ 100000000       <--- 100 Million!
  88. # HAHAHA! ~ HAHAHA!
  89. #
  90. # ...
  91. """ ================================== """
  92.  
  93. #END
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement