Calculate the factorial of a number without using recursion or loops

-15

0

The goal of this code golf is to create a program that calculates the factorial of a positive number, without using recursion or loops. The rules:

  1. Your program should prompt for input. This input will be a positive integer
  2. Your program should output the factorial of the given positive integer
  3. You are not allowed to use recursion or loops (including goto)
  4. You are not allowed to execute an external program
  5. You are not allowed to use a built-in function to calculate the factorial (including eval functions)
  6. Your program MUST work without network connection

Good luck!

ProgramFOX

Posted 2013-10-23T16:11:36.577

Reputation: 8 017

Question was closed 2017-06-18T15:19:33.387

This eliminates many languages. – ericw31415 – 2016-05-11T20:33:32.510

4im pretty sure this is impossible, u need some sort of looping (built-in or otherwise), unless u let trick looping/recursion. so you have to depend on a built-in looping function? its impossible in many languages.... – Math chiller – 2013-10-23T16:30:02.993

Actually, I've no idea whether it is possible or not, but perhaps someone finds a cool trick using bitwise operators. – ProgramFOX – 2013-10-23T16:32:47.617

i think the answer below is kinda cheating, it does loop just internally – Math chiller – 2013-10-23T16:33:38.717

2Just take a look how numbers are converted to strings, every programming language will loop. (Ok there could be the C equivalent with literal strings) – Johannes Kuhn – 2013-10-23T17:46:58.347

9And by the way: Restricting eval changes the rules, invalidates most of the answers, and is just like "ohh, they found an other way to solve this, forbid that too"... – Johannes Kuhn – 2013-10-23T18:53:05.330

18-1 for changing the question radically – Doorknob – 2013-10-23T20:54:10.683

3

@ProgramFOX This is impossible (wihtout adding abitiary limitations like 32-bit or defer the loop to the language). The problem you describe here is more or less equivalent to "Find a formula that calculates the factorial". Just look at the Γ-Function. While there are formulas that can calculate n!, they are too complex and involve either Π or , which can not be solved without loops/recursion.

– Johannes Kuhn – 2013-10-24T08:21:12.227

@JohannesKuhn: May be tcl´s time is valid? – sergiol – 2017-08-23T08:22:25.660

time is just a loop. – Johannes Kuhn – 2017-09-02T20:09:10.347

Answers

3

APL, 4 characters / GolfScript, 9 characters

APL:

×/⍳⎕

GolfScript:

~),1>{*}*

Howard

Posted 2013-10-23T16:11:36.577

Reputation: 23 109

And how does that GolfScript work? Isn't * applied to a block like eval? – Johannes Kuhn – 2013-10-30T07:30:07.393

5{*}* does a loop – Claudiu – 2014-10-16T16:36:58.313

38

Tcl, 82

puts [lindex {1 1 2 6 24 120 720 5040 40320 362880 3628800 39916800} [gets stdin]]

No eval, no recursion, no loops. As requested. Only works with 32-Bit integers.

Johannes Kuhn

Posted 2013-10-23T16:11:36.577

Reputation: 7 122

5but isn't 12! = 479001600 < 2^29? – Will Ness – 2015-05-25T07:54:57.017

23

Mathematica, 32

Integrate[x^Input[]/E^x,{x,0,∞}]

I hope I did everything correctly, otherwise please correct it.
I don't even have Mathematica.

If nothing else works, use the Gamma function.

Johannes Kuhn

Posted 2013-10-23T16:11:36.577

Reputation: 7 122

here's a test at WolframAlpha.

– Will Ness – 2015-05-25T08:01:04.127

Nice approach. It works fine. – DavidC – 2013-10-26T16:33:38.560

13

Mathematica 20

This should be easy to beat.

Times@@Range@Input[]

DavidC

Posted 2013-10-23T16:11:36.577

Reputation: 24 524

"you are not allowed to use a built-in function to calculate the factorial" – Math chiller – 2013-10-23T16:30:57.937

3@tryingToGetProgrammingStraight the built-in one would be Factorial – ssch – 2013-10-23T16:31:18.627

14@tryingToGetProgrammingStraight. I used three functions, Times, Apply (@@), and Range. – DavidC – 2013-10-23T16:31:22.033

7

R - 14 characters

prod(1:scan())

Gaffi

Posted 2013-10-23T16:11:36.577

Reputation: 3 411

+1 but I think it should be scan(n=1) – flodel – 2013-10-29T00:07:15.513

@flodel leaving out the n=1 saves space, and still allows the function to work, so long as your first input is the value to target, and the next is empty (press enter twice on the first value). – Gaffi – 2013-10-29T15:02:42.683

5

Bash 43

Using only internal commands:

read n;echo $((`eval "echo 1 '*'{1..$n}"`))

The inside is pretty much seq -s'*' $n

ssch

Posted 2013-10-23T16:11:36.577

Reputation: 223

Two small improvements: $[…] instead of $((…)) and \* instead of '*'. – manatwork – 2013-10-24T08:01:06.053

@manatwork Nice hadn't seen $[ ] before (probably because it's deprecated) – ssch – 2013-10-25T14:04:05.213

4

Ruby, 25 characters

p eval [*1..gets.to_i]*?*

Howard

Posted 2013-10-23T16:11:36.577

Reputation: 23 109

Or 21 characters if you assume the input will have no trailing line separator: http://pastebin.com/pMRDWn4v

– manatwork – 2013-10-24T08:06:58.553

3

Excel Formula, 32

=PRODUCT(ROW(INDIRECT("1:"&A1)))

Enter as an array formula, and the value to be factored should be in A1

it's 34 if you have to count the {} that excel will put around the formula when successfully entered with Ctrl+Shift+Enter

SeanC

Posted 2013-10-23T16:11:36.577

Reputation: 1 117

=PRODUCT(ROW(INDIRECT("1:"&ROW()))) to put in any cell, where the row number in which it's placed is the value to be factored. This way, the solution is self-contained and does not require any other references. (Not that your answer needed any improving.) – Gaffi – 2013-10-28T17:45:11.290

3

Perl 6 (14 bytes)

I assume that reduce operator ([*]) is not a loop.

say [*] 2..get

Konrad Borowski

Posted 2013-10-23T16:11:36.577

Reputation: 11 185

2

Java, 500 bytes

A couple years late to the party, but I think I've captured the spirit of the question, as it uses no form of looping whatsoever. Takes the first command line argument as an integer (greater than or equal to 1), and prints out its factorial, or terminates with exception in the case of overflow. Since java integers can only hold the factorials of numbers up to 16, it just uses a switch statement with the results hard coded in ;)

public class M{public static void main(String[]args){int n=Integer.parseInt(args[0]);int r;switch(n){case(1):r=1;break;case(2):r=2;break;case(3):r=6;break;case(4):r=24;break;case(5):r=120;break;case(6):r=720;break;case(7):r=5040;break;case(8):r=40320;break;case(9):r=362880;break;case(10):r=3628800;break;case(11):r=39916800;break;case(12):r=479001600;break;case(13):r=1932053504;break;case(14):r=1278945280;break;case(15):r=2004310016;break;case(16):r=2004189184;break;default:r=1/0;}System.out.print(r);}}

EDIT - Saved 7 bytes thanks to NoOneIsHere

class M{public static void main(String[]args){int n=Integer.parseInt(args[0]);int r;switch(n){case(1):r=1;break;case(2):r=2;break;case(3):r=6;break;case(4):r=24;break;case(5):r=120;break;case(6):r=720;break;case(7):r=5040;break;case(8):r=40320;break;case(9):r=362880;break;case(10):r=3628800;break;case(11):r=39916800;break;case(12):r=479001600;break;case(13):r=1932053504;break;case(14):r=1278945280;break;case(15):r=2004310016;break;case(16):r=2004189184;break;default:r=1/0;}System.out.print(r);}}

Phoenix

Posted 2013-10-23T16:11:36.577

Reputation: 151

1You can change args to a, and remove some publics (I think). – NoOneIsHere – 2016-09-03T17:49:28.890

1

TI-Basic, 9 bytes

prod(seq(I,I,1,Ans

Makes a list from 1 to the input and calculates the product.

Timtech

Posted 2013-10-23T16:11:36.577

Reputation: 12 038

1

Ruby: 30 26 characters

(“Translation” of David Carraher's answer.)

p [*1..gets.to_i].reduce:*

Sample run:

bash-4.1$ ruby -e 'p [*1..gets.to_i].reduce:*' <<< 5
120

bash-4.1$ ruby -e 'p [*1..gets.to_i].reduce:*' <<< 10
3628800

manatwork

Posted 2013-10-23T16:11:36.577

Reputation: 17 865

1Remove parens around :* to save 2 chars – Doorknob – 2013-10-23T17:00:34.063

@Doorknob, which version? It doesn't work with 1.9.3. http://pastebin.com/JVEb0Wzv

– manatwork – 2013-10-24T07:49:52.930

http://pastebin.com/mSG6d5ax – Doorknob – 2013-10-24T20:50:47.090

Thank you @Doorknob. That $><< was the problem. – manatwork – 2013-10-25T08:14:52.367

1

J, 14 characters

*/1+i.".1!:1[1

Gareth

Posted 2013-10-23T16:11:36.577

Reputation: 11 678

Wouldn't */@:>:@i. work? – Leaky Nun – 2016-05-20T17:23:28.377

@KennyLau I'm not at my computer right now so I can't check, but my answer includes the input 1!:1[1 which your version doesn't. – Gareth – 2016-05-20T17:28:29.613

I see, thanks for your explanation. – Leaky Nun – 2016-05-20T17:29:13.437

1

Python 3 - 77 characters

import functools
print(functools.reduce(int.__mul__,range(1,int(input())+1)))

recursive

Posted 2013-10-23T16:11:36.577

Reputation: 8 616

1

Ruby, 21 characters

p eval [*?1..gets]*?*

kube

Posted 2013-10-23T16:11:36.577

Reputation: 111

0

Haskell - 51 characters

main=getLine >>=(print .(\n->product[1..n]). read)

From Evolution of a Haskell Programmer, the tenured professor.

PyRulez

Posted 2013-10-23T16:11:36.577

Reputation: 6 547

0

PHP, 116

class a{function __destruct(){global $_,$x,$i,$argv;$x*=$i;if($i++<$argv[1])$_=new a;else echo$x;}}$i=$x=1;$_=new a;

No recursions, loops, range, reduce, implode, repeat, map, etc.

jimmy23013

Posted 2013-10-23T16:11:36.577

Reputation: 34 042

I think recursive self-initialisation is definitely recursion. In fact, this is literally no different than a recursive function and global variables... – cat – 2016-06-10T18:10:24.287

@cat You can print a stacktrace using echo new Exception; and it has 2 levels for every time. Internally it is probably a loop on a queue though. – jimmy23013 – 2016-06-10T23:24:02.620

@cat You might have misunderstood it. It is self-initialization but the function is __destruct. – jimmy23013 – 2016-06-10T23:33:48.780

It's still recursion, regardless of whether it's only on a GC cycle – cat – 2016-06-10T23:46:24.397

@cat Well, you can have opinion. – jimmy23013 – 2016-06-10T23:50:23.837

0

Factor, 44 bytes

[ 1 readln string>number 1 <range> product ] 

cat

Posted 2013-10-23T16:11:36.577

Reputation: 4 989

0

Julia

As a function — 12 bytes:

n->prod(1:n)

With an input prompt — 23 bytes:

prod(1:int(readline()))

By integration — 50 bytes:

n=int(readline());int(quadgk(x->x^n/e^x,0,Inf)[1])

(Not sure if the function-only form qualifies, re rule #1.)

charlie

Posted 2013-10-23T16:11:36.577

Reputation: 171