def test_prime_factor_calculation(self): prime = PrimeNumbers() var = 13195 prime.calc_all_prime_factors(var) self.assertEqual(prime.last_prime_factor[0], 5) self.assertEqual(prime.last_prime_factor[1], 7) self.assertEqual(prime.last_prime_factor[2], 13) self.assertEqual(prime.last_prime_factor[3], 29)
def compute_prime_factors(self): """ Computes the prime fators for a given integer. :return: List of prime factors :rtype: list :raises PrimeFactor Error: if something unexpected happens """ if self.integer < 1: raise PrimeFactorsError("Value less than one is not accepted") else: prime_number_generator = PrimeNumbers(self.integer) prime_numbers = prime_number_generator.sieve_of_atkin() count = 0 while self.integer is not 1: while self.integer % prime_numbers[count] is 0: self.integer /= prime_numbers[count] self.prime_factors.append(prime_numbers[count]) count = count + 1 return self.prime_factors
def compute_prime_factors(self): """ Computes the prime fators for a given integer. :return: List of prime factors :rtype: list :raises PrimeFactor Error: if something unexpected happens """ if self.integer < 1: raise PrimeFactorsError('Value less than one is not accepted') else: prime_number_generator = PrimeNumbers(self.integer) prime_numbers = prime_number_generator.sieve_of_atkin() count = 0 while self.integer is not 1: while self.integer % prime_numbers[count] is 0: self.integer /= prime_numbers[count] self.prime_factors.append(prime_numbers[count]) count = count + 1 return self.prime_factors
def test_file_handling(self): # Generate data to file prime = PrimeNumbers() var = 10 prime.calc_all_prime_factors(var) # Init data from file prime = PrimeNumbers() self.assertEqual(prime.primes[0], 2) self.assertEqual(prime.primes[1], 3) self.assertEqual(prime.primes[2], 5) self.assertEqual(prime.primes[3], 7)
def test_prime_numbers_for_two(self): prime = PrimeNumbers(2) self.assertEqual(prime.rwh(), [2])
import multiprocessing import datetime from prime_numbers import PrimeNumbers prime_object = PrimeNumbers() primes_till = 100000000 counter = 0 print('Parallel Programming Assignment 2') print('*********************************') def increase_counter(number): global primes global prime_object if prime_object.is_prime(number): primes[number-1] = True running = True table = [] while running: primes = multiprocessing.Array('b', primes_till) primes_till = eval(input("Enter max number: ")) processors_count = multiprocessing.cpu_count() chosen_processors = eval(input(f'Chose number of processors from 1 to {processors_count}: ')) if chosen_processors not in range(1, processors_count + 1): print(f'You chose {chosen_processors}, ' f'that is not included in the expected range! We are proceeding with the maximum power.') chosen_processors = processors_count
import time from itertools import takewhile, islice, repeat from prime_numbers import PrimeNumbers c = 20000 p = 0 i = 0 for p in PrimeNumbers(): start = time.time() i += 1 end = time.time() - start print('Prim: {}. Count:{} {:1.3f}s Dichte {:1.3f}'.format( p, i, end, float(i) / float(p))) # prim = PrimeNumbers() # # start = time.time() # prim = list(takewhile(lambda x: x < 224738, prim)) # end = time.time() - start # print(prim) # print( # '1. Prim: {}. It took for {}: {:1.3f}s Dichte {:1.3f}'.format(prim[-1], len(prim), end, float(len(prim)) / float(prim[-1]))) # start = time.time() # prim = list(map(lambda x: next(x), repeat(PrimeNumbers(), c))) # for p in prim: # pass # end = time.time() - start # print(prim) # print('2. Prim: {}. It took for {}: {:1.3f}s Dichte {:1.3f}'.format(prim[-1], c, end, float(c) / float(p)))
def test_4_is_prime(self): self.assertEqual(PrimeNumbers.test_if_prime(value=4), False)
# python3 search.py # Search for prime numbers. # Простые числа from prime_numbers import PrimeNumbers from time import process_time print("time =", process_time()) prime_numbers = PrimeNumbers() print(prime_numbers.sequenceprimes()) print("LEN =", len(prime_numbers.sequenceprimes())) print("Default:", prime_numbers.rangeprimes()) print(prime_numbers.sequenceprimes(11)) print("from 6 to 13:", prime_numbers.rangeprimes(6, 13)) print("------------------------------------------------") print("from 98 to 280:", prime_numbers.rangeprimes(98, 250)) print("------------------------------------------------") print("time =", process_time()) list_need = prime_numbers.rangeprimes(10000, 1000000) print("time =", process_time()) print("LEN =", str(len(list_need)))
def test_prime_numbers_for_two(self): prime = PrimeNumbers(2) self.assertEqual(prime.rwh(),[2])
def test_prime_raise_value_error_int_negative_one(self): self.assertRaises(ValueError, PrimeNumbers().prime, -1)
def test_prime_upto_ten(self): self.assertEqual(PrimeNumbers().prime(12), [2, 3, 5, 7, 11], msg="Expected [[2, 3, 5, 7, 11]")
def test_prime_upto_two(self): self.assertEqual(PrimeNumbers().prime(2), [2], msg="Expected [2]")
def test_prime_numbers_for_one(self): prime = PrimeNumbers(1) self.assertEqual(prime.rwh(), [])
from prime_numbers import PrimeNumbers """ Main program """ if __name__ == '__main__': try: # Init prime number class (Read calculated prime numbers from file) prime = PrimeNumbers() # Ask value for prime factor calculation var = int(input("Give me the number:")) # Set result file prime.set_res_file('res.txt') # Calculate all prime factors for a number defined by user prime.calc_all_prime_factors(var) except Exception as e: print(str(e))
def test_prime_raises_type_error_input_list(self): self.assertRaises(TypeError, PrimeNumbers().prime, [1, 2, 3])
def test_prime_raises_type_error_input_string(self): self.assertRaises(TypeError, PrimeNumbers().prime, "one")
def test_prime_upto_twenty(self): self.assertEqual(PrimeNumbers().prime(20), [2, 3, 5, 7, 11, 13, 17, 19], msg="Expected [2, 3, 5, 7, 11, 13, 17, 19]")
def test_prime_numbers(self): prime = PrimeNumbers(11) self.assertEqual(prime.rwh(), [2, 3, 5, 7, 11])
def test_out_of_range_error(self): with self.assertRaises(ValueError): PrimeNumbers.test_if_prime(value=-1)
def test_prime_numbers_for_one(self): prime = PrimeNumbers(1) self.assertEqual(prime.rwh(),[])
def test_2_is_prime(self): self.assertEqual(PrimeNumbers.test_if_prime(value=2), True)
def test_prime_numbers(self): prime = PrimeNumbers(11) self.assertEqual(prime.rwh(),[2,3,5,7,11])
def test_prime_raise_value_error_int_equal_zero(self): self.assertRaises(ValueError, PrimeNumbers().prime, 0)