Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
number_factoring.py
#!/usr/bin/env python3
#
# Version: v1.1.3
# License: MIT License
# Copyright (c) 2020-2021 Hunter T.
#
########################################################################################
"""
Take a user provided integer and find all of the possible factors for that
number.
:note: Does not find the factors of negative numbers.
"""
####[ Functions ]#######################################################################
def finding(x: int) -> None:
"""Find all of the factors of a given integer.
:param x: User provided integer.
"""
factors = []
i = 0
while i < x:
i += 1
if x % i == 0:
factors.append(i)
first = 0
last = len(factors) - 1
even_odd = len(factors) % 2
if even_odd == 1:
factor_length = int(len(factors) / 2 + 1)
else:
factor_length = int(len(factors) / 2)
## Print the corresponding factors side by side.
for i in range(factor_length):
print("{}*{}".format(factors[first], factors[last]))
first += 1
last -= 1
print("\nA total of {} factors of {}".format(len(factors), num))
####[ Main ]############################################################################
## Make sure that user input is valid and that any ValueErrors are caught.
while True:
try:
num = int(input("Enter a number: "))
except ValueError:
print("Invalid input: only numbers are accepted as input\n")
continue
if num < 0:
print("Invalid input: only positive numbers are accepted as input\n")
continue
break
print("\nFactors of {} are: ".format(num))
finding(num)
@StrangeRanger

This comment has been minimized.

Copy link
Owner Author

@StrangeRanger StrangeRanger commented Jun 7, 2021

Project Tracker

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment