| Bytes | Lang | Time | Link |
|---|---|---|---|
| 026 | C gcc | 250821T172148Z | Toby Spe |
| 005 | Thunno 2 | 230809T154040Z | The Thon |
| 025 | Regex 🐇 RME / PCRE2 v10.35+ | 220818T211025Z | Deadcode |
| 033 | Knight | 220816T231601Z | naffetS |
| 357 | Nibbles | 220817T124316Z | Dominic |
| 004 | Vyxal | 220816T231008Z | naffetS |
| 024 | Factor + math.unicode | 210329T200315Z | user |
| 053 | q | 190809T132615Z | scrawl |
| 021 | Haskell | 190805T073359Z | xnor |
| 061 | Rust | 190808T124649Z | emilanov |
| 044 | Java 10 | 190805T091509Z | Kevin Cr |
| 004 | Jelly | 190805T075513Z | Mr. Xcod |
| 634 | ArnoldC | 190806T104210Z | Charlie |
| 082 | Python 3 Input as string | 190807T074356Z | ar4093 |
| 129 | Lua | 190806T164035Z | ouflak |
| 012 | x8664 machine code | 190806T094600Z | Peter Co |
| 053 | Scala | 190805T145641Z | V. Court |
| 047 | PHP | 190805T113905Z | Night2 |
| 027 | Bash + coreutils | 190805T204344Z | Digital |
| 007 | 05AB1E | 190805T080622Z | Kevin Cr |
| 006 | Gaia | 190805T184944Z | Giuseppe |
| 019 | Attache | 190805T183035Z | Conor O& |
| 006 | Stax | 190805T182911Z | recursiv |
| 045 | Perl 5 Mbigint p | 190805T181815Z | Xcali |
| 021 | Wolfram Language Mathematica | 190805T171801Z | Roman |
| 050 | Forth gforth | 190805T171523Z | reffu |
| 035 | JavaScript Node.js | 190805T152825Z | gkgkgkgk |
| 006 | MathGolf | 190805T140354Z | maxb |
| 025 | Ruby | 190805T135139Z | G B |
| 029 | Python 2 | 190805T130252Z | Jonathan |
| 022 | Perl 6 | 190805T121648Z | Jo King |
| 041 | C gcc | 190805T105929Z | pommicke |
| 091 | Whitespace | 190805T095840Z | Kevin Cr |
| 008 | Japt | 190805T092813Z | Shaggy |
| 033 | R | 190805T094530Z | Nick Ken |
| 008 | Japt | 190805T090141Z | Gymhgy |
| 066 | Retina | 190805T092728Z | Neil |
| 022 | Physica | 190805T085944Z | Mr. Xcod |
| 011 | APL Dyalog Unicode | 190805T072257Z | Adá |
| 007 | APL Dyalog Extended | 190805T084020Z | Adá |
| 021 | JavaScript ES6 | 190805T074718Z | Arnauld |
| 006 | Pyth | 190805T074015Z | Mr. Xcod |
C (gcc), 26 bytes
f(b,c){b>c?b*=f(b-c,c):0;}
Simple recursive implementation:
int f(int base, int count)
{
return base > count ? base * f(base-count, count) : b;
}
There are equivalent expressions of the same length, e.g.:
f(b,c){b*=b>c?f(b-c,c):1;}
Thunno 2, 5 bytes
RrẈhp
Explanation
RrẈhp # Implicit input
Rr # Push the range [n..1]
Ẉ # Uninterleave into k pieces
hp # Product of first group
# Implicit output
Regex 🐇 (RME / PCRE2 v10.35+), 25 bytes
^(x+),((?*x+)(?>\1|.*))*$
Attempt This Online! - PCRE2
Try it on replit.com! - RegexMathEngine, in ECMAScript+(?*)+(?>) mode
Takes its input in unary, as two strings of x characters whose lengths represent the numbers, in the order {factorial count, base value}, joined/delimited by a ,. Returns its output as the number of ways the regex can match. (The rabbit emoji indicates this output method.)
^(x+), # \1 = input factorial count; tail = input base value
(
(?*x+) # Assert tail > 0; multiply the number of possible matches by tail
(?> # Atomic group – lock in this match once it's finished
\1 # tail -= \1
| # or
.* # tail = 0
)
)* # Iterate the above as many times as possible, minimum zero
$ # Assert tail == 0
Regex 🐇 (RME / PCRE2 v10.35+), 26 bytes
^((?=(x*).*,\2)(?*x+)\2)*,
Attempt This Online! - PCRE2
Try it on replit.com! - RegexMathEngine, in ECMAScript+(?*) mode
Takes the two numbers in the order {base value, factorial count}. Although this is 1 byte longer, it does not need an atomic group to be this length, using an atomic lookahead instead.
^ # tail = input base value
(
(?= # Atomic lookahead
(x*) # \2 = maximum value ≤ tail for which the following matches:
.*, # tail = input factorial count
\2 # Assert tail ≥ \2
)
(?*x+) # Assert tail > 0; multiply the number of possible matches by tail
\2 # tail -= \2
)* # Iterate the above as many times as possible, minimum zero
, # Assert tail == 0
Nibbles, 3.5 bytes (7 nibbles)
`*`%@\,
, # get the range from 1..first input,
\ # reverse it,
% # and take each element in steps of
@ # the second input,
`* # and finally get the product
Or 7.5 bytes (15 nibbles) with input as string of a base-ten integer followed by exclamation marks:
`*`%,|$\$D\,`r$
Factor + math.unicode, 30 29 24 bytes
- Saved 1 byte thanks to @Bubbler
- Saved 5 bytes thanks to @chunes!
[ 1 rot neg <range> Π ]
Takes k (which -th factorial) and then n. First we push an n onto the stack, then rot makes the stack look like n 1 k. neg negates k, which will act as the step for a range from n to 1 (made using <range>). Then Π finds the product of that range, and that's it.
q, 59 57 55 53 bytes
{prd 2+(&)1_i=last i:("J"$x(&)not[n])#(!)sum n:"!"=x}
explanation:
q)x:"12!!" / let our input be 12!!, assign to x
q)sum n:"!"=x / count "!"s
2i
q)(!)sum n:"!"=x / (!)m -> [0,m)
0 1
q)("J"$x(&)not[n]) / isolate the number in input
12
q)("J"$x(&)not[n])#(!)sum n:"!"=x / x#y means take x items from list y, if x>y, circle around
0 1 0 1 0 1 0 1 0 1 0 1
q)i:("J"$x(&)not[n])#(!)sum n:"!"=x / assign to i
q)i
0 1 0 1 0 1 0 1 0 1 0 1
q)(last i)=i:("J"$x(&)not[n])#(!)sum n:"!"=x / take last elem of i and see which are equal in i
010101010101b
q)1_(last i)=i:("J"$x(&)not[n])#(!)sum n:"!"=x / drop first elem
10101010101b
q)(&)1_(last i)=i:("J"$x(&)not[n])#(!)sum n:"!"=x / indices of 1b (boolean TRUE)
0 2 4 6 8 10
q)2+(&)1_(last i)=i:("J"$x(&)not[n])#(!)sum n:"!"=x / add 2 across array
2 4 6 8 10 12
q)prd 2+(&)1_(last i)=i:("J"$x(&)not[n])#(!)sum n:"!"=x / product across array
46080
here also is a version in k (same logic), 4241 bytes
{*/2+&1_i=last i:("J"$x@&~:n)#!+/n:"!"=x}
Haskell, 21 bytes
n%a=product[n,n-a..1]
Combining the built-in product function with stepped range enumeration beats what I could code up recursively (even with flawr saving a byte).
22 bytes
n%a|n<1=1|m<-n-a=n*m%a
Here's a solution taking input in string format like 9!!, which I think is more interesting.
42 bytes
(\[(n,a)]->product[n,n-length a..1]).reads
Rust, 92 73 61 bytes
fn f(n:i128,k:i128)->i128{if n<=0{return 1}return n*f(n-k,k)}
I am just starting to learn rust, so I'm sure this can be shorter. Will update as I learn. The return value should be i128 in order to compute the last test.
Edit: Recursion is shorter.
You can add your own test, or edit one of the already existing ones.
Java 10, 44 bytes
f->b->{int r=1;for(;b>0;b-=f)r*=b;return r;}
Takes the factorial as first input, base as second.
This above doesn't work for the largest test case due to the limited integer range (32-bits). To fix this we can use BigIntegers, which coincidentally is exactly double the size - 88 79 bytes:
f->b->{var r=f.ONE;for(;b.signum()>0;b=b.subtract(f))r=r.multiply(b);return r;}
-9 bytes thanks to @OlivierGrégoire.
Explanation:
f->b->{ // Method with two integer parameters and integer return-type
int r=1; // Result-integer, starting at 1
for(;b>0; // Loop as long as the base is still larger than 0
b-=f) // After every iteration: decrease the base by the factorial
r*=b; // Multiply the result by the base
return r;} // Return the result
Jelly, 4 bytes
RṚmP
How? Given \$n\$ and \$k\$, it first generates the range \$n,\cdots, 1\$ (with RṚ), then with m it keeps every \$k^{\text{th}}\$ element of this range (so \$n, n-k, n-2k,\cdots,n-\lfloor n/k\rfloor k\$), and finally multiplies them using P.
ArnoldC, 702 698 634 bytes
LISTEN TO ME VERY CAREFULLY f
I NEED YOUR CLOTHES YOUR BOOTS AND YOUR MOTORCYCLE n
I NEED YOUR CLOTHES YOUR BOOTS AND YOUR MOTORCYCLE p
GIVE THESE PEOPLE AIR
HEY CHRISTMAS TREE r
YOU SET US UP 1
HEY CHRISTMAS TREE c
YOU SET US UP 0
STICK AROUND n
GET TO THE CHOPPER r
HERE IS MY INVITATION r
YOU'RE FIRED n
ENOUGH TALK
GET TO THE CHOPPER n
HERE IS MY INVITATION n
GET DOWN p
ENOUGH TALK
GET TO THE CHOPPER c
HERE IS MY INVITATION 0
LET OFF SOME STEAM BENNET n
ENOUGH TALK
BECAUSE I'M GOING TO SAY PLEASE c
GET TO THE CHOPPER n
HERE IS MY INVITATION 0
ENOUGH TALK
YOU HAVE NO RESPECT FOR LOGIC
CHILL
I'LL BE BACK r
HASTA LA VISTA, BABY
Translated to pseudocode:
f(n,p) {
r=1;
c=0;
while (n) {
r=r*n;
n=n-p;
c=n<0;
if (c) n=0;
}
return r;
}
Note: ArnoldC has only one type of data: 16-bit signed integer. Hence I cannot test the 420!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! case.
Python 3 (Input as string), 82 bytes
m=lambda n,s:n<1or n*m(n-s,s)
f=lambda x:int(m(int(x.split("!")[0]),x.count("!")))
Note: the outer int() in f is solely for the fact that without it, 0! will output True
Lua, 150 145 129 bytes
i=io.read()p,c=i.gsub(i,"!","")p=p+0
function f(n,c)r=1 if n~=0 then
while(n>0) do r=r*n n=n-c
end
end
return r end
print(f(p,c))
Ported from Charlie's ArnoldC psuedocode. -16 bytes thanks to Charlie
x86-64 machine code, 12 bytes
Same machine code does the same thing in 32-bit mode, and for 16-bit integers in 16-bit mode.
This is a function, callable with args n=RCX, k=ESI. 32-bit return value in EAX.
Callable from C with the x86-64 System V calling convention with dummy args to get the real args into the right registers. uint32_t factk(int, uint32_t k, int, uint64_t n); I couldn't just use Windows x64 because 1-operand mul clobbers RDX, and we don't want REX prefixes to access R8/R9. n must not have any garbage in the high 32 bits so JRCXZ works, but other than that it's all 32-bit.
NASM listing (relative address, machine code, source)
1 factk:
2 00000000 6A01 push 1
3 00000002 58 pop rax ; retval = 1
4 00000003 E306 jrcxz .n_zero ; if (n==0) return
5 .loop: ; do {
6 00000005 F7E1 mul ecx ; retval *= n (clobbering RDX)
7 00000007 29F1 sub ecx, esi ; n -= k
8 00000009 77FA ja .loop ; }while(sub didn't wrap or give zero)
9 .n_zero:
10 0000000B C3 ret
0xc = 12 bytes
Or 10 bytes if we didn't need to handle the n=0 special case, leaving out the jrcxz.
For standard factorial you'd use loop instead of sub/ja to save 2 bytes, but otherwise the exact same code.
Test caller that passes argc as k, with n hard-coded.
align 16
global _start
_start:
mov esi, [rsp]
;main:
mov ecx, 9
call factk
mov esi, eax
mov edx, eax
lea rdi, [rel print_format]
xor eax, eax
extern printf
call printf
extern exit
call exit
section .rodata
print_format: db `%#x\t%u\n`
```
Scala, 53 bytes
Takes two arguments, the first being the number and the second being the ! count.
Somehow breaks on the big test case. This step range costs many bytes in the end because of this -1* so improvements ought to be possible by refactoring.
I have, as always, a doubt about how we count bytes for function declarations, because everyone doesn't do the same way everywhere down there, so well... It's 17 bytes less, so 36 bytes, if we count out func variables' (a and b) declaration and func brackets.
(a:Int,b:Int)=>{var s=1
for(i<-a to b by -1*b)s*=i
s}
Try it online! I commented out the broken test cases, because, well, they do break.
PHP, 47 bytes
Recursive function with tests
function f($n,$c){return$n>0?f($n-$c,$c)*$n:1;}
PHP, 54 bytes
Loop version (original answer)
for($n=$argv[1],$f=1;$n>0;$f*=$n,$n-=$argv[2]);echo$f;
First argument is the the base value and second one is the factorial count.
Supports specified valid range, but outputs big numbers in scientific notation. For example the last sample is shown as 4.1697106428257E+31.
05AB1E, 10 8 7 bytes
ݦRIιнP
Input as two separated inputs: first input being base; second input being factorial.
Try it online or verify all test cases.
-2 bytes thanks to @Mr.Xcoder.
-1 byte thanks to @JonathanAllan.
Explanation:
Ý # Create a list in the range [0, (implicit) base-input]
¦ # And remove the first item to make it the range [1, base]
# (NOTE: this is for the edge case 0. For the other test cases simply `L` instead
# of `ݦ` is enough.)
R # Reverse this list so the range is [base, 1]
Iι # Uninterleave with the second input as step-size
# i.e. base=3, factorial=7: [[3],[2],[1],[],[],[],[]]
# i.e. base=10, factorial=8: [[10,2],[9,1],[8],[7],[6],[5],[4],[3]]
# i.e. base=420, factorial=30: [[420,390,360,...,90,60,30],[419,389,359,...],...]
н # Only leave the first inner list
P # And take the product of its values
# (which is output implicitly as result)
Original 10 bytes answer:
L0KD¤-IÖÏP
Input as two separated inputs: first input being base; second input being factorial.
Try it online or verify all test cases.
Explanation:
L # Create a list in the range [1, (implicit) base-input]
0K # Remove all 0s (edge case for input 0, which will become the list [1,0])
D # Duplicate this list
¤ # Get the last value (without popping)
# (could also be `Z` or `¹` for max_without_popping / first input respectively)
- # Subtract it from each item in the list
IÖ # Check for each if they're divisible by the second factorial-input
Ï # In the list we copied, only leave the values at the truthy indices
P # And take the product of those
# (which is output implicitly as result)
Gaia, 6 bytes
…)¦v%Π
Takes input as n, k, so input of 3 4 would be 3!!!!.
… push [0...n-1], or [] if n == 0
)¦ increment each value (does nothing if [])
v reverse list
% take every k'th element
Π product; product([]) = 1.
Attache, 21 19 bytes
${x<y∨x*$[x-y,y]}
Try it online! Pretty direct recursive implementation. (Note: true is essentially 1, as it can be used in arithmetic operations as 1.) This is one of the few programs I've written for this site where using a unicode operator saves bytes (1, to be precise).
Alternatives
20 bytes: ${x<y or x*$[x-y,y]}
21 bytes: Prod@${{_%y=x%y}\1:x}
27 bytes: ${x*[`1,$][x>y][x-y,y]∨1}
27 bytes: ${If[x>y,x*$[x-y,y],_or 1]}
27 bytes: ${x*[`1,$][x>y][x-y,y]or 1}
29 bytes: ${If[x>y,x*$[x-y,y],_+not _]}
Wolfram Language (Mathematica), 22 21 bytes
1##&@@Range[#,1,-#2]&
-1 thanks to attinat: Times --> 1##&
Explanation: use Range to make a list of the values {n, n-k, n-2k, n-3k, ...}, stopping before going below 1 (i.e., stopping just right). Then multiply all numbers in this list with Times (or 1##&).
Forth (gforth), 50 bytes
: f 1 1 2over / 1+ 0 do 2over i * - 1 max * loop ;
Code Explanation
: f \ start a new word definition
1 1 \ add placeholder and accumulator to stack
2over / 1+ \ get the number of times to run the loop (num/factorial + 1)
0 do \ start loop from 0 to num/factorial
2over \ copy num and factorial to the top of the stack
i * - \ get the current number to multiply by (num - factorial * i)
1 max \ make sure it can't be 0 or negative [set to 1 if it is]
* \ multiply accumulator by result
loop \ end loop
; \ end the word definition
MathGolf, 7 6 bytes
╙╒x%ε*
Found a clever way to handle 0! without changing the other test cases. Takes input as k n (reverse order), which helps with implicit popping.
Explanation
╙ maximum of two elements (pops largest of k and n,
which is n for every valid case except 0!, where 1 is pushed)
╒ range(1,n+1)
x reverse int/array/string
% slice every k:th element
ε* reduce list with multiplication
Perl 6, 22 bytes
{[*] $^a,*-$^b...^1>*}
Anonymous codeblock that returns the product of the range starting from the first input, decreasing by the second until it is below 1, excluding the last number. This works for 0, since the base case of a the reduce by product is 1, so the output is 1.
Whitespace, 91 bytes
[S S S T N
Push_1][S N
S _Duplicate_1][S N
S _Duplicate_1][T N
T T _Read_STDIN_as_integer_(base)][T T T _Retrieve_base][S S S N
_Push_0][T N
T T _Read_STDIN_as_integer_(factorial)][N
S S N
_Create_Label_LOOP][S N
S _Duplicate_base][S S S T N
_Push_1][T S S T _Subtract][N
T T S N
_If_negative_jump_to_Label_PRINT_RESULT][S N
S _Duplicate_base][S T S S T S N
_Copy_0-based_2nd_(result)][T S S N
_Multiply][S N
T _Swap_top_two][S S S N
_Push_0][T T T _Retrieve_factorial][T S S T _Subtract][N
S N
N
_Jump_to_Label_LOOP][N
S S S N
_Create_Label_PRINT_RESULT][S N
N
_Discard_top][T N
S T _Print_result_as_integer]
Letters S (space), T (tab), and N (new-line) added as highlighting only.
[..._some_action] added as explanation only.
Try it online (with raw spaces, tabs and new-lines only).
Explanation in pseudo-code:
Integer result = 1
Integer base = STDIN as integer
Integer factorial = STDIN as integer
Start LOOP:
If(base <= 0):
Call function PRINT_RESULT
result = result * base
base = base - factorial
Go to next iteration of LOOP
function PRINT_RESULT:
Print result as integer to STDOUT
R, 33 bytes
function(n,k)prod(seq(n+!n,1,-k))
Handles \$n=0\$ by adding the logical negation of \$n\$.
Retina, 66 bytes
^0
1
\d+
*!,
+`(!+)(!+),\1$
$1$2,$2,$1
!+$
1
+`(!+),(\d+)
$.($2*$1
Try it online! Link includes faster test cases. Mauls numbers without exclamation marks. Explanation:
^0
1
Fix up 0!.
\d+
*!,
Convert n to unary and add a separator.
+`(!+)(!+),\1$
$1$2,$2,$1
Repeatedly subtract k from n while n>k, and collect the results.
!+$
1
Replace k with 1 (in decimal).
+`(!+),(\d+)
$.($2*$1
Multiply by each intermediate value in turn, converting to decimal.
Physica, 22 bytes
f=>n;k:n<1||n*f[n-k;k]
26 bytes
Re-learning how to use my own "language" \o/... If I knew how to write a parser 2 years ago, this would have been 20 bytes :(
->n;k:GenMul##[n…1]{%%k}
or
->n;k:GenMul##Range[n;1;k]
APL (Dyalog Unicode), 11 bytesSBCS
Anonymous tacit infix function. Takes n as right argument and b as left argument.
×/1⌈⊢,⊢-×∘⍳
×∘⍳ multiply b by the ɩntegers 1 through n
⊢- subtract that from n
⊢, prepend n
1⌈ max of one and each of those
×/ product
APL (Dyalog Extended), 7 bytesSBCS
Anonymous tacit prefix function. Takes [n,b] as argument.
×/-\…1¨
1¨ one for each element of the argument; [1,1]
-\ cumulative difference; [n,n-b]
… range using second element of left argument as indicator of step, e.g. [9,7] continues with 5
×/ product
JavaScript (ES6), 21 bytes
Takes input as (k)(n).
k=>g=n=>n<1||n*g(n-k)
Or 24 bytes to support BigInts.
JavaScript (ES6), 55 bytes
Takes input as a string, using the format described in the challenge.
s=>(a=s.split`!`,k=a.length-1,g=n=>n<1||n*g(n-k))(a[0])
Pyth, 6 bytes
These are all equivalent 6-byters:
*F:Q1E
*F:E1Q
*F%E_S
Or, 11 bytes, taking input as a string:
*F:.vQ1/Q\!

