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
Hide 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
#!/usr/bin/env python
def
is_resilient
(
n
,
d
):
from
__future__
import
division
if
n
==
1
:
from
math
import
sqrt
,
ceil
return
True
for
div
in
xrange
(
2
,
min
(
n
,
d
)
+
1
):
def
primes_until
(
n
):
if
not
n
%
div
and
not
d
%
div
:
""" Sieve of Eratosthenes """
return
False
lst
=
[
False
]
*
n
i
=
2
return
True
while
i
<
n
:
if
not
lst
[
i
]:
yield
i
def
resilience
(
d
):
for
j
in
xrange
(
i
,
n
,
i
):
r
=
0
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
):
def
primes
(
n
):
if
is_resilient
(
n
,
d
):
for
p
in
all_primes
:
r
+=
1
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
d
=
2
while
True
:
try
:
if
resilience
(
d
)
<
smallest
:
while
resilience
(
d
)
>=
15499
/
94744
:
print
d
d
+=
1
break
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