r/dailyprogrammer • u/jnazario 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
21
u/chunes 1 2 Oct 03 '16 edited Oct 03 '16
Befunge-93
8
u/RedFerretBob Oct 03 '16
Here is my Java solution and first submission:
(The classes are in separate files)
8
u/Lusaron Oct 15 '16 edited Oct 15 '16
x86_64 assembly (on Windows).
Reads a number from the command line (max length of 12 (arbitrary, slightly longer than 479001600)).
Aborts if anything other than a number is found (not including \r\n).
includes.h Gist:
https://gist.github.com/anonymous/c5ed0b007fb968d0818ecf48ecb4de5a
Build script Gist:
https://gist.github.com/anonymous/5f81d1cd0b170367ef4fa78f20877dea
7
u/spirit_rose_a_metre Oct 03 '16
Python 3.5
I found it easier to calculate each factorial and check with the input along the way. Might be less efficient, though.
I made a twitter bot with IFTTT.com that tweets every time a new challenge is out!
1
u/jnazario 2 0 Oct 03 '16
Fsharp Solution
4
u/JakDrako Oct 05 '16
VB.Net
Output
7
u/nwsm Oct 03 '16 edited Oct 03 '16
+/u/CompileBot Java
1
u/[deleted] Oct 03 '16
3
u/narcodis Oct 03 '16 edited Oct 03 '16
Javascript : nice-looking solution
Javascript: gross-looking but shorter solution
Output
3
u/Bizarre_Monkey Oct 03 '16
C
Output:
3
u/Zebrofish Oct 03 '16 edited Oct 03 '16
Never submitted before and I would appreciate feedback. I know there has to be a better way to check if a number is a factorial rather than generating a list and checking if the input is in the list. I'm going to try and figure it out, but for now this works!
3
u/partlyPaleo Oct 04 '16 edited Oct 04 '16
Using dc(1)
With comments to make it more understandable.
2
u/Lux01 0 0 Oct 03 '16
A quick and dirty Clojure solution
2
u/StopDropHammertime Oct 03 '16 edited Oct 04 '16
F#: Now updated to handle 0, 1, and negative values
2
u/gabyjunior 1 2 Oct 03 '16 edited Oct 03 '16
bc script, outputs 0 instead of NONE. Doing multiply instead of divide.
Output
2
u/skeeto -9 8 Oct 03 '16
C
2
u/TomDLux Oct 03 '16
2
u/[deleted] Oct 03 '16
Pascal:
2
u/MaDxEyE Oct 03 '16
C++
2
u/_Skitzzzy Oct 03 '16
2
u/schulzsebastian Oct 03 '16 edited Oct 03 '16
Go
would appreciate feedback, that's my first time with go!
2
u/Stormbreaker0295 Oct 06 '16 edited Oct 06 '16
A very simple and straightforward Java solution that allows users to input their own integers, also my first submission! Feel free to leave any comments and yes, I do know I didn't manually input the challenge values, also that I could have used a class, I just forgot how to organize one.
2
u/nonameleftover Oct 19 '16
Java, and my first submission.
I am just a beginner so I am very open to criticism.
I couldn't figure out how to spit out the correct factorial without subtracting 1 from it (line 11), and I am just beginning to understand classes and OOP so any criticism or advice regarding that is appreciated.
1
u/marchelzo Oct 03 '16
Ty
1
u/KeinBaum Oct 03 '16
Scala
1
u/Zeraific Oct 03 '16 edited Oct 03 '16
PHP
1
u/glenbolake 2 0 Oct 03 '16 edited Oct 03 '16
Scala with pattern matching
+/u/CompileBot scala
1
u/Captain___Obvious Oct 03 '16
Poorly implemented Scheme:
1
u/unfallenrain20 Oct 03 '16
+/u/CompileBot Python 3
1
u/janibus75 Oct 03 '16
Java
1
u/kjr1995 Oct 03 '16
A quick and easy python3 script.
1
u/rnda Oct 03 '16
Ruby
1
u/totallygeek Oct 03 '16
Bash
Output
1
u/Scroph 0 0 Oct 03 '16
Straightforward C++11 solution :
1
u/5k17 Oct 03 '16
Factor
1
u/Minolwa Oct 03 '16 edited Oct 03 '16
Scala
1
u/rnda Oct 03 '16
Ruby
some kind of recursion
1
u/Zambito1 Oct 03 '16 edited Oct 03 '16
Java
Not as efficient as other posts, I wrote this before looking at how other people did it.
+/u/CompileBot Java
1
u/[deleted] Oct 03 '16
C++
Yea pretty sure there are nicer ways to do this but i achieved the desired output assuming the input from the user plays nice.
1
u/[deleted] Oct 03 '16
Go solution.
Output
1
u/Wiggledan Oct 03 '16 edited Oct 03 '16
C89
1
u/phenomaks Oct 03 '16
C++ Solution
1
u/schulzsebastian Oct 03 '16
Python
1
u/staszekstraszek Oct 03 '16 edited Oct 03 '16
Python. I am a fresh beginner, but here is mine:
edit: also I would love a hint or 2
1
u/UnicornJoey Oct 03 '16
C++
int getFactorial(int factorial) {
}
int main(){
}
1
u/watchboy Oct 03 '16
+/u/CompileBot Rust
Input:
1
u/MoltenCookie Oct 03 '16
Python3
Some recursive made the problem a whole lot easier
1
u/rnda Oct 03 '16
Rust
Recently I started to learn Rust, and most of the time I don't know what I'm doing, so any feedback appreciated.
1
u/urielsalis Oct 03 '16 edited Oct 03 '16
Java(and first submission)
+/u/CompileBot Java
1
u/Specter_Terrasbane Oct 03 '16 edited Oct 04 '16
Python 2.7
1
u/[deleted] Oct 03 '16
Python 3.5
1
u/moeghoeg Oct 03 '16
Racket:
1
u/[deleted] Oct 03 '16
+/u/CompileBot R
1
u/[deleted] Oct 03 '16
Java (First submission!) Would love feedback
import java.util.Scanner; public class ReverseFactorial {
1
u/[deleted] Oct 04 '16
My solution in Python 3. I'm more or less a complete beginner but this was really easy for me as well.
By the way, the spacing might be off on the solution. I couldn't figure out how to do it at first.
1
u/dauntless26 Oct 04 '16
Python 3
1
u/free_elvis Oct 04 '16
Bash script
output
1
u/totallygeek Oct 04 '16
Python
1
u/daegren Oct 04 '16
Here is my solution in Go :) also here: https://github.com/daegren/daily_programmer/tree/master/286
Test:
and Main (you'll have to fix the imports)
1
u/milnak Oct 04 '16
Windows batch script
1
u/janek_jurec Oct 04 '16
Python3
1
u/Boxtel Oct 04 '16 edited Oct 04 '16
C# Made two solutions to this problem.
1
u/kykoobg Oct 04 '16
var input = document.getElementById('input'); var output = document.getElementById('output');
function checkInput(){ var userInput = prompt("Enter a single integer and I will return it's factorial: ");
1
u/vurhd Oct 04 '16
Here is my solution in JAVA
1
u/minikomi Oct 04 '16 edited Oct 04 '16
REBOL:
In REPL:
I want to learn:
1
u/annoir Oct 04 '16
In Python 3.5:
1
u/MXCIV Oct 04 '16
Java
1
u/leosek Oct 04 '16
C solution:
1
u/lop3rt Oct 04 '16
Ruby.
Looking for feedback on:
I believe I have the logic down, and I'm actually just wondering if the way I spread it out into 3 different methods is "proper" / good practice.
1
u/[deleted] Oct 04 '16
C# (call this with your number and div = 2):
1
u/BritainRitten Oct 04 '16
JavaScript: Recursive. IMO, the
unfactorial-finding should be separated from the displaying of the result.1
u/MaxFrost Oct 04 '16
In Powershell with recursion, because why not.
1
u/HerrNieschnell Oct 04 '16 edited Oct 05 '16
Feedback welcome, first time submission :)
+/u/CompileBot Python 3
1
u/HoisinDuck Oct 04 '16 edited Oct 04 '16
My solution in Python 2.7; feedback and critique welcome:
1
u/clutron Oct 04 '16
My Solution Java
1
u/_dd97_ Oct 04 '16 edited Oct 05 '16
vb.net
output:
1
u/ddek Oct 04 '16
C++, fairly roundabout way
1
u/Zync Oct 04 '16
Python 3.4
1
u/karrash76 Oct 04 '16
Hi, I'm trying to learn Java after more than 15 years w/o programming and I arrived here looking for programming tips and I love this thread!! ;) Please, review my code, I'll accept your corrections
1
u/primaryobjects Oct 04 '16
R
Demo | Gist
1
u/spamburghlar Oct 04 '16
Java; This challenge lends itself to a recursive solution.
Output:
5! = 120 NONE = 150 10! = 3628800 12! = 479001600 3! = 6 NONE = 18
1
u/HelloYesThisIsDuck Oct 05 '16
First submissions, since I am learning Python 3. (I know, too many comments.)
+/u/CompileBot Python 3
1
u/madole Oct 05 '16
Simple Javascript solution
1
u/wtrevino Oct 05 '16
Python 3.5 with recursion:
Output:
1
u/ken_umali Oct 05 '16 edited Oct 05 '16
Java solution (my first submission):
Separate class:
1
u/thepizzagui Oct 05 '16
Fairly new to python and first time poster, heres my solution in python:
1
u/XiiencE Oct 05 '16
Python 3
1
u/Piolhituh Oct 05 '16
Here is my python 3.5 solution:
1
u/Grafeno Oct 05 '16 edited Oct 05 '16
C++, started learning about 2 weeks ago (my first real programming language), I probably needlessly complicated things
Also added handling of "1", most code here seems to return "NONE" for 1 (unless I'm missing something).
1
u/[deleted] Oct 05 '16
Hi There. I primarily work in C/C++ but I'm acquainting myself with Java so here's my Java attempt. Feedback appreciated
}
and the supporting class
}
1
u/[deleted] Oct 05 '16 edited Oct 05 '16
Python 3. Looking for feedback, thanks!
1
u/smapti Oct 05 '16 edited Oct 05 '16
C++ using recursion
OUTPUT
1
u/Cl0v3 Oct 05 '16 edited Oct 05 '16
INPUT:
OUTPUT:
1
u/Bourbaki88 Oct 05 '16
R
And the output:
1
u/DrumsXgamer Oct 05 '16
+/u/CompileBot Python 3
1
u/bohuim Oct 05 '16
Swift 3
cmd+f "swift" to see if anyone else did it in swift
0 results :[
Thread is 2 days old but here's my take!
main is longer than the actual function, since I wrote it to take in continuous input until empty line.
1
u/chunkycatvomit Oct 06 '16
Rust!
output
1
u/aicss Oct 06 '16
Python
1
u/RiceCake6 Oct 06 '16
+/u/CompileBot Python 3
1
u/zokker13 Oct 06 '16
Some C++ attempt:
1
u/weekendblues Oct 06 '16
Just a quick C solution.
1
u/smapti Oct 06 '16
Python 2.7 recursive solution. First time with Python, feedback welcome.
OUTPUT
1
u/Ellestini Oct 06 '16
Python: New to python
1
u/lotbr Oct 06 '16 edited Oct 06 '16
Factor
Just trying but it does something...
Output:
1
u/[deleted] Oct 06 '16
Java (and my first submission):
1
u/[deleted] Oct 06 '16
Here's my Ruby and first submission:
1
u/pedantik Oct 07 '16
Go
Output:
1
u/AestheticMemeGod Oct 07 '16
My first /r/DailyProgrammer submission in C.
1
u/forfunc Oct 07 '16 edited Oct 07 '16
Python 2.7
Output:
1
u/[deleted] Oct 07 '16
C++
The reverseFactorial function just returns the factorial or -1 if it is not possible.
For the basic formatting I used a switch case checking for the -1.
With the main function calling the printReverseFactorial function as follows:
1
u/Valvinar Oct 07 '16
This is my first submission and I did it in Java to familiarize myself with it. Any feedback is welcome.
Overall this is not much different than RedFerretBob. Originally I did it all in one class but decided I should just go all the way.
Java solution
(I have to do this to give each class it's own spoiler box?)
1
u/0xC4 Oct 08 '16
Python 3
Output:
1
u/[deleted] Oct 08 '16
Here's my attempt in Python 3
1
u/xtcriott Oct 08 '16 edited Oct 08 '16
vb.net -- first crack at it. Any tips welcome.
1
u/ganska_bra Oct 08 '16
** Rust **
if only loops could return something else than
()1
u/mbdxjst2 Oct 08 '16
Brand new coder, first attempt in Python. I would love feedback if you have the time.
1
u/[deleted] Oct 08 '16 edited Oct 08 '16
Here's my Scheme solution (it is also my first submission here :)):
1
u/MrShears Oct 08 '16
Here's my python 3.5 solution:
1
u/fapencius Oct 08 '16
Python3:
1
u/[deleted] Oct 08 '16 edited Oct 08 '16
A recursive Clojure solution:
1
u/zellisgoatbond Oct 08 '16
Python 2.7, first submission:
1
u/bitwisecomparison Oct 09 '16
Here is my Python 3.x solution and unittests
import sys
class Factorial(object):
if name == 'main': args = sys.argv if len(args) > 2: print('''Too many arguments. You need only specify a number to return it's factorial.''') factorial = Factorial() print(factorial.reverse(factorial=int(args[1])))
import unittest from factorial import Factorial
class Tests(unittest.TestCase):
if name == 'main': unittest.main()
1
u/cyou2 Oct 09 '16
rust
1
u/dbaggins1 Oct 09 '16
Here is my C++11 solution and my first submission. Feedback is welcome.
1
u/SoupKitchenHero Oct 09 '16
Python 3.5.2
Output
1
u/Mister_Spacely Oct 10 '16 edited Oct 10 '16
My C code, using recursion.
main.c
inputFile.txt
outputFile.txt
1
u/mikeycodes Oct 10 '16 edited Oct 10 '16
First time posting here, decided to start trying to learn Go. Imagine the implementation has a lot of flaws but it's all a learning process :P
1
u/Ruchka135 Oct 10 '16
Python 2.7
Challenge Output:
Get reverse factorial for > 3628800
10
Get reverse factorial for > 479001600
12
Get reverse factorial for > 6
3
Get reverse factorial for > 18
NONE!
1
u/MusicalCoder Oct 10 '16
Python 3
1
u/KewaiiGamer Oct 10 '16
Java
This is my first submission
1
u/astinog Oct 10 '16
C++
1
u/hero_complex_volcano Oct 10 '16
Javascript: perhaps a bit bigger that most answers but it works
1
u/futbolbrasil Oct 10 '16
javascript
1
u/[deleted] Oct 11 '16
First submission, tried it out in Elixir:
Suggestions welcome.
1
u/punkcat Oct 12 '16 edited Oct 12 '16
Python 3
1
u/mjanmohammad Oct 12 '16
Python 2 +/u/CompileBot Python
1
u/MalsR1 Oct 12 '16
Java solution with some tests
public class ReverseFactorialFinder {
}
@RunWith(Parameterized.class) public class ReverseFactorialFinderTest {
}
1
u/PentaProgrammer Oct 13 '16
Python Will ouput 1 when input is 1 as opposed to 0. Usage: invFactorial(<value>)
More readable version
1
u/Frosthrone Oct 13 '16 edited Oct 15 '16
Java
}
1
u/Domoman1 Oct 14 '16
Python 3 - my first submission so appreciate any comments! :)
1
u/fazxyll Oct 14 '16 edited Oct 14 '16
+/u/CompileBot Java
1
u/xilni Oct 15 '16
Swift
1
u/Doggamnit Oct 16 '16 edited Oct 16 '16
Python 2.7
Output
One Liner(Just thought this was interesting to look at - https://github.com/csvoss/onelinerizer
Output
1
u/viralwarrior012 Oct 16 '16 edited Oct 16 '16
Here's my solution in javascript:
1
u/speedster217 Oct 16 '16 edited Oct 16 '16
Haskell:
1
u/chunes 1 2 Oct 17 '16 edited Oct 17 '16
Factor
Output:
I just started learning this language yesterday so it's undoubtedly bad.
1
u/[deleted] Oct 17 '16 edited Oct 17 '16
PYTHON3
Quick and simple solution, the program calculates factorials from 1 and check for every factorial is it matches with input.
Example of output:
1
u/davisCEO Oct 17 '16
Javascript - my first code after beginner level learning for 1 day
<html> <head> </head>
</html>
1
u/enquirist Oct 18 '16
PHP -- Accepts user input on a basic form and checks that input is valid. Contains both a factorial and a reverse factorial function (although the former causes the program to crash if the input is too big). Coding newbie here. Welcome comments.
Sample Output:
1
u/[deleted] Oct 19 '16 edited Oct 19 '16
C#: Accepting criticism, still learning
1
u/thethecoucs Oct 20 '16
static void Main(string[] args) { Console.Write("La respuesta es: " + reverseFactorial(120) + "\n"); Console.Write("La respuesta es: " + reverseFactorial(150) + "\n"); Console.Write("La respuesta es: " + reverseFactorial(3628800) + "\n"); Console.Write("La respuesta es: " + reverseFactorial(479001600) + "\n"); Console.Write("La respuesta es: " + reverseFactorial(6) + "\n"); Console.Write("La respuesta es: " + reverseFactorial(18) + "\n"); Console.ReadKey(true); }
1
u/Tetsumi- 1 0 Oct 21 '16
Racket
1
u/[deleted] Oct 22 '16
Python 3
Would appreciate feedback. I probably should have used a while loop, but couldn't quite get it to work, and I thought that shipping shitty was better than not shipping at all.
1
u/iLoveAGoodIDea Oct 24 '16
c++ im still learning, dont know why it doesnt work for 13 or higher #include <iostream>
1
u/ruler01 Oct 24 '16
Ruby
its been a while since programming :)
1
u/mr_loveboat Oct 24 '16
Python
1
u/[deleted] Oct 24 '16
C#
My very first program in C#, hope it's not too dirty.
1
u/ASpueW Oct 24 '16
Rust
1
u/HansMannibus Oct 24 '16
JavaScript
1
u/calithe Oct 25 '16
Java:
1
u/qwertyegg Oct 26 '16 edited Oct 26 '16
Java
output:
1
u/Mark2599 Oct 27 '16 edited Oct 27 '16
Solution in java
This is my first solution task from dailyprogrammer, what do you think about my code?
1
u/gym-jim Oct 28 '16
Python
Second ever post here
Second ever "program" ever written in any language
Please give me any and all feedback you may have.
Thanks everyone!
1
u/[deleted] Oct 28 '16
My C Solution and first submission! :) Would like some feedback!
1
u/[deleted] Oct 29 '16
Python 3
1
u/mycroftholmess Oct 30 '16
This is my solution in Swift
1
u/AncientLion Oct 31 '16 edited Nov 04 '16
First time/ C++
1
u/JaicompDeveloper Nov 02 '16
My answer in Ruby:
1
u/Goger63 Nov 04 '16 edited Nov 04 '16
C# Code - First Time submitting
Any ideas are appreciated
1
u/theheartbreakpug Nov 06 '16
+/u/CompileBot Java
I like to keep things straight forward. Tis my first post, studying for technical interviews and I never do these kind of puzzles =/
1
u/juanchi35 Nov 07 '16
C++
1
u/yoho139 Nov 07 '16 edited Nov 09 '16
C, golfed (109B)
And expanded, with an explanation
1
u/[deleted] Nov 08 '16
Python 3
1
u/knifepegasus Nov 11 '16 edited Nov 11 '16
My submission written in C
1
u/sickapps420 Nov 13 '16
PL/SQL feedback is welcome, this'll be my first post on here.
1
u/tadm123 Nov 14 '16
Python 3.5
Output:
1
u/[deleted] Nov 17 '16
Java (First submission from a noob programmer, be gentle)
1
u/[deleted] Nov 21 '16
C I was wondering: wouldn't it be faster to interrupt the calculation with really, really big numbers? Well, the average difference I measured was only 1 millisecond between the two calculations. It would probably need a way bigger sample size (1000 or so?). So yeah, there are two methods. The one that assumes it is a factorial (correct) is slower with big numbers when it is incorrect, but faster when the number actually is a valid factorial number.
Sponsored by a boring lecture.
1
u/dadas2412 Nov 22 '16
C++
include "std_lib_facilities_4.h"
void isitfact(int abc){
}
int main(){
}
1
u/doublec Nov 29 '16
Prolog:
It works both forwards and backwards:
1
u/Yeah22 Dec 01 '16
Java took a few courses in Java a few years back, haven't programmed really since then up until about a month ago. Any helpful comments on my code are GREATLY appreciated! Tried to make it as simple as possible.
1
u/dariosilva88 Dec 02 '16
Haskell
OUTPUT
1
u/[deleted] Dec 04 '16 edited Dec 04 '16
4tH - Still brand new to 4tH so I couldn't add any function to tell whether it's not a reverse factorial. It does work if it actually is a reverse factorial though.
1
u/lyinsteve Dec 04 '16
This is my solution in trill, my hobby language