-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2b4a672
commit 8c258ef
Showing
1 changed file
with
49 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,53 @@ | ||
#!/usr/bin/python3 | ||
|
||
from sys import argv | ||
|
||
|
||
|
||
with open(argv[1]) as f: | ||
|
||
for line in f: | ||
|
||
num = int(line) | ||
|
||
print("{:d}=".format(num), end="") | ||
|
||
if num % 2 == 0: | ||
|
||
print("{}*2".format(num//2)) | ||
|
||
continue | ||
|
||
for i in range(3, num, 2): | ||
|
||
if num % i == 0: | ||
|
||
factor = num//i | ||
|
||
for j in range(3, factor, 2): | ||
|
||
if factor % j == 0 or i % j == 0: | ||
|
||
""" | ||
Factorize as many numbers as possible into a product of two smaller numbers. | ||
Usage: factors <file> | ||
where <file> is a file containing natural numbers to factor. | ||
One number per line | ||
You can assume that all lines will be valid natural numbers\ | ||
greater than 1 | ||
You can assume that there will be no empy line, and no space\ | ||
before and after the valid number | ||
The file will always end with a new line | ||
Output format: n=p*q | ||
one factorization per line | ||
p and q don’t have to be prime numbers | ||
See example | ||
You can work on the numbers of the file in the order of your choice | ||
Your program should run without any dependency: You will not be ablei\ | ||
to install anything on the machine we will run your program on | ||
Time limit: Your program will be killed after 5 seconds\ | ||
if it hasn’t finish | ||
Push all your scripts, source code, etc… to your repository | ||
""" | ||
# library to get arguments | ||
import sys | ||
|
||
|
||
# fn unpack number factorial | ||
def fc(): | ||
""" | ||
function fc to search file to convert number and format n=p*q | ||
""" | ||
try: | ||
revfile = sys.argv[1] | ||
with open(revfile) as f: | ||
for revnumber in f: | ||
revnumber = int(revnumber) | ||
if revnumber % 2 == 0: | ||
print("{}={}*{}".format(revnumber, revnumber // 2, 2)) | ||
continue | ||
i = 3 | ||
while i < revnumber // 2: | ||
if revnumber % i == 0: | ||
print("{}={}*{}".format(revnumber, revnumber // i, i)) | ||
break | ||
i = i + 2 | ||
if i == (revnumber // 2) + 1: | ||
print("{}={}*{}".format(revnumber, revnumber, 1)) | ||
except (IndexError): | ||
pass | ||
|
||
print("{}*{}".format(factor, i)) | ||
|
||
break | ||
# autostart | ||
fc() |