Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
projecteuler
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
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
Taddeüs Kroes
projecteuler
Commits
13a15978
Commit
13a15978
authored
12 years ago
by
Taddeus Kroes
Browse files
Options
Downloads
Patches
Plain Diff
Coninued working on problem 243, making it more efficient
parent
c80984d8
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
problem243.py
+43
-19
43 additions, 19 deletions
problem243.py
with
43 additions
and
19 deletions
problem243.py
+
43
−
19
View file @
13a15978
#!/usr/bin/env python
def
is_resilient
(
n
,
d
):
if
n
==
1
:
return
True
from
__future__
import
division
from
math
import
sqrt
,
ceil
for
div
in
xrange
(
2
,
min
(
n
,
d
)
+
1
):
if
not
n
%
div
and
not
d
%
div
:
return
False
def
primes_until
(
n
):
"""
Sieve of Eratosthenes
"""
lst
=
[
False
]
*
n
i
=
2
return
True
while
i
<
n
:
if
not
lst
[
i
]:
yield
i
def
resilience
(
d
):
r
=
0
for
j
in
xrange
(
i
,
n
,
i
):
lst
[
j
]
=
True
i
+=
1
def
times
(
a
,
b
):
return
a
*
b
def
div
(
m
,
n
):
return
not
divmod
(
n
,
m
)[
1
]
MAX
=
2000000
all_primes
=
list
(
primes_until
(
MAX
))
for
n
in
xrange
(
1
,
d
):
if
is_resilient
(
n
,
d
):
r
+=
1
def
primes
(
n
):
for
p
in
all_primes
:
if
p
>=
n
:
raise
StopIteration
return
r
/
(
d
-
1.
)
yield
p
def
phi
(
n
):
return
reduce
(
times
,
iter
(
1
-
1
/
p
for
p
in
primes
(
n
)
if
div
(
p
,
n
)),
n
)
def
resilience
(
d
):
return
phi
(
d
)
/
(
d
-
1
)
smallest
=
15499.
/
94744
d
=
2
while
True
:
if
resilience
(
d
)
<
smallest
:
print
d
break
try
:
while
resilience
(
d
)
>=
15499
/
94744
:
d
+=
1
if
d
==
MAX
:
print
'
maximum reached:
'
,
break
except
KeyboardInterrupt
:
print
'
interrupted:
'
,
d
+=
1
print
d
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