Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
P
projecteuler
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Taddeüs Kroes
projecteuler
Commits
13a15978
Commit
13a15978
authored
Nov 27, 2012
by
Taddeus Kroes
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Coninued working on problem 243, making it more efficient
parent
c80984d8
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
43 additions
and
19 deletions
+43
-19
problem243.py
problem243.py
+43
-19
No files found.
problem243.py
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
))
def
primes
(
n
):
for
p
in
all_primes
:
if
p
>=
n
:
raise
StopIteration
for
n
in
xrange
(
1
,
d
):
if
is_resilient
(
n
,
d
):
r
+=
1
yield
p
return
r
/
(
d
-
1.
)
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
try
:
while
resilience
(
d
)
>=
15499
/
94744
:
d
+=
1
if
d
==
MAX
:
print
'maximum reached:'
,
break
except
KeyboardInterrupt
:
print
'interrupted:'
,
d
+=
1
print
d
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment