g | x | w | all
Bytes Lang Time Link
026C gcc250821T172148ZToby Spe
005Thunno 2230809T154040ZThe Thon
025Regex 🐇 RME / PCRE2 v10.35+220818T211025ZDeadcode
033Knight220816T231601ZnaffetS
357Nibbles220817T124316ZDominic
004Vyxal220816T231008ZnaffetS
024Factor + math.unicode210329T200315Zuser
053q190809T132615Zscrawl
021Haskell190805T073359Zxnor
061Rust190808T124649Zemilanov
044Java 10190805T091509ZKevin Cr
004Jelly190805T075513ZMr. Xcod
634ArnoldC190806T104210ZCharlie
082Python 3 Input as string190807T074356Zar4093
129Lua190806T164035Zouflak
012x8664 machine code190806T094600ZPeter Co
053Scala190805T145641ZV. Court
047PHP190805T113905ZNight2
027Bash + coreutils190805T204344ZDigital
00705AB1E190805T080622ZKevin Cr
006Gaia190805T184944ZGiuseppe
019Attache190805T183035ZConor O&
006Stax190805T182911Zrecursiv
045Perl 5 Mbigint p190805T181815ZXcali
021Wolfram Language Mathematica190805T171801ZRoman
050Forth gforth190805T171523Zreffu
035JavaScript Node.js190805T152825Zgkgkgkgk
006MathGolf190805T140354Zmaxb
025Ruby190805T135139ZG B
029Python 2190805T130252ZJonathan
022Perl 6190805T121648ZJo King
041C gcc190805T105929Zpommicke
091Whitespace190805T095840ZKevin Cr
008Japt190805T092813ZShaggy
033R190805T094530ZNick Ken
008Japt190805T090141ZGymhgy
066Retina190805T092728ZNeil
022Physica190805T085944ZMr. Xcod
011APL Dyalog Unicode190805T072257ZAdá
007APL Dyalog Extended190805T084020ZAdá
021JavaScript ES6190805T074718ZArnauld
006Pyth190805T074015ZMr. 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;}

Try it online!

Thunno 2, 5 bytes

RrẈhp

Try it online!

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

Knight, 34 33 bytes

;=p=xE P;=yP;W<0=x-x y=p*x pO+p!p

Try it online!

-1 byte thanks to Aiden Chow

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

enter image description here


Or 7.5 bytes (15 nibbles) with input as string of a base-ten integer followed by exclamation marks:

`*`%,|$\$D\,`r$

enter image description here

Vyxal, 4 bytes

ɾṘḞΠ

Try it Online!

Factor + math.unicode, 30 29 24 bytes

[ 1 rot neg <range> Π ]

Try it online!

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]

Try it online!

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

Try it online!

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

Try it online!

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.

Try it online!

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.

Try it online.

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.

Try it online.

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

Try it online!

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

Try it online!

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("!")))

Try it online!

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))

Try it online!

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;}

Try it online!


PHP, 54 bytes

Loop version (original answer)

for($n=$argv[1],$f=1;$n>0;$f*=$n,$n-=$argv[2]);echo$f;

Try it online!


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.

Bash + coreutils, 27

seq -s\* $[$1+!$1] -$2 1|bc

Try it online!

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%Π

Try it online!

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 _]}

Stax, 6 bytes

å▐█ΩV∟

Run and debug it

It takes input in the form {count} {base}.

Perl 5 -Mbigint -p, 45 bytes

$b=s/!//g;$\=1*$_||1;$\*=$_ while($_-=$b)>0}{

Try it online!

Wolfram Language (Mathematica), 22 21 bytes

1##&@@Range[#,1,-#2]&

Try it online!

-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 ;

Try it online!

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           

JavaScript (Node.js), 35 bytes

(n,i,f=i)=>i-n<1?f:r(n,i-n,f*(i-n))

Try it online!

MathGolf, 7 6 bytes

╙╒x%ε*

Try it online!

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

Ruby, 25 bytes

f=->s,r{s<1?1:s*f[s-r,r]}

Try it online!

Python 2, 29 bytes

f=lambda n,b:n<1or n*f(n-b,b)

Try it online!

Perl 6, 22 bytes

{[*] $^a,*-$^b...^1>*}

Try it online!

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.

C (gcc), 41 bytes

r;f(n,k){for(r=1;n>0;n-=k)r*=n;return r;}

Try it online!

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

Japt, 8 bytes

TõUV f ×

Try it

-1 thanks to EoI pointing out how dumb uncaffeinated Shaggy can be!

R, 33 bytes

function(n,k)prod(seq(n+!n,1,-k))

Try it online!

Handles \$n=0\$ by adding the logical negation of \$n\$.

Japt, 8 bytes

1õUV f ×

Try it

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]

Try it online!


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]

Try it online!

APL (Dyalog Unicode), 11 bytesSBCS

Anonymous tacit infix function. Takes n as right argument and b as left argument.

×/1⌈⊢,⊢-×∘⍳

Try it online!

×∘⍳ 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¨

Try it online!

 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)

Try it online!

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])

Try it online!

Pyth, 6 bytes

These are all equivalent 6-byters:

*F:Q1E
*F:E1Q
*F%E_S

Try it online! (*F:Q1E)

Or, 11 bytes, taking input as a string:

*F:.vQ1/Q\!

Test suite.