Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- import time # ONLY used for speed testing.
- import math # For use of modulus 'mod' and square root 'sqrt'.
- """ A mildly convoluted, jumbled mess to check if the value of 'p' is both
- a Prime Number and a Palindromic Number. Time included for speed tests. """
- n = 0 # <--- <--- Main int. Increase +1 per cycle
- # z = 41 <--- Constant Variable
- # p = n**2 - n + z <--- Main Equation
- """ ^^^ Moved above Maths to 'is_prime' ^^^ """
- def is_prime(p):
- """ Return 'True' if 'p' IS a prime number and False if 'p' is NOT prime. """
- if p == 1:
- return False # 1 is NOT a prime number!
- # Check if EVEN and NOT 2, (3, 5 and 11 as exceptions) '1' is NOT prime.
- if p == 2:
- return True
- if p > 2 and p % 2 == 0 or p % 3 == 0 or p % 5 == 0 or p % 11 == 0:
- return False
- # Check if EVEN (p > 2) can it be divided by 3, 5 or 11?
- max_div = math.floor(math.sqrt(p)) #<--- Python uses mod from the floor
- for d in range(3, 1 + max_div, 2): #<--- Skip ALL EVEN numbers
- if p % d == 0:
- return False
- return True
- def is_pal():
- # Test: IS 'p' Prime & Palindromic?
- # Yes == True | No == False
- for n in range(0, 10000):
- z = 41 # Constant Variable
- p = n**2 - n + z # Equation Variable
- is_prime(p) # Check if 'p' is prime
- if (is_prime(p) == True): #<--- Test if Prime
- if (str(p) == str(p)[::-1]): #<--- Test if Palindrome
- print("\n","(",n,")","P =",p)
- """ ========= TEST: Time Function ========= """
- def test_time():
- t0 = time.time() #<--- time 0
- for p in range(1,10000): # Check if prime up to 'Some Number'
- is_prime(p) # Initialize is_prime Test for 'p'.
- t1 = time.time() #<--- time 1
- print("Time Required:", t1 - t0) # Output Time (Compare Time 0 & Time 1).
- def run():
- is_pal()
- test_time() # Test Time Required to complete task.
- run() # Initialize the script.
- """ ========= RESULTS ========= """
- # OUTPUT:
- #Python 3.6.1 (default, Dec 2015, 13:05:11)
- #[GCC 4.8.2] on linux
- # ...
- # ( 10 ) P = 131
- #
- # ( 11 ) P = 151
- #
- # ( 17 ) P = 313
- #
- # ( 19 ) P = 383
- #
- # ( 28 ) P = 797
- #
- # ( 1814 ) P = 3288823
- #
- # ( 2738 ) P = 7493947
- """ ========= Time Required ========= """
- #
- # Range: n @ 10000 p @ 10000 <--- 10K!
- # 0.007488250732421875 ~ 0.008120298385620117
- #
- # Range: n @ 10000 p @ 100000 <--- 100K!
- # 0.2168869972229004 ~ 0.21737384796142578
- #
- # Range: n @ 10000 p @ 1000000 <--- 1 Million!
- # 5.324736595153809 ~ 5.35823392868042
- #
- # Range: n @ 10000 p @ 10000000 <--- 10 Million!
- # 188.01961374282837 ~ 207.1309757232666
- #
- # Range: n @ 10000 p @ 100000000 <--- 100 Million!
- # HAHAHA! ~ HAHAHA!
- #
- # ...
- """ ================================== """
- #END
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement