Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
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
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
a0bcc27e
Commit
a0bcc27e
authored
Oct 10, 2009
by
Georg Brandl
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Modernize factorisation demo (mostly augassign.)
parent
3d072c95
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
19 additions
and
17 deletions
+19
-17
Demo/scripts/fact.py
Demo/scripts/fact.py
+19
-17
No files found.
Demo/scripts/fact.py
View file @
a0bcc27e
...
...
@@ -9,39 +9,41 @@ import sys
from
math
import
sqrt
def
fact
(
n
):
if
n
<
1
:
raise
ValueError
# fact() argument should be >= 1
if
n
==
1
:
return
[]
# special case
if
n
<
1
:
raise
ValueError
(
'fact() argument should be >= 1'
)
if
n
==
1
:
return
[]
# special case
res
=
[]
# Treat even factors special, so we can use i
= i+
2 later
while
n
%
2
==
0
:
# Treat even factors special, so we can use i
+=
2 later
while
n
%
2
==
0
:
res
.
append
(
2
)
n
=
n
//
2
n
//=
2
# Try odd numbers up to sqrt(n)
limit
=
sqrt
(
float
(
n
+
1
)
)
limit
=
sqrt
(
n
+
1
)
i
=
3
while
i
<=
limit
:
if
n
%
i
==
0
:
if
n
%
i
==
0
:
res
.
append
(
i
)
n
=
n
//
i
n
//=
i
limit
=
sqrt
(
n
+
1
)
else
:
i
=
i
+
2
i
+=
2
if
n
!=
1
:
res
.
append
(
n
)
return
res
def
main
():
if
len
(
sys
.
argv
)
>
1
:
for
arg
in
sys
.
argv
[
1
:]:
n
=
eval
(
arg
)
print
n
,
fact
(
n
)
source
=
sys
.
argv
[
1
:]
else
:
source
=
iter
(
raw_input
,
''
)
for
arg
in
source
:
try
:
while
1
:
n
=
input
()
print
n
,
fact
(
n
)
e
xcept
EOFError
:
p
ass
n
=
int
(
arg
)
except
ValueError
:
print
arg
,
'is not an integer'
e
lse
:
p
rint
n
,
fact
(
n
)
if
__name__
==
"__main__"
:
main
()
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