Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import time
- def IsPrime(n):
- start = time.time()
- if n == 2:
- end = time.time()
- print("The function took", str(end-start), "seconds.")
- return True
- if n == 3:
- end = time.time()
- print("The function took", str(end-start), "seconds.")
- return True
- if n == 4:
- end = time.time()
- print("The function took", str(end-start), "seconds.")
- return False
- if n == 5:
- end = time.time()
- print("The function took", str(end-start), "seconds.")
- return True
- if n == 6:
- end = time.time()
- print("The function took", str(end-start), "seconds.")
- return False
- if n > 6:
- bool = []
- for i in range(2, n//2+1):
- if n%i == 0:
- bool.append("True")
- if "True" in bool:
- end = time.time()
- print("The function took", str(end-start), "seconds.")
- return False
- else:
- end = time.time()
- print("The function took", str(end-start), "seconds.")
- return True
- else:
- end = time.time()
- print("The function took", str(end-start), "seconds.")
- return False
- def IsPrime_alternative(n):
- start = time.time()
- i=2
- if n<i:
- end = time.time()
- print("The function took", str(end-start), "seconds.")
- return False
- while i < n:
- if n%i==0:
- end = time.time()
- print("The function took", str(end-start), "seconds.")
- return False
- else:
- i+=1
- end = time.time()
- print("The function took", str(end-start), "seconds.")
- return True
- difficult_to_divide_list = []
- # check for a random number x if it is hard to divide (i.e. divisible only by numbers higher than 8). If yes, return True, else: return False
- def check_boolean(x):
- bool = False # assume that it is not hard to divide
- while ~bool:
- if x%2 != 0:
- bool = True
- else:
- bool = False
- break
- if x%3 != 0:
- bool = True
- else:
- bool = False
- break
- if x%4 != 0:
- bool = True
- else:
- bool = False
- break
- if x%5 != 0:
- bool = True
- else:
- bool = False
- break
- if x%6 != 0:
- bool = True
- else:
- bool = False
- break
- if x%7 != 0:
- bool = True
- else:
- bool = False
- break
- if x%8 != 0:
- bool = True
- else:
- bool = False
- break
- break
- return bool
- N = 1000
- prime_list = []
- for i in range(1,N):
- if IsPrime_alternative(i):
- prime_list.append(i)
- def add_to_hard_divisors_list(N):
- for i in range(2,N):
- if check_boolean(i):
- difficult_to_divide_list.append(i)
- add_to_hard_divisors_list(N)
- def intersection(l1, l2):
- l3 = [value for value in l1 if value in l2]
- return l3
- def overhang(l1, l2):
- l3 = [value for value in l1 if value not in intersection(l1,l2)]
- return l3
- print("\n"+"This is the list of prime numbers: ")
- print(prime_list)
- print("\n"+"This is the list of difficult to divide numbers: ")
- print(difficult_to_divide_list)
- print("\n"+"This is the list of the junction: ")
- print(intersection(difficult_to_divide_list, prime_list))
- print("\n"+"This is the list of difficult to divide non-prime numbers: ")
- print(overhang(difficult_to_divide_list, prime_list))
Add Comment
Please, Sign In to add comment