Skip to content

Commit

Permalink
Update solution for task 0
Browse files Browse the repository at this point in the history
  • Loading branch information
ehoneahobed committed Sep 17, 2022
1 parent 8a35bee commit f2a3e43
Showing 1 changed file with 23 additions and 26 deletions.
49 changes: 23 additions & 26 deletions factors
Original file line number Diff line number Diff line change
@@ -1,27 +1,24 @@
#!/usr/bin/python3
import sys


def factorize(num):
""" Generate 2 factors for a given number"""
factor1 = 2
while (num % factor1):
if (factor1 <= num):
factor1 += 1

factor2 = num // factor1
return (factor2, factor1)


if len(sys.argv) != 2:
sys.exit(f"Wrong usage: {sys.argv[0]} <file_path>")

filename = sys.argv[1]

file = open(filename, 'r')
lines = file.readlines()

for line in lines:
num = int(line.rstrip())
factor2, factor1 = factorize(num)
print(f"{num} = {factor2} * {factor1}")
from sys import argv
from math import sqrt

def factor():
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
sqn = int(sqrt(num))
if sqn % 2 == 0:
sqn += 1
for i in range(3, sqn + 1, 2):
if num % i == 0:
print("{}*{}".format(i, num//i))
break
if num % i != 0:
print("{}={}*1".format(num, num))


factor()

0 comments on commit f2a3e43

Please sign in to comment.