Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
R
RSA
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
nicolas.albanesi
RSA
Commits
406bc4c3
Commit
406bc4c3
authored
3 years ago
by
jonas.stirnema
Browse files
Options
Downloads
Patches
Plain Diff
Update prime.py
parent
ff629db3
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/prime.py
+37
-5
37 additions, 5 deletions
src/prime.py
with
37 additions
and
5 deletions
src/prime.py
+
37
−
5
View file @
406bc4c3
from
math
import
*
"""
Check wether a number is prime or not
def
is_prime_number
(
number
):
"""
Check wether a number is prime or not
Bruteforcing method
"""
def
isPrimeNumber
(
number
):
"""
from
math
import
floor
,
sqrt
if
number
==
1
:
# 1 is not prime
return
False
if
number
==
2
:
# 2 is a prime number
...
...
@@ -17,3 +16,36 @@ def isPrimeNumber(number):
return
True
def
is_probably_prime
(
n
,
k
):
"""
Check wether a number is probably prime
Miller Rabin Primality Test
V2
"""
from
random
import
randint
if
n
==
1
:
# 1 is not prime
return
False
if
n
==
2
:
# 2 is a prime n
return
True
if
not
n
&
0x01
:
# even numbers are not prime
return
False
# Get the number of witness we need
# Number of trailing zeroes in the binary representation of n-1
# n - 1 = 2**n * n
s
=
((
n
-
1
)
&
-
(
n
-
1
)).
bit_length
()
-
1
d
=
int
((
n
-
1
)
/
2
**
s
)
for
i
in
range
(
1
,
n
):
x
=
2
+
randint
(
1
,
n
-
4
)
y
=
pow
(
x
,
d
,
n
)
if
y
!=
1
and
y
!=
n
-
1
:
for
r
in
range
(
1
,
s
-
1
):
y
=
pow
(
y
,
2
,
n
)
if
y
==
n
-
1
:
break
if
y
!=
1
:
return
False
return
True
# Probably Prime
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment