Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Code Golf

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Required fields*

Is this number a factorial?

The Task

Given a natural number as input, your task is to output a truthy or falsey value based on whether the input is a factorial of any natural number. You can assume that the input number will always be in the range of numbers supported by your language, but you must not abuse native number types to trivialize the problem.

Standard Loopholes apply.


Input

You'll be given a natural number (of type Integer or similar).

You can take input in any way you want except assuming it to be in a predefined variable. Reading from file, console, dialog box (prompt), input box etc. is allowed. Input as function argument is allowed as well!


Output

Your program should output a truthy or falsey value based on whether the input number is a factorial of any natural number.

Make sure that your truthy/falsey values are consistent for all inputs, i.e, if you are using pair of 1 and 0 to denote truthy and falsey values respectively, then your program must output 1 for all inputs that should have truthy values and 0 for all inputs that should have falsey values.

You can take output in any way you want except writing it to a variable. Writing to file, console, screen etc. is allowed. Function return is allowed as well!

Your program must not produce errors for any input!


Test Cases

Input Output
1 Truthy (0! or 1!)
2 Truthy (2!)
3 Falsey
4 Falsey
5 Falsey
6 Truthy (3!)
7 Falsey
8 Falsey
24 Truthy (4!)
120 Truthy (5!)

Winning Criterion

This is , so the shortest code in bytes wins!

Answer*

Draft saved
Draft discarded
Cancel
8
  • \$\begingroup\$ Cumulative sum? \$\endgroup\$ Commented May 20, 2017 at 12:32
  • \$\begingroup\$ @LeakyNun Fixed \$\endgroup\$ Commented May 20, 2017 at 12:32
  • \$\begingroup\$ One extra byte in GNU APL 1.2 N∊×\⍳N←⎕ How does this take an argument? I don't see n anywhere. Is this a Dyalog-specific thing? \$\endgroup\$ Commented May 21, 2017 at 2:15
  • 2
    \$\begingroup\$ @Arc676 My solution is a train and you call it like so: (⊢∊(×/⍳)) right_argument as you can see in the TIO link. And the refers to the right argument. \$\endgroup\$ Commented May 21, 2017 at 7:57
  • 1
    \$\begingroup\$ Notes: AGL will save you a byte; ⊢∊×\ä⍳. The "correct" (but longer) solution would be 0=1|!⍣¯1; "Is the inverse factorial an integer?" \$\endgroup\$ Commented May 22, 2017 at 6:02

AltStyle によって変換されたページ (->オリジナル) /