Skip to content

Commit

Permalink
Updates
Browse files Browse the repository at this point in the history
Added some infor in the readme, Added the unit_test.py, added some
examples and made some minor modifications in pyprime.
  • Loading branch information
sylhare committed Mar 22, 2017
1 parent 06f74be commit cb8168a
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 8 deletions.
13 changes: 9 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ Algorithm developped :
- Fermat's test (based on Fermat's theorem)
- Prime generating functions

## Math
# Math

Here are a bit of information to help understand some of the algorithms

Congruence :
------------
"``" means congruent, `a ≡ b (mod m)` implies that
`m/(a-b), ∃ k ∈ Z` that verifies `a = kn + b`
`m / (a-b), ∃ k ∈ Z` that verifies `a = kn + b`

which implies:

Expand All @@ -35,10 +35,15 @@ Fermart's Theorem

Miller rabin
------------
Take a random `a{1,...,n−1}` and `n > 2`, </br>
Take a random `a{1,...,n−1}` and `n > 2`, </br>
Find `d` and `s` such as with `n - 1 = 2^s * d` (with d odd) </br>
if `(a^d)^2^r ≡ 1 mod n` for all `r` in `0` to `s-1` </br>
Then `n` is prime.

The test output is false of 1/4 of the "a values" possible in `n`,
so the test is repeated t times.
so the test is repeated t times.


Strong Pseudoprime
-------------------
A strong [pseudoprime](http:https://mathworld.wolfram.com/StrongPseudoprime.html) to a base `a` is an odd composite number `n` with `n-1 = d·2^s` (for d odd) for which either `a^d = 1(mod n)` or `a^(d·2^r) = -1(mod n)` for some `r = 0, 1, ..., s-1` </br>
11 changes: 8 additions & 3 deletions src/examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,15 @@
Created on Tue Mar 21 13:23:52 2017
@author: sylhare
"""

import pyprime as p
import unit_test as ut

#Demo of Grphical Prime functions
p.sacksPlot()
p.ulamPlot()

#Demo of the Primarity testing functions
print(p.isPrime(7))
Expand All @@ -17,6 +22,6 @@
print(p.genPrimes(7))
print(p.findPrimes(2, 7, p.fermat))

#Demo of Grphical Prime functions
p.sacksPlot()
p.ulamPlot()
#Demo of the unit_test functions
print(ut.carmiTest(p.isPrime))
print(ut.ppTest(p.isPrime))
2 changes: 1 addition & 1 deletion src/pyprime.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ def sacks(upper=1000, primeTest=pyprime):
theta = math.sqrt(i) * 2 * math.pi #i=1 theta= 2pi for a given i, angle=(i*theta)/1
r = math.sqrt(i)

if primeTest(i) == True:
if primeTest(i):
pricoo.append((theta,r))
else:
coord.append((theta,r))
Expand Down
81 changes: 81 additions & 0 deletions src/unit_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# -*- coding: utf-8 -*-
"""
Created on Tue Feb 14 11:34:49 2017
@author: Sylhare
"""


#Carmichael number often trigger false positive for the fermat algorithm
__carmichael = [561, 1105, 1729, 2465, 2821, 6601, 8911, 10585, 15841, 29341]

#The key is the base, the list is the pseudoprimes of that base
__pseudoPrimes = {2: [2047, 3277, 4033, 4681, 8321],
3: [121, 703, 1891, 3281, 8401, 8911],
4: [341, 1387, 2047, 3277, 4033, 4371],
5: [781, 1541, 5461, 5611, 7813],
6: [217, 481, 1111, 1261, 2701],
7: [25, 325, 703, 2101, 2353, 4525],
8: [9, 65, 481, 511, 1417, 2047],
9: [91, 121, 671, 703, 1541, 1729]}

def ppTest(function):
"""
Test the function on a couple of pseudo primes numbers
Returns a string with all the results
True - passes the test
False - fails the test
"""
results = {}
status = "\nPseudo Primes Test:\n"

for key in __pseudoPrimes:
results[key] = functionTest(function, __pseudoPrimes[key])
status += "[" + passTest(results[key]) + "]: PseudoPrime #" \
+ str(key) + "\n"

return status

def carmiTest(function):
"""
Test the function on the carmichael numbers
Fermat and millerRabin usually fail this test
Returns a string with the result
True - passes the test
False - fails the test
"""
results = functionTest(function, __carmichael)
status = "\nCarmichael Test:\n"
status += "[" + passTest(results) + "]: Carmichael"

return status

def functionTest(function, src):
"""
Take a function and a source list of numbers to test on the function
Returns a list of tuples (the number tested, it's own result)
"""
results = []
for n in src:
results.append((n, function(n)))

return results

def passTest(results):
"""
For Prime testing functions, in order to pass the test,
the results should be a list of False
Return OK when pass the test, FAIL otherwise
"""
for n in results:
if n[1]:
return "FAIL"

return " OK "

0 comments on commit cb8168a

Please sign in to comment.