CyrusMusyoka / RSA-Factoring-Challenge

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

RSA Factoring Challenge

how it works:

  1. from sys import argv: This line imports the argv function from the sys module, which helps access command-line arguments.

  2. with open(argv[1]) as f:: Opens the file specified in the command line arguments for reading (argv[1] is the second argument, typically a filename).

  3. for line in f:: Loops through each line in the file.

  4. num = int(line): Converts each line (considered as a string) into an integer and assigns it to the variable num.

  5. print("{:d}=".format(num), end=""): Prints the number without a newline character but ends with an equals sign.

  6. if num % 2 == 0:: Checks if the number is even. If so, it prints the number as the product of 2 and half the number.

  7. for i in range(3, num, 2):: Initiates a loop to check divisors from 3 up to the number (skipping even numbers).

  8. if num % i == 0:: Checks if the number is divisible by i. If so, it further checks if the quotient is also divisible only by odd numbers (i.e., it's a prime factorization).

  9. factor = num//i: Calculates the quotient of num divided by i.

  10. for j in range(3, factor, 2):: Initiates another loop to check if the quotient (factor) or the current divisor i is divisible by any odd number (checking for non-prime factors).

  11. if factor % j == 0 or i % j == 0:: If any of the numbers are divisible by j, it breaks out of the loop to avoid printing non-prime factors.

  12. print("{}*{}".format(factor, i)): Prints the prime factors found if they pass the prime factorization check.

This code reads numbers from a file, checks for their prime factors, and prints them in a specific format, showing the factorization of each number.

About


Languages

Language:Shell 100.0%