r/dailyprogrammer • 2 0 • Oct 03 '16

[2016-10-03] Challenge #286 [Easy] Reverse Factorial

Description

Nearly everyone is familiar with the factorial operator in math. 5! yields 120 because factorial means "multiply successive terms where each are one less than the previous":

5! -> 5 * 4 * 3 * 2 * 1 -> 120

Simple enough.

Now let's reverse it. Could you write a function that tells us that "120" is "5!"?

Hint: The strategy is pretty straightforward, just divide the term by successively larger terms until you get to "1" as the resultant:

120 -> 120/2 -> 60/3 -> 20/4 -> 5/5 -> 1 => 5!

Sample Input

You'll be given a single integer, one per line. Examples:

120
150

Sample Output

Your program should report what each number is as a factorial, or "NONE" if it's not legitimately a factorial. Examples:

120 = 5!
150   NONE

Challenge Input

3628800
479001600
6
18

Challenge Output

3628800 = 10!
479001600 = 12!
6 = 3!
18  NONE
121 Upvotes

295 comments sorted by

View all comments

Show parent comment

0

u/[deleted] Oct 03 '16

[deleted] — view deleted comment

2

u/Zebrofish Oct 03 '16

Thank you this is exactly the kind of feedback I was hoping. I forgot about the all the int()s I had left hanging around, but thanks for the tip.

def rev_fact(n):
    x = 0
    if n == 1:
        print("The reverse factorial is: 0 and 1")
        return
    while n > 1:
        x += 1
        n = n / x
    if n == 1:
        print("The reverse factorial is: " + str(x))
    else:
        print("Your number isn't a factorial product!")
rev_fact(3628800)
rev_fact(479001600)
rev_fact(6)
rev_fact(18)

2

u/Zebrofish Oct 03 '16

output

rev_fact(6)
rev_fact(18)
The reverse factorial is: 10
The reverse factorial is: 12
The reverse factorial is: 3
Your number isn't a factorial product!
The reverse factorial is: 0 and 1