Skip to content

Commit

Permalink
Fixed maths is_probably_prime power int
Browse files Browse the repository at this point in the history
  • Loading branch information
sirfoga committed Aug 16, 2017
1 parent 8fef6b8 commit b4005f3
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 7 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@ All notable changes to this project will be documented in this file.
- duplicate primes matrix in maths module
- duplicate code in maths prime checking
- duplicate code in ml time series
- low primality checking in maths.Integer
### Fixed
- matplotlib dependencies
- Plot4d.plot canvas updater
- maths is_probably_prime power int
### Added
- charts module examples
- tests module examples

## 4.6.1
### Added
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

[![FOSSA Status](https://app.fossa.io/api/projects/git%2Bhttps%3A%2F%2Fgithub.com%2Fsirfoga%2Fpyhal.svg?type=shield)](https://app.fossa.io/projects/git%2Bhttps%3A%2F%2Fgithub.com%2Fsirfoga%2Fpyhal?ref=badge_shield) [![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)
[![Open Source Love](https://badges.frapsoft.com/os/v1/open-source.svg?v=103)](https://opensource.org/licenses/Apache-2.0)
[![Contributions welcome](https://img.shields.io/badge/contributions-welcome-brightgreen.svg?style=flat)](https://github.com/sirfoga/scrapebots/issues)
[![Contributions welcome](https://img.shields.io/badge/contributions-welcome-brightgreen.svg?style=flat)](https://github.com/sirfoga/pyhal/issues)


<!-- [![Stories in Ready](https://badge.waffle.io/sirfoga/hal.svg?label=ready&title=Ready)](http://waffle.io/sirfoga/hal) -->
Expand Down Expand Up @@ -48,6 +48,7 @@ Take a look [here](https://github.com/sirfoga/pyhal/blob/master/setup.py#L58) fo
You can take a look at [my other repository](https://github.com/sirfoga/pymisc/tree/master/misc): there are lots of implementations from various HAL modules.
Browse extra examples here:
- [`charts`](docs/examples/CHARTS.md) module
- [`tests`](docs/examples/TESTS.md) module


## Usage and documentation
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/CHARTS.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Charts
# hal.charts.plotter

## Plot3d.param()
```python
Expand Down
39 changes: 39 additions & 0 deletions docs/examples/TESTS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# hal.tests

## battery_test()
```python
from unittest import TestCase

from hal.maths.maths import Integer
from hal.tests.utils import battery_test, random_name


def is_prime(string):
return Integer(string).is_probably_prime()

class EasyTest(TestCase):
def test_primality(self):
tests = {
"2": True, # 2 is prime
"3": True, # 3 is also prime
"4": False, # 4 is NOT a prime (2 * 2)
"997": True, # should be prime
"4366114": False, # not a prime (divisible by 2)
"14553714337": True, # this should be prime
"58853781722270786163": False # not a prime (divisible by 3)
}

battery_test(
self.assertEquals, tests, is_prime
)
```

```bash
Ran 1 test in 0.000s

OK
```


## Questions and issues
The [github issue tracker](https://github.com/sirfoga/pyhal/issues) is **only** for bug reports and feature requests. Anything else, such as questions for help in using the library, should be mailed [here](mailto:sirfoga@protonmail.com).
7 changes: 2 additions & 5 deletions hal/maths/maths.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@ class Integer(object):
def __init__(self, string):
self.to_int = int(string)
self.to_string = string
self.is_probably_prime = (self.to_int % 2 != 0) and (
self.to_int in LOW_PRIMES)

def is_naive_prime(self):
"""
Expand Down Expand Up @@ -76,8 +74,7 @@ def is_probably_prime(self):
return False

# if all else fails, call rabin to determine if to_int is prime
self.is_probably_prime = self.test_miller_rabin(5)
return self.is_probably_prime
return self.test_miller_rabin(5)

return True

Expand Down Expand Up @@ -111,7 +108,7 @@ def test_miller_rabin(self, precision):
# -> prime
for _ in range(precision):
a = random.randrange(2, self.to_int - 1)
v = pow(a, s, self.to_int)
v = pow(int(a), int(s), self.to_int)
if v != 1:
i = 0
while v != (self.to_int - 1):
Expand Down

0 comments on commit b4005f3

Please sign in to comment.