Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from math import log
- import datetime
- from mpi4py import MPI
- import math
- comm = MPI.COMM_WORLD
- rank = comm.Get_rank()
- size = comm.Get_size()
- #print(size)
- # Liczba pierwsza Mersenne'a
- M = lambda x: 2**x - 1
- f = lambda x: 2**(x-1) * M(x)
- #print(size, rank)
- # Test Lucasa-Lehmera
- def S(x):
- S = 4
- for i in range(2, x):
- S = (S**2 - 2) % M(x)
- return S
- def perfect(n):
- perfect = []
- p = int(round((log(n, 2) + 1) / 2))
- s = [x for x in range(3, p + 1,2)]
- perfect.append([f(2), M(2)])
- k = math.ceil(len(s) / (size - 1))
- for kk in range(k):
- for i in range(1, size):
- if (kk*(size-1)+i-1)>=len(s):
- comm.send(0, dest=i, tag=11)
- elif (kk*(size-1)+i-1)>=size:
- comm.send(0, dest=i, tag=11)
- else:
- comm.send(s[kk*(size-1)+i-1], dest=i, tag=11)
- for i in range(1, size):
- data = comm.recv(source=i, tag=12)
- if len(data)>0:
- perfect.append(data)
- return perfect
- if rank == 0:
- start = datetime.datetime.now()
- s = perfect(2 ** 40)
- for el in s:
- print("Liczba doskonała :" + str(el[0]) + " i liczba Mersenne'a :" + str(el[1]))
- end = datetime.datetime.now()
- print((end - start).microseconds)
- elif rank > 0:
- while True:
- data = comm.recv(source=0, tag=11)
- if data == 0:
- comm.send([], dest=0, tag=12)
- break
- print(False)
- elif S(data) == 0:
- comm.send([f(data), M(data)], dest=0, tag=12)
- print([f(data), M(data)])
- else:
- comm.send([], dest=0, tag=12)
- print(False)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement