There is a rather curious number which shows up sometimes in math problems or riddles. The pseudofactorial(N) is the least (i.e. lowest) common multiple of the numbers 1 through N; in other words, it's the lowest number which has all numbers from 1 through N as factors.
For instance pseudofactorial(7) = 3 * 4 * 5 * 7, which is the same as 7! except that 2 and 6 have been removed because they are contained in other terms.
Write a program to calculate pseudofactorial(N) and as always, shortest code wins.
Here is a short list for your use. More test cases can be found in the OEIS under A003418.
Factorial:
- 1
- 2
- 6
- 24
- 120
- 720
- 5040
Pseudofactorial:
- 1
- 2
- 6
- 12
- 60
- 60
- 420
45 Answers 45
-
11\$\begingroup\$ That interrobang is so sexy. \$\endgroup\$shooqie– shooqie2016年06月11日 07:17:40 +00:00Commented Jun 11, 2016 at 7:17
-
1\$\begingroup\$ O man you beat Dennis... \$\endgroup\$Mama Fun Roll– Mama Fun Roll2016年06月12日 04:29:34 +00:00Commented Jun 12, 2016 at 4:29
-
2\$\begingroup\$ Impressive, but how are these 3 bytes? \$\endgroup\$ceased to turn counterclockwis– ceased to turn counterclockwis2016年06月12日 16:13:06 +00:00Commented Jun 12, 2016 at 16:13
-
2\$\begingroup\$ en.wikipedia.org/wiki/APL_(codepage) \$\endgroup\$Leaky Nun– Leaky Nun2016年06月13日 16:57:14 +00:00Commented Jun 13, 2016 at 16:57
Jelly, 4 bytes
Ræl/
Try it online! or verify all test cases.
How it works
Ræl/ Main link. Input: n
R Range; yield [1, ..., n].
/ Reduce the range...
æl by LCM.
C (x86-64 gcc -O1), 52 bytes
d(n,k,b,t){for(b=k=1;b;++k)for(t=n,b=0;t;b+=k%t--);}
Checks numbers from 1 upwards. For each number, divides it by all numbers from n down to 1, and sums the remainders. Stops when the sum is 0.
Usage:
main()
{
printf("%d\n", d(7)); // outputs 420
}
It's not obvious how it returns a value (there is no return statement).
The calling convention for x86 says that the function must return its value in the eax register. Conveniently, the division instruction idiv expects its input in eax, and outputs the result in eax (quotient) and edx (remainder). The last iteration divides k by 1, so eax will contain the right value when the function exits.
This only works with gcc and -O1 optimization option (otherwise, it outputs different results — usually 0 or random-looking numbers, but sometimes 421, while the correct answer is 420).
-
\$\begingroup\$ How do you get away with not declaring the type of n, k, b,and t? \$\endgroup\$Tony Ruth– Tony Ruth2016年06月09日 23:49:48 +00:00Commented Jun 9, 2016 at 23:49
-
\$\begingroup\$ C has the default-int rule - all omitted types are
intby default (including the return value). It works for function arguments if they are declared using the so-called "old-style" syntax. The declaration with explicitly defined types would beint d(n,k,b,t) int n,k,b,t; {...}\$\endgroup\$anatolyg– anatolyg2016年06月09日 23:54:32 +00:00Commented Jun 9, 2016 at 23:54 -
\$\begingroup\$ if you're taking advantage of a calling convention then this should really be marked "C (cdecl)" rather than just "C" \$\endgroup\$Steve Cox– Steve Cox2016年06月10日 20:32:56 +00:00Commented Jun 10, 2016 at 20:32
-
\$\begingroup\$ @SteveCox Both
cdeclandstdcalluse the same method for return-value, so I guessx86is enough \$\endgroup\$anatolyg– anatolyg2016年06月13日 07:04:10 +00:00Commented Jun 13, 2016 at 7:04 -
1\$\begingroup\$ @Deadcode It works in all gcc versions (but only with
-O1), and in some clang versions. Fortunately, we have Compiler Explorer! I added a link so anyone could check these results. \$\endgroup\$anatolyg– anatolyg2023年03月30日 12:08:13 +00:00Commented Mar 30, 2023 at 12:08
Haskell, 20 bytes
f x=foldr1 lcm[1..x]
Usage example: map f [1..7] -> [1,2,6,12,60,60,420].
The lcm trick in Haskell.
Python, 46 bytes
g=lambda n,c=0:n<1or(c%n<1)*c or g(n,c+g(n-1))
Looking for the multiple c of g(n-1) directly. I had though before that this method would wrongly find 0 as a multiple of anything, but the or short-circuiting or (c%n<1)*c will skip c==0 as well because 0 is Falsey.
50 bytes:
g=lambda n,i=1:n<1or(i*n%g(n-1)<1)*i*n or g(n,i+1)
Like Dennis's solution, but as a recursive function. Having computed g(n-1), looks for the smallest multiple i*n of n that's also a multiple of g(n-1). Really slow.
Thanks to Dennis for 4 bytes by looking at multiples of n instead of g(n-1).
Python + SymPy, 45 bytes
import sympy
lambda n:sympy.lcm(range(1,n+1))
Fairly self-explanatory.
Python 2, (削除) 57 (削除ここまで) 54 bytes
i=r=input();exec't=r\nwhile r%i:r+=t\ni-=1;'*r;print r
Test it on Ideone.
How it works
The input is stored in variables i and r.
exec executes the following code r times.
t=r
while r%i:r+=t
i-=1
While i varies from r to 1, we add the initial value of r (stored in t) as many times as necessary to r itself to create a multiple of i. The result is, obviously, a multiple of t.
The final value of r is thus a multiple of all integers in the range [1, ..., n], where n is the input.
-
1\$\begingroup\$ Without using third party libraries or
exectricks there's a 78 byte solution:from fractions import*;lambda n:reduce(lambda x,y:x*y/gcd(x,y),range(1,n+1),1)Uses the fact thatlcm(x,y) = x*y/gcd(x,y). \$\endgroup\$Bakuriu– Bakuriu2016年06月10日 12:02:31 +00:00Commented Jun 10, 2016 at 12:02 -
\$\begingroup\$ use python standard lib
mathto get 44 bytes.import math;lambda n:math.lcm(*range(1,n+1))\$\endgroup\$InQβ– InQβ2025年08月22日 04:54:16 +00:00Commented Aug 22 at 4:54
J, 9 bytes
[:*./1+i.
Straight-forward approach. Creates the range of numbers [0, ..., n-1], then adds one to each, and reduce it using the LCM.
Usage
f =: [:*./1+i.
f 7
420
MATL, 4 bytes
:&Zm
Explanation
: % Take input N implicitly. Generate range [1 2 ... N]
&Zm % LCM of the numbers in that array. Display implicitly
Mathematica, 13 bytes
LCM@@Range@#&
-
\$\begingroup\$ isn't this equivalent to just composing
LCMandRangewith@*? \$\endgroup\$Maltysen– Maltysen2016年06月10日 00:04:36 +00:00Commented Jun 10, 2016 at 0:04 -
1\$\begingroup\$
LCMoperates element-wise on a list, which would be passed byRange, meaning this would just return the lcm(x) for x from 1 through n. Also, there's a missing&that would close the anonymous function. Something likeLCM@@Range@#&for 13 bytes would work. \$\endgroup\$miles– miles2016年06月10日 00:30:06 +00:00Commented Jun 10, 2016 at 0:30
Regex 🐇 (PCRE2 v10.35+), (削除) 60 (削除ここまで) (削除) 59 (削除ここまで) (削除) 54 (削除ここまで) 52 bytes
^((?*(?=(xx+?)2円*$|)((?=x2円)(x+)4円*(?=4円$))*+x+)x)*$
Takes its input in unary, as a string of x characters whose length represents the number. Returns its output as the number of ways the regex can match. (The rabbit emoji indicates this output method.)
Attempt This Online! - PCRE2 v10.40+
The 🐇 output method takes advantage of an aspect of regex that's normally hidden: the backtracking paths that aren't taken. In normal use, all traces of the avenues of potential alternative matches are erased when a final match is found. But in 🐇, they're all taken and counted, allowing a number to be outputted that's larger than the input. For scalar unary input, this provides a strict superset of the power of returning a number via the matched substring or a capture group (either of which can only return values less than or equal to the input), except that there can be no extra state of returning "no match" (in 🐇, that just returns \0ドル\$).
The limitation of having to work within the space of the input is still present. 🐇 can do combinations of multiplication and addition to yield numbers larger than the input, but it can't do subsequent tests on those results (it can only do tests on intermediate results calculated using capture groups and tail). So for example in this problem, the regex can't just directly search for the smallest number that is divisible by all numbers in the range \$[1,n]\$, because that number is not only larger than the input, it's too large even to be able to emulate using number base arithmetic (and even if that were possible, it would not golf well). So, this regex uses an algorithm different from all of the other answers:
It takes the base prime \$p\$ of every prime power \$p^k\le n\$, and calculates the product of that list of numbers. And because each \$p\$ will occur \$\max \left\{ k ,円 \middle| ,円 p^k \le n \right\}\$ times in that list, this product is the same as the product of the prime powers themselves would be, if only the largest from each base prime were included.
The prime power portion is based on (削除) my prime powers answer (削除ここまで) Neil's prime powers answer (which is in turn based on my earliest CGCC answer). In the prime powers challenge, my regex is shorter, but for the purposes of this challenge, his regex (after some extra golfing) allows capturing the smallest prime factor, thus golfing down the overall regex by (削除) 1 (削除ここまで) 6 bytes.
^ # tail = N = input number
( # Loop the following:
(?* # Non-atomic lookahead:
# M = tail, which cycles from N down to 1
(?=(xx+?)2円*$|) # 2円 = smallest prime factor of M if M ≥ 2;
# otherwise, leave 2円 unchanged in PCRE, or
# unset in ECMAScript
(
(?=x2円) # Keep iterating until tail ≤ 2,円 and because of
# what 2円 is, this means at the end of the loop,
# either tail == 2円 (if M is a prime power) or
# tail == 1 (if M is not a prime power)
(x+)4円*(?=4円$) # tail = 4円 = {largest proper divisor of tail}
# = tail / {smallest prime factor of tail}
)*+ # Iterate the above as many times as possible,
# minimum zero, and lock in the result using a
# possessive quantifier.
x+ # Multiply number of possible matches by tail
)
x # tail -= 1
)* # Loop the above a minimum of 0 times
$ # Assert that at the end of the above loop, tail == 0
This even returns a correct value for \$n=0\$, while the earlier 60 byte version did not (and needed to be extended to 63 bytes to do so).
A list of other answers that return \$f(0)=1\$ correctly, complete at the time of this edit: AWK, Dyalog APL, J, Japt, Julia v0.7+ (but it was v0.6 at the time of posting, and didn't support it then), MATLAB, MATL, Maxima (with functs), Minkolang (1st answer), Nibbles, PHP, Pari/GP, Perl 5, Perl 6, Pyth, Python (with SymPy), Python (with math), Python, QBIC, Rexx, Vyxal. (Not tested: 8th, Axiom, Hoon)
Thunno 2, 2 bytes
Rl·
Explanation
# Implicit input
R # Range [1..input]
l· # Reduced by LCM
# Implicit output
Pyth - 8 bytes
Checks all numbers till it finds one that is divisible by [1..N].
f!s%LTSQ
Octave, 27 bytes
@(x)lcm(1,num2cell(1:x){:})
Creates an anonymous function that can be invoked as ans(N).
Explanation
This solution creates a list of all numbers between 1 and x (1:x), converts them to a cell array with num2cell. Then the {:} indexing creates a comma-separated-list which is passed to lcm as multiple input arguments to compute the least common multiple. A 1 is always passed as the first argument to lcm because lcm always needs at least two input arguments.
-
1\$\begingroup\$ So
lcmin Octave accepts more than 2 inputs! Interesting \$\endgroup\$Luis Mendo– Luis Mendo2016年06月09日 21:00:10 +00:00Commented Jun 9, 2016 at 21:00 -
\$\begingroup\$ @LuisMendo Yup 2+ \$\endgroup\$Suever– Suever2016年06月09日 21:00:29 +00:00Commented Jun 9, 2016 at 21:00
MATLAB, 49 bytes
@(x)find(~any(bsxfun(@rem,1:prod(1:x),(1:x)')),1)
Perl 6, 13 bytes
{[lcm] 1..$_}
Anonymous code block that creates a Range from 1 to the input (inclusive), and then reduces that with &infix:<lcm>.
Example:
#! /usr/bin/env perl6
use v6.c;
my &postfix:<p!> = {[lcm] 1..$_}
say 1p!; # 1
say 2p!; # 2
say 3p!; # 6
say 4p!; # 12
say 5p!; # 60
say 6p!; # 60
say 7p!; # 420
say 10000p!; # 5793339670287642968692270879...
# the result from this is 4349 digits long
-
\$\begingroup\$ ideone.com/gR4SK5 \$\endgroup\$Brad Gilbert b2gills– Brad Gilbert b2gills2016年06月12日 19:14:42 +00:00Commented Jun 12, 2016 at 19:14
Brachylog, 5 bytes
fa~⟦1
Reversed I/O.
a An affix of
f the list of the output's factors
~⟦1 is [1 .. n].
Essentially a specialization of my basic LCM solution f⊇p~d: ranges are duplicate-free, so they don't need to be deduplicated; ranges are ascending, so they don't need to be permuted; ranges are necessarily contiguous prefixes of the list of factors, so ⊇ can be replaced by the evidently much more performant a.
JavaScript (ES6), (削除) 92 (削除ここまで) (削除) 88 (削除ここまで) (削除) 80 (削除ここまで) (削除) 74 (削除ここまで) 69 bytes:
Thanks @ConorOBrien and @Neil
y=>(g=(a,b)=>b?g(b,a%b):a,[...Array(y)].map((_,i)=>y=y*++i/g(y,i)),y)
Ruby, 25 bytes
g=->n{(1..n).reduce :lcm}
Ruby, 25 bytes
g=->n{n<1?1:a[n-1].lcm n}
-
2\$\begingroup\$ Hello, and welcome to PPCG! Great first post! You don't have to name your function, so you can remove
g=. \$\endgroup\$NoOneIsHere– NoOneIsHere2016年06月10日 23:39:10 +00:00Commented Jun 10, 2016 at 23:39 -
\$\begingroup\$ Anonymous functions are allowed. \$\endgroup\$Erik the Outgolfer– Erik the Outgolfer2016年06月11日 11:31:53 +00:00Commented Jun 11, 2016 at 11:31
05AB1E, 20 bytes
Lpvyi1LÒN>¢àN>*ˆ}} ̄P
Explanation
Lpv # for each item in isprime(range(1,N)): N=7 -> [0,1,1,0,1,0,1]
yi # if prime
1LÒN>¢ # count occurrences of the prime
in the prime-factorization of range(1,N):
p=2 -> [0,1,0,2,0,1,0]
àN>*ˆ # add max occurrence of that prime multiplied by the prime
to global array: N=7 -> [4,3,5,7]
}} # end if/loop
̄P # get product of global array
Minkolang 0.15, 12 bytes
I have two 12-byte solutions, and have included them both.
1n[i1+4$M]N.
Explanation
1 Push 1
n Take number from input
[ For loop that repeats n times
i1+ Push loop counter + 1
4$M Pop b, a and push lcm(a,b)
] Close for loop
N. Output as number and stop.
About as straightforward as it gets.
11nLd[4$M]N.
Explanation
11 Push two 1s
n Take number from input
L Pop b, a and push range from a to b, inclusive
d Duplicate top of stack (n)
[4$M] Pop b, a and push lcm(a,b), n times
N. Output as number and stop.
A third solution can be derived from this: remove a 1 and add a d after the current d. In both cases, the extra number is needed because the for loop runs one too many times, and making it run one less time takes two bytes (1- just before the [).
GameMaker Language, 60 bytes
for(b=k=1;b;++k){b=0for(t=argument0;t;b+=k mod t--)}return k
Based on the logic of anatolyg's answer.
-
\$\begingroup\$ I haven't actually tested this in GameMaker, but as written it almost certainly doesn't work, and needs to return
k-1, notk. \$\endgroup\$Deadcode– Deadcode2023年03月30日 00:17:37 +00:00Commented Mar 30, 2023 at 0:17
PHP, (削除) 61 (削除ここまで) (削除) 52 (削除ここまで) 48 bytes
saved 9 bytes thanks to @user59178, 4 bytes by merging the loops.
Recursion in PHP is bulky due to the function key word; so I use iteration.
And with a "small"few tricks, I now even beat Arnauld ́s JS.
while(++$k%++$i?$i>$argv[1]?0:$i=1:$k--);echo$k;
takes input from command line argument. Run with -r.
breakdown
while(++$k%++$i? # loop $i up; if it does not divide $k
$i>$argv[1]?0 # break if $i (smallest non-divisor of $k) is larger than input
:$i=1 # while not, reset $i and continue loop with incremented $k
:$k--); # undo increment while $i divides $k
echo$k; # print $k
ungolfed
That ́s actually two loops in one:
while($i<=$argv[1]) # loop while $i (smallest non-divisor of $k) is not larger than input
for($k++, # loop $k up from 1
$i=0;$k%++$i<1;); # loop $i up from 1 while it divides $k
echo$k; # print $k
Note: copied from my answer on the duplicate
QBIC, (削除) 35 (削除ここまで) 32 bytes
This brought me here.
:{p=0[a|~q%b|p=1]]~p=0|_Xq\q=q+1
Explanation:
: Get cmd line param as number 'a'
{ Start an infinite DO loop
p=0 Sets a flag that shows if divisions failed
[a| FOR (b=1; b<=a; b++)
~q%b IF 'q' (which starts at 1 in QBIC) is not cleanly divisible by 'b'
|p=1 THEN Set the flag
]] Close the FOR loop and the IF, leave the DO open
~p=0 IF 'q' didn't get flagged
|_Xq THEN quit, printing 'q'
\q=q+1 ELSE raise 'q', redo
[DO Loop implicitly closed by QBIC]
Here's a version that stops testing q when b doesn't cleanly divide it. Also, the order of testing b's against q is reversed in the assumption that higher b's will be harder to divide by (take 2, 3, 4 for instance: if %2=0, %4 could be !0. Vice versa not so much...).
:{p=0[a,2,-1|~q%b|p=1┘b=2]]~p=0|_Xq\q=q+1
Explore related questions
See similar questions with these tags.
2and6were removed from the list of multiples. Can please you clarify the rules? \$\endgroup\$