46
\$\begingroup\$

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. 1
  2. 2
  3. 6
  4. 24
  5. 120
  6. 720
  7. 5040

Pseudofactorial:

  1. 1
  2. 2
  3. 6
  4. 12
  5. 60
  6. 60
  7. 420
\$\endgroup\$
8
  • 7
    \$\begingroup\$ I'm not sure I understand why 2 and 6 were removed from the list of multiples. Can please you clarify the rules? \$\endgroup\$
    – Maltysen
    Commented Jun 9, 2016 at 20:23
  • 3
    \$\begingroup\$ @Mattysen, psuedofactorial(N) is the smallest number which has the numbers 1 through N as factors (The least common multiple of those numbers). That is the technical definition, but the way I wrote it was somewhat suggestive that it is similar to a factorial. \$\endgroup\$
    – Tony Ruth
    Commented Jun 9, 2016 at 20:26
  • 2
    \$\begingroup\$ oeis.org/A003418 \$\endgroup\$
    – Neil
    Commented Jun 10, 2016 at 0:19
  • 4
    \$\begingroup\$ Welcome to Programming Puzzles & Code Golf! This is a nice first challenge! \$\endgroup\$
    – Alex A.
    Commented Jun 10, 2016 at 0:40
  • 1
    \$\begingroup\$ Your first challenge got to the top of HNQ. Nice! \$\endgroup\$
    – Daniel M.
    Commented Jun 10, 2016 at 0:56

44 Answers 44

1
2
1
\$\begingroup\$

J-uby, 10 bytes

:+|:/&:lcm

Attempt This Online!

Explanation

:+ | :/ & :lcm

:+ |            # Get range 1..n, then
     :/ & :lcm  # reduce with LCM
\$\endgroup\$
1
\$\begingroup\$

Factor, 26 bytes

[ [1,b] 1 [ lcm ] reduce ]

Try it online!

\$\endgroup\$
1
\$\begingroup\$

05AB1E, 3 bytes

L.¿

Pretty similar as most golfing languages.

Try it online or verify all test cases.

Explanation:

L    # Push a list in the range [1, (implicit) input]
 .¿  # Pop and push the LCM (Least Common Multiple) of this list
     # (which is output implicitly as result)
\$\endgroup\$
1
\$\begingroup\$

Pyt, 9 bytes

Đ`Đ↔Ĺ↔⁻ł+

Try it online!

Đ            implicit input; Đuplicate
 `     ł     do... while top of stack is truthy
  Đ          Đuplicate top of stack
   ↔         flip stack
    Ĺ        get ĹCM of top two on stack
     ↔       flip stack
      ⁻      decrement
        +    add (removes pesky 0); implicit print
\$\endgroup\$
1
\$\begingroup\$

Thunno, \$ 5 \log_{256}(96) \approx \$ 4.12 bytes

R1+Al

Attempt This Online!

Explanation

R      # range(0, input)
 1+    # plus one
   Al  # LCM of list
\$\endgroup\$
1
\$\begingroup\$

Python, 44 bytes

lambda n:math.lcm(*range(1,n+1))
import math

Attempt This Online!

Requires Python 3.9+

\$\endgroup\$
1
\$\begingroup\$

Vyxal, 3 bytes

ɾ∆Ŀ

Run it!

Explanation:

ɾ   # numbers from 1 to input
 ∆Ŀ # least common multiple
\$\endgroup\$
1
\$\begingroup\$

Thunno 2, 2 bytes

Try it online!

Explanation

    # Implicit input
R   # Range [1..input]
 ŀ  # Reduced by LCM
    # Implicit output
\$\endgroup\$
0
\$\begingroup\$

Hoon, 67 bytes

|*
*
(roll (gulf 1 +<) |=({a/@ b/_1} (div (mul a b) d:(egcd a b))))

Create the list [1..n], fold over the list with lcm. Unfortunately, the Hoon stdlib doesn't have a pre-built one I can use :/

\$\endgroup\$
0
\$\begingroup\$

𝔼𝕊𝕄𝕚𝕟, 7 chars / 9 bytes

Мū…⩤⁽1ï

Try it here (ES6 only).

Just a LCM of inclusive range from 1 to input.

\$\endgroup\$
0
\$\begingroup\$

AWK, 42 bytes

{for(x=n=1;n<=$1;)if(x%n++){x++;n=1}$0=x}1

Command line usage:

awk '{for(x=n=2;n<=$1;)if(x%n++){x++;n=2}$0=x}1' <<< NUM

I didn't see an AWK solution and a duplicate of the question just got posted yesterday, so I thought I'd throw this together. It's rather slow solving for 19 or larger on my box, but it works.

\$\endgroup\$
0
\$\begingroup\$

Axiom, 35 bytes

f(x)==reduce(lcm,[i for i in 1..x])

test code and results

(25) -> [[i,f(i)] for i in [1,6,19,22,30]]
   (25)  [[1,1],[6,60],[19,232792560],[22,232792560],[30,2329089562800]]
                                                  Type: List List Integer

i just make the solution of Find the smallest positive integer which has all integers from 1 to n as factors becouse you say it is douplicate i post it here

\$\endgroup\$
0
\$\begingroup\$

8th, 23 bytes

Code

1 ' lcm rot 2 swap loop

This code leaves resulting pseudofactorial on TOS

Usage and example

ok> 7 1 ' lcm rot 2 swap loop .
420

Or more clearly

ok> : pseudofact 1 ' n:lcm rot 2 swap loop ;

ok> 7 pseudofact .
420
\$\endgroup\$
0
\$\begingroup\$

Husk, 3 bytes

F⌉ḣ

Try it online!

same as APL.

\$\endgroup\$
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.