| Bytes | Lang | Time | Link |
|---|---|---|---|
| 028 | Juby | 250502T173258Z | Jordan |
| nan | 230225T145008Z | The Thon | |
| 038 | Arturo | 230225T160443Z | chunes |
| 035 | Factor + math.unicode | 221027T013832Z | chunes |
| 012 | Japt | 221024T143839Z | Shaggy |
| 079 | Whispers v3 | 210218T064958Z | Razetime |
| 006 | Husk | 210218T063803Z | Leo |
| 005 | Stax | 210218T063540Z | Razetime |
| 081 | PHP | 170530T111925Z | Jör |
| 034 | Axiom | 170530T101953Z | user5898 |
| 030 | Pari/GP | 170530T065903Z | alephalp |
| 142 | Python 3 | 160613T191758Z | george |
| 026 | Mathematica | 160615T073526Z | Martin E |
| 052 | Haskell | 160615T002427Z | Vaelus |
| 095 | Hoon | 160614T223457Z | RenderSe |
| 047 | Ruby | 160614T002627Z | Peter Ka |
| 067 | Python 2 | 160614T043643Z | Dennis |
| 022 | Julia | 160613T180020Z | lynn |
| 027 | Mathematica | 160613T175710Z | lynn |
| 032 | Perl 6 | 160613T215651Z | Brad Gil |
| 020 | J | 160613T213151Z | miles |
| 006 | M | 160613T182041Z | Adnan |
| 008 | 05AB1E | 160613T182608Z | Adnan |
| 013 | MATL | 160613T175228Z | Luis Men |
| 088 | JavaScript ES6 | 160613T182920Z | Neil |
| 009 | Jelly | 160613T180615Z | lynn |
Thunno, \$ 12 \log_{256}(96) \approx \$ 10 bytes
(Actually 9.88 bytes but that doesn't show up on the leaderboard)
Fztz!R+/SAG/
Port of Adnan's 05AB1E answer.
Explanation
Fztz!R+/SAG/ # Implicit input
Fzt # Get factorial and triplicate
z!R+ # Push range(1, input+1)
/S # Divide and sum
AG/ # Get GCD and divide
Japt, 12 bytes
l
/yNÎõ@/XÃx
l\n/yNÎõ@/XÃx :Implicit input of integer U
l :Factorial
\n :Reassign to U
/ :Divide by
y :GCD with
N : Array of all inputs
Î : First element (i.e., original input)
õ : Range [1,NÎ]
@ : Map each X
/X : Divide U by X
à : End map
x : Reduce by addition
Whispers v3, 79 bytes
> Input
>> 1!
>> 2∕R
>> (1]
>> Each 3 4
>> ∑5
>> 6⊓2
>> 2∕7
>> Output 8
Same idea as Lynn's Jelly answer.
Husk, 6 bytes
is\ṁ\ḣ
Not too hard in Husk since it can do math with rational numbers. The hard part was extracting the denominator, I ended up inverting the fraction, converting it to string, and getting the first number from that string.
Explanation
is\ṁ\ḣ
ḣ range [1..n]
ṁ sum the result of this function for each number:
\ inverse
\ invert
s convert to string
i get the first number in the string (the numerator)
PHP, 81 Bytes
for($p=1;$z++<$argn;$n=$n*$z+$p/$z)$p*=$z;for($t=1+$n;$p%--$t||$n%$t;);echo$p/$t;
Axiom, 34 bytes
f(x)==denominator(sum(1/n,n=1..x))
test
(24) -> [[i,f(i)] for i in 1..30]
(24)
[[1,1], [2,2], [3,6], [4,12], [5,60], [6,20], [7,140], [8,280], [9,2520],
[10,2520], [11,27720], [12,27720], [13,360360], [14,360360], [15,360360],
[16,720720], [17,12252240], [18,4084080], [19,77597520], [20,15519504],
[21,5173168], [22,5173168], [23,118982864], [24,356948592],
[25,8923714800], [26,8923714800], [27,80313433200], [28,80313433200],
[29,2329089562800], [30,2329089562800]]
Type: List List Expression Integer
Python 3, 153 150 146 142 bytes
I'm sure this can golfed further. But I'm new here
f=lambda x:0**x or x*f(x-1)
z=int(input());i=f(z)
r=sum(i/y for y in range(1,z+1))
p=lambda a,b:a if b<1else not a%b+b or p(b,a%b)
print(i/p(r,i))
Mathematica, 26 bytes
Denominator@Tr[1/Range@#]&
An unnamed function taking n as input and returning the denominator. Uses the standard trick of abusing Tr (trace) to sum the list of reciprocals.
Haskell, 52
Import Data.Ratio
f n=denominator$sum[1%k|k<-[1..n]]
If the file is loaded into GHCI, f can be used as a function.
Hoon, 95 bytes
|=
@
=+
n=(gulf 1 +<)
=+
f=(roll n mul)
(div f d:(egcd f (roll (turn n |=(@ (div f +<))) add)))
Create list [1...n], fold over it with ++mul for the factorial, create list [n!/1, n!/2, ... n!/n] and sum it, find GCD of n! and the list, and divide the factorial by that number.
There's probably a much easier way to calculate the denominator, but I can't figure it out :/
Ruby, 57 47 bytes
->n{(1..n).reduce{|a,i|a+1.to_r/i}.denominator}
Thanks to Kevin Lau for shortening this by ten bytes.
Python 2, 69 67 bytes
a=b=k=r=1
exec'a=a*k+b;b*=k;k+=1;'*input()
while r*a%b:r+=1
print r
Test it on Ideone.
How it works
Let H(n) be the sum of the multiplicative inverses of the first n positive integers. At all times, we have that a / b = 1 + H(k - 1). In fact, a, b, and k are all initialized to 1, and 1 / 1 = 1 = 1 + H(0).
We repeat the code snippet
a=a*k+b;b*=k;k+=1;
(as a string) n (input) times and execute the result. In each step, we update a, b, and k using the identity a / b + 1 / k = ak / bk + b / bk = (ak + b) / bk.
After all copies have been executed, a / b = 1 + H(n), which has the same denominator as H(n).
The fully reduced form of a / b is (a ÷ gcd(a,b)) / (b ÷ gcd(a,b)). Instead of calculating the greatest common divisor, we initialize r as 1 and keep incrementing r until ra is a multiple of b.
Clearly, this makes ra the least common multiple of a and b. Since gcd(a,b) · lcm(a,b) = ab, we have that b ÷ gcd(a,b) = lcm(a,b) ÷ a = ra ÷ a = r, making r the desired output.
Julia, 22 bytes
An anonymous function.
n->1.//(1:n)|>sum|>den
Mathematica, 27 bytes
An anonymous function.
Denominator@*HarmonicNumber
For example:
In[1] := (Denominator@*HarmonicNumber)[10]
Out[1] = 2520
Perl 6, 36 32 bytes
{([+] 1.FatRat X/1..$_).denominator}
{([+] 1.FatRat X/1..$_).nude[1]}
Explanation:
{
(
[+] # reduce with &infix:<+>
# the following produces a Seq of Rational numbers
# 1/1, 1/2, 1/3 ... 1/n
1.FatRat # FatRat.new: 1,1
X/ # crossed using &infix:</>
1 .. $_ # Range from 1 to the input inclusive
) # resulting in a FatRat
.nude # (nu)merator (de)nominator
.[1] # grab the denominator
}
Test:
my &hd = {([+] 1.FatRat X/1..$_).nude[1]}
say (1..10)».&hd; # (1 2 6 12 60 20 140 280 2520 2520)
say hd 100; # 2788815009188499086581352357412492142272
say chars hd 1000; # 433
say chars hd 10000; # 4345
J, 20 bytes
(!%!+.[:+/!%1+i.)@x:
Based on the approach used by @Lynn's solution.
If precision is not necessary for large values of n or if we can assume n will be passed as an extended integer, suffixed by x, a shorter solution can be used for 15 bytes.
!%!+.[:+/!%1+i.
Usage
f =: (!%!+.[:+/!%1+i.)@x:
f 30
2329089562800
(,:f"0) >: i. 15
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
1 2 6 12 60 20 140 280 2520 2520 27720 27720 360360 360360 360360
Explanation
(!%!+.[:+/!%1+i.)@x: Input: n
x: Convert n into an extended integer
i. Creates the range [0, 1, ..., n-1]
1+ Add one to each, range is now [1, 2, ..., n]
! Get factorial of n
% Divide n! by each value in the range [1, 2, ..., n]
[:+/ Sum those values
! Get n!
+. Get gcd between n! and the sum
! Get n!
% Divide n! by the gcd and return
M, 9 6 bytes
Thanks to FryAmTheEggman for saving 3 bytes! Code:
RİSg¹İ
M has a huge advantage here, because it works with fractions rather than floats. Explanation:
R # Get the list [1 ... n].
İ # Inverse each, resulting into [1/1, 1/2, 1/3, ..., 1/n].
S # Sum it up. (86021/27720 for n=12)
g¹ # Compute the greatest common denominator with n. (1/27720 for n=12)
İ # Calculate the inverse again. (27720 for n=12)
Uses the Jelly encoding. Try it online!.
Also, there is a 4-byte solution, which outputs a leading zero sometimes (e.g. 280 -> 0280). I'm not sure if this is allowed or not:
RİSV
05AB1E, 8 bytes
Code:
!йL/O¿/
Explanation:
! # Take the factorial of the input.
Ð # Triplicate this.
¹L # Get the list [1 ... input].
/O # Divide and sum up.
¿ # Get the GCD of the sum and the factorial.
/ # Divide the factorial by this.
There might be some accuracy problems for n > 19 due to Python's division... Uses the CP-1252 encoding.
MATL, 14 13 bytes
:p:G:!/s1\&X<
Explanation
For input N, the output is upper-bounded by N! (factorial of N). The code computes n/k for n = 1, ..., N! and for k = 1, ..., N. Then it sums over k, which gives the harmonic number multiplied by each n. The desired result is the index n of the first of those values that is an integer.
JavaScript (ES6), 88 bytes
m=>{for(d=1,i=0;i<m;d*=++i);for(n=i=0;i<m;n+=d/++i);for(g=d;g;[g,n]=[n%g,g]);return d/n}
Only works up to m=20 because of the limits of JavaScript's numeric precision.
Jelly, 9 bytes
!©÷RSg®®÷
Argument: n
! ÷R Compute [n!÷1, n!÷2, … n!÷n].
© (And store n! in the register.)
S Find the sum of this list.
g® GCD with n!.
®÷ Divide n! by this GCD.