From 8c258ef6c08f7f84d5519115b48de318c2b81c01 Mon Sep 17 00:00:00 2001 From: Ehoneah Obed Date: Sat, 17 Sep 2022 22:36:57 +0000 Subject: [PATCH] Update solution --- rsa | 80 +++++++++++++++++++++++++++++++++++++------------------------ 1 file changed, 49 insertions(+), 31 deletions(-) diff --git a/rsa b/rsa index 4f68f9f..cf7c5ea 100755 --- a/rsa +++ b/rsa @@ -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 + where 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()