I like what you did here @rydan, the only issue is I like to break things... So I tried to get it to factor a number over 150 (on the lab site) and it freezes so me being me I was like how can we get it to factor any number.

The below will do it by breaking down all then numbers and multiplying them together then return a list of approximately 1/2 the size, it keeps doing this till the list sizes are under 14. I time tested mine vs yours.

On anything smaller than 1000 we are matched almost neck in neck with you taking the lead mostly over that though my code starts to break through quickly.

It was a fun little project, thanks for giving me something to do for a bit! There are several different ways to do this but this one was the fastest to do. You should try some of the other ways. Again thank you!

My Code on my machine:

**100592 function calls in 8.406 seconds** @ 2750 Count 10,311 kb file

Your Code on my machine:

**13759 function calls in 10.284 seconds** @2750 Count 10,311 kb file

As you can clearly see I do roughly 9x the function calls in almost a whole 2 secs less time. It get's more sever the large the number.

Again,

Mine:

**132795 function calls in 17.598 seconds** @3500 Count 17,323 kb file

Yours:

**17509 function calls in 21.418 seconds** @3500 Count 17,323 kb file

Again:

Mine:

**179307 function calls in 37.331 seconds** @4500 Count 29,708 kb file

Yours:

**22509 function calls in 46.765 seconds** @4500 Count 29,708 kb file

**MY CODE:**

```
import cProfile
def factorial(number):
"""
This function calculates the factorial of the given number.
Numbers over 1000 can take over 10secs!
:param number: Number to calculate
:return: Returns the value of the number calculated
"""
def shrink_list(lst):
"""
This sub-function takes all the numbers in the factorial and multiplies
every other even one to ever odd one. If the amount of numbers is odd
it takes the last one off and adds it to the end after the multiplication.
:param lst: List of numbers to multiply
:return: Returns a list of numbers after shortening and multiplication.
"""
if len(lst) % 2 == 0:
return [(item1 * item2) for item1, item2 in zip(lst[0::2], lst[1::2])]
else:
new_lst = lst[:-1]
new_lst = [(item1 * item2) for item1, item2 in zip(new_lst[0::2], new_lst[1::2])]
new_lst.append(lst[-1])
return new_lst
# This creates a list of all the numbers that need to be factored
numbers_to_multiply = [x for x in range(1, number + 1)]
valid = False # For our flow control
while not valid:
# This will continually call the sub-function till the list
# of numbers is less than 14.
numbers_to_multiply = shrink_list(numbers_to_multiply)
if len(numbers_to_multiply) < 14:
valid = True
return_value = 1
# This for loop finishes the multiplication on the remaining members.
for number in numbers_to_multiply:
return_value *= number
return return_value
def cf_range(num_range):
"""
This function saves the factorial to file. It also calls the function
ranging from 1 to the given number. So all factorials to the number are
saved to file. The computer has problems displaying any number
the system can not handle.
:param num_range: Range of numbers to be factored
"""
with open("factorial_of_%s.txt" % str(num_range), "w+") as file:
for number in range(1, num_range + 1):
file.write(str(factorial(number)) + "\n")
cProfile.run("cf_range(5000)")
```

**Your Code:**

```
import cProfile
def factorial(x):
fact = 1
try:
x = int(x)
while(x>0):
fact *= x
x-=1
return fact
except ValueError:
return ("Invalid Input")
def cf_range(num_range):
with open("other_factorial_of_%s.txt" % str(num_range), "w+") as file:
for number in range(1, num_range + 1):
file.write(str(factorial(number)) + "\n")
cProfile.run("cf_range(2750)")
```