コード例 #1
0
class TestPrime(object):
    """Tests focused on the Prime class."""
    def setup_method(self):
        self.prime = Prime()

    def test_prime_happy(self):
        """Test prime() method on the happy path."""
        result = self.prime.primes()
        assert result is not None
        assert True

    def test_prime_5(self):
        """ Test prime(5) and make sure it returns [2, 3, 5]. """
        result = self.prime.primes(stop=5)
        assert len(result) == 3, 'list of primes was expected to have length 3'
        assert result[0] == 2
        assert result[1] == 3
        assert result[2] == 5

    def test_is_prime(self):
        assert self.prime.is_prime(5)
        assert self.prime.is_prime(7)

        assert not self.prime.is_prime(500)
        assert not self.prime.is_prime(280000)

    def test_primes(self):
        result = self.prime.primes(stop=100000)
        print(result)

    def test_primes_between(self):
        result = self.prime.primes_between(1, 5)

        expected_result = [2, 3]
        assert result == expected_result, 'Expected list of primes did not match'

        expected_result = [
            7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71,
            73, 79, 83, 89
        ]
        result = self.prime.primes_between(5, 97)
        assert result == expected_result, 'Expected list of primes did not match'

        # When min and max yield an empty set, we should get an empty list
        result = self.prime.primes_between(4, 5)
        assert result == [], 'Result should have been an empty list'
コード例 #2
0
ファイル: 007.py プロジェクト: ryansgot/euler
#!/usr/bin/python
#
# Author: Ryan Scott
# Purpose: Solve problem 007 of the Euler Project
# Assumes: Python 2.7
#
# By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13.
# What is the 10 001st prime number?

from prime import Prime

prime = Prime()
prime_count = 6 # given by problem
num = 14        # next number to try
while True:
    if prime.is_prime(num):
        prime_count += 1
    if 10001 == prime_count:
        break
    num += 1

print str(num - 1)
コード例 #3
0
ファイル: main.py プロジェクト: christoph-fricke/prime-py
"""Main file"""

from prime import Prime
import timeit
import json

primeDict = {}
primeId = 0
for i in range(1000000):
    start_time = timeit.default_timer()
    result = Prime.is_prime(i)
    end_time = timeit.default_timer()
    if result:
        primeDict[primeId] = {'prime': i, 'time': end_time - start_time}
        primeId += 1
with open('primes.json', 'w') as file:
    json.dump(primeDict, file)
コード例 #4
0
ファイル: 003.py プロジェクト: ryansgot/euler
#!/usr/bin/python
#
# Author: Ryan Scott
# Purpose: Solve problem 003 of the Euler Project
# Assumes: Python 2.7
#
# The prime factors of 13195 are 5, 7, 13 and 29.
# What is the largest prime factor of the number 600851475143 ?

from prime import Prime
import math

num = 600851475143

# keep track of state through loop
largest_prime_divisor = 0
prime = Prime()
for possible_divisor in range(2, int(math.ceil(math.sqrt(num)))):
    if 0 == num % possible_divisor and prime.is_prime(possible_divisor):
        if possible_divisor > largest_prime_divisor:
            largest_prime_divisor = possible_divisor

print str(largest_prime_divisor)