[Question]
  [
The [generalised harmonic number](https://en.wikipedia.org/wiki/Harmonic_number#Generalized_harmonic_numbers) of order \$m\$ of \$n\$ is
$$H\_{n,m} = \sum\_{k=1}^n \frac 1 {k^m}$$
For example, the [harmonic numbers](https://en.wikipedia.org/wiki/Harmonic_number) are \$H\_{n,1}\$, and \$H\_{\infty,2} = \frac {\pi^2} 6\$. These are related to the [Riemann zeta function](https://en.wikipedia.org/wiki/Riemann_zeta_function) as
$$\zeta(m) = \lim\_{n \to \infty} H\_{n,m}$$
Given two positive integers \$n > 0\$, \$m > 0\$, output the *exact* rational number \$H\_{n,m}\$. The fraction should be reduced to its simplest term (i.e. if it is \$\frac a b\$, \$\gcd(a, b) = 1\$). You may output as a numerator/denominator pair, a rational number or any clear value that distinguishes itself as a rational number. You **may not** output as a floating point number.
This is [code-golf](/questions/tagged/code-golf "show questions tagged 'code-golf'"), so the shortest code in bytes wins
## Test cases
```
n, m -> Hₙ,ₘ
3, 7 -> 282251/279936
6, 4 -> 14011361/12960000
5, 5 -> 806108207/777600000
4, 8 -> 431733409/429981696
3, 1 -> 11/6
8, 3 -> 78708473/65856000
7, 2 -> 266681/176400
6, 7 -> 940908897061/933120000000
2, 8 -> 257/256
5, 7 -> 2822716691183/2799360000000
```
  
[Answer]
# [M](https://github.com/DennisMitchell/m), 4 bytes
```
Rİ*S
```
[Try it online!](https://tio.run/##y/3/P@jIBq3g////G/83BwA "M – Try It Online")
Obligatory trivial answer ;-;
```
Rİ*S  Main Link; takes n on the left and m on the right
R     Range: [1, 2, 3, ..., n]
 İ    Inverse/Reciprocal: [1, 1/2, 1/3, ..., 1/n]
  *   Exponent: [1, 1/2^m, 1/3^m, ..., 1/n^m]
   S  Sum
```
[Answer]
# [Wolfram Language (Mathematica)](https://www.wolfram.com/wolframscript/), 14 bytes
```
HarmonicNumber
```
[Try it online!](https://tio.run/##y00syUjNTSzJTE78n2b73yOxKDc/LzPZrzQ3KbXof0BRZl5JtLKuXZqDg3KsWl1wcmJeXTVXtbGOgnmtDle1mY6CCYg21VEwBdEmOgoWIBoobwiiLXQUjEG0uY6CEVQ9WJ8RVJ0piM9V@x8A "Wolfram Language (Mathematica) – Try It Online")
Naturally.
---
Alternatively, **16 bytes** without the built-in:
```
Tr[Range@#^-#2]&
```
[Try it online!](https://tio.run/##y00syUjNTSzJTE78n2b7P6QoOigxLz3VQTlOV9koVu1/QFFmXkm0sq5dmoODcqxaXXByYl5dNVe1sY6Cea0OV7WZjoIJiDbVUTAF0SY6ChYgGihvCKItdBSMQbS5joIRVD1YnxFUnSmIz1X7HwA "Wolfram Language (Mathematica) – Try It Online")
[Answer]
# [Raku](http://raku.org/), 29 bytes
```
{sum((1/1..$^n)X**-$^m).nude}
```
[Try it online!](https://tio.run/##Fck9CoAgAAbQq3yIREYZ0u9gnaMpCdIpK4qGsM5uOb3h7fpYam9vRAadd@dl41jkgnM6rmxIkoyOlvH1mvXrz@kGoQpdD2fwUPUSmO2AbFD0KWSJOlChCrQoA@I//wE "Perl 6 – Try It Online")
This function takes two arguments, `$^m` and `$^n`. `1/1 .. $^n` is a sequence of rational numbers from `1` to the second argument. `X** -$^m` produces the exponentiated cross product of that list with the negative of the first argument. `sum` sums those rational numbers, and `.nude` produces a two-element list of the numerator and denominator of the sum.
[Answer]
# JavaScript (ES7), 68 bytes
A 1-byte shorter, slightly less readable version with a single recursive call. This is otherwise identical to the commented version below.
```
m=>g=(n,N=0,D=1)=>D?g(n-!!n,n?p=N*n**m+D:D,n?q=D*n**m:N%D):[p/N,q/N]
```
[Try it online!](https://tio.run/##PU9LbuJAEN37FJVIkbuhcf/s/hA1bDyz9CZLQiQEhjiCNtgkUhTNDUaazSxn7jHnyQXmCKTtWKlFSa/q1XuvnlYvq3bdVMfzxNeb8rJ1l4Ob7RzypHCM5I5jN8vnO@QnV1ee@PnRFSM/Gh3G@TQP8OTyHk6LmxxPF0dakBMtlpemPD1XTYnibRvjpClXm@/Vvrx79WvEcHKu785N5XcIJ@1xX53R9b2/xsm2br6t1o@oBTeDtwhg4QkcCDRluwQH7UCmiwd6v1mOKb4NnHXt23pfJvs6hOzpi4JA3h1s0QEjjwkUMIaYxqHn4FynB3OI///79f7nd@gxTCF@//szDoI/8EUS0DCZgTBCZJwKba1UkSKQdlOeMs6l4pQLq1ioKCOQdRvDFGdGME211v2KRSkB0@1SybWUKbM0FdYarqyKgg/vFTlVkSEgO6CNZibVkqrMZJ1IpAmIPo5SygRbrdIwVUNIGzSZMVYHc2ql5IJ9ViQGa5FpKjLVxfx6S3OlLOdGDt8NNx8 "JavaScript (Node.js) – Try It Online")
---
# JavaScript (ES7), 69 bytes
Expects `(m)(n)`. Returns `[numerator, denominator]`.
```
m=>g=(n,N=0,D=1)=>n?g(n-1,p=N*n**m+D,q=D*n**m):D?g(0,D,N%D):[p/N,q/N]
```
[Try it online!](https://tio.run/##PU/LrtMwEN3nK0ZXQrFbN/bYiR9F7t0EltncZW@RqjYtQa3TJgUJIf4AiQ1L@A@@5/4An1CcEDGLkT3nzDlnPmw/bftd11xui9Du6/vB389@dfQksMoLVnqkfhUejyQskF18NQuz2XlesqsvxyddlhGMRFa9KulyfeEVu/Jqc@/q68emq0l66FOadfV2/7Y51U@fw44Imt3ap1vXhCOhWX85NTfy8BweaHZouzfb3XvSg1/BlwRgHRicGXR1vwEP/UTm63f8eb@Zc/o6cnZt6NtTnZ3amHKkrysG5bBwIGdKAmVQwRxSnsZegveDHjxC@uf395efP2JPYQnpy69vaRT8Su@KgYHFCqSVskAujXNKJ5pBPkwxF4hKI0fptIiVFAyKAbFCo7BSGG6MGSGR5AzsgOUKjVK5cDyXzlnUTifRB0dF5DqxDNTwMdYImxvFdWGLQSQxDOQYR2tto63ReZzqKaSLmsJaZ6I5d0qhFP8qkZO1LAyXhR5i/j/LoNYO0arpumnnLw "JavaScript (Node.js) – Try It Online")
### How?
The recursive function \$g\$ first computes the unreduced numerator and denominator \$(N,D)\$ of \$H\_{n,m}\$ and saves a copy of the final result into \$(p,q)\$:
```
n ? g(n - 1, p = N * n**m + D, q = D * n**m) : ...
```
When \$n=0\$, it enters its 2nd phase where the GCD of \$(p,q)\$ is computed in \$N\$:
```
... : D ? g(0, D, N % D) : ...
```
When \$D=0\$, it eventually returns:
```
... : [p / N, q / N]
```
[Answer]
# [J](http://jsoftware.com/), 13 bytes
```
1#.(%@^~1+i.)
```
[Try it online!](https://tio.run/##VZDLakIxEIb3fYqhpVhpe5hLMhdBKBRcufIB3JRKdSPkAXz10zmaQ20gm3xf8v@Z0/g4LA6wXsEC3gBhlft9gM/ddjPS0/Dy/LG/0OtxWI7Lh@@vnzOwM1dqbBGisAaDAwjcGBUkEqVGHIq5kpfk2rmjEjqjNTO7CpNR06jdKEImUjBa4QgnjSnD0yhzBrXpiO5izQ29mDStXvX2qCT3zllVPUuZlivjZNZZZBa6h2W1FiLEiHMxu6vO1RrXuQzD3zCMVIPIpc/k//382PgL "J – Try It Online")
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), ~~13~~ 12 bytes
```
*€:@S,:gɗɗP$
```
[Try it online!](https://tio.run/##y0rNyan8/1/rUdMaK4dgHav0k9NPTg9Q@X94uX7Wo4a5@pqR//9HG@somMfqKESb6SiYgGhTHQVTEG2io2ABooHyhiDaQkfBGESb6ygYQdWD9RlB1ZmC@AA "Jelly – Try It Online")
A byte less than `*€µP:¹S,P:g/$`, but the abuse of `ɗ` leads me to believe this can be shorter yet.
```
*€              Raise each 1 .. n to the power of m.
         ɗP$    For that list and the product of its elements:
  :@            divide each by the product
    S           and sum;
        ɗ       for that sum:
     ,          pair it with the product,
      :         and divide both by
       g        the GCD of the sum and product.
```
[Answer]
# Java, ~~155~~ 138 bytes
```
int g(int a,int b){return b<1?a:g(b,a%b);}
m->n->{int p=1,d=0,t;for(;n>0;d=d*t+p,p*=t)t=(int)Math.pow(n--,m);return d/g(d,p)+"/"+p/g(d,p);}
```
[Try it online!](https://tio.run/##ZU89b4MwEJ3jX2FFqmSDcRJ1qFQHulXqkClj1cFgoKZgW@ZIFSF@OzWtt95wd@/dx7vr5E1mnfpa9eCsB9wFzCfQPW8mU4G2hicCuansdYWrXo4jvkht8IxwsMiPICGEm9UKD6FKruC1ad8/pG9HOqPda9x1fjNQt7Vn/4i/iaJo8nXICpMVszaAXX5iKj8yEI31RJjiKFSuEkgdc0kOFHIS2uhFwid39puYLGMDFb6GyRusDi1RzNF0f9inLgKxrDuBdtf7CPXA7QTcBWXoDWm4dK6/kycak0dKxe@bS/wQbTe1mySWbPMlnaNWeT69yOeWlEw@lEEELesP)
$$Denominator\_{n, m} = \prod\_{k=1}^n k^m$$
$$Numerator\_{n,m} = \sum\_{k=1}^n \frac {Denominator\_{n,m}} {k^m} = Numerator\_{n-1,m} \times k^m + Denominator\_{n-1,m}$$
# Java + Commons Lang 2, 127 bytes
```
m->n->{org.apache.commons.lang.math.Fraction x=null;for(x=x.ZERO;n>0;)x=x.add(x.getFraction(1,(int)Math.pow(n--,m)));return x;}
```
[Answer]
# [Factor](https://factorcode.org/) + `math.unicode`, ~~35~~ 24 bytes
```
[ [1,b] 0 rot - v^n Σ ]
```
[Try it online!](https://tio.run/##VVFNT8MwDL33V5g7W@J82AlIuyIuXBCnaUilBJjG2qoNk1C1X8P/4S@V0LUdOFKk2O89PzsveRGrpn@4v727uYK8bauihX0e34Zr2eTla2hhF5oyvJ9SH@W2qJ7D6XEIv/QW6ibE@Fk32zLCdZZ1GaTogEHDES5gsQLllLIoFHuvaSwboKmMRiJqQoHKk0wxQmw6I8RJQumUZMHMA2YCuaQ0goxG1tpIL4zy3iH5qRmevSCKKasTecyyY@kMa0HWWTqrqzTGNAQRueSRycxlPg/hU1/pnOfkVHitUUn536ealSwLZWnWsH/3xEjkEZ0e1zWpHPs1rPHyaQMSmirCAg6PJXx/wabvEn/4vdU@r6GNebFb9j8 "Factor – Try It Online")
*-11 thanks to @Bubbler!*
## Explanation:
It's a quotation (anonymous function) that takes two integers from the data stack as input and leaves a fully-reduced mixed fraction (it's simply the way that Factor is) on the data stack as output.
* `[1,b]` Make a range from `1` to `n`.
* `0 rot` Push `0` and bring `m` to the top of the stack.
* `-` Subtract `m` from `0`.
* `v^n` Raise every element in the range to the `-m` power.
* `Σ` Sum.
[Answer]
# [J](http://jsoftware.com/), 11 bytes
```
1#.#\@$^-@]
```
[Try it online!](https://tio.run/##TZC5agMxEIb7PMUQB0wgXuaQ5jAYDAZXqVLbaUJMSGNQ5bdfS15tsgIV0nz8x/yOz8P6ArstrOENELb1bgY4fLwfR1oNq9P@5XOzP4@vT99fP1eQG1yAb9ODnTlTYYsQhd00tD6khESiVIhDsZ4KaANSBxyV0BmtmNmDaEhuSO5IEjKRhFESRzhpNJvUEJ9tqPxZU/8zN/RkUjR71knXGyBzcFX1msw0PYa2bBXVD93Dar4SIsSIuCwwN@RshXNz52WithUj1SBy6cv5V8iTwngH "J – Try It Online")
TFW J beats Jelly...
Used as `n f m`, where `n` and `m` are given as extended-precision integers.
### How it works
```
1#.#\@$^-@]    NB. dyadic train; left = n, right = m
   #\@$        NB. 1..n in a dyadic context:
     @$        NB.   reshape m into dimension n, and then
   #\          NB.   get the lengths of prefixes
       ^       NB. each raised to the power of
        -@]    NB. -m
1#.            NB. sum of them
```
[Answer]
# [Vyxal](https://github.com/Lyxal/Vyxal), 14 bytes
```
ɾ$eDΠ$/∑$Π":ġ/
```
[Try it Online!](http://lyxal.pythonanywhere.com?flags=&code=%C9%BE%24eD%CE%A0%24%2F%E2%88%91%24%CE%A0%22%3A%C4%A1%2F&inputs=8%0A3&header=&footer=)
[Answer]
# [Nim](http://nim-lang.org/), 81 bytes
```
import math,rationals
func H(n,m:int):any=
 var r=0//1;for i in 1..n:r+=1//i^m
 r
```
[Try it online!](https://tio.run/##PcvBCsIwDADQe78iR4thtWxT6ejdrxDCcCxg0xGrsK@vHqS3d3nCqVZOW9YCicqKSoWz0PNllrfMcDsIpsBSbCDZo4EPKWg8OeenJSswsIDvOgl6jN45vicDWh/zmn@3x4s1f59xaB5xbB7w2tyjt/UL "Nim – Try It Online")
[Answer]
# Haskell, 28 bytes
```
n#m=sum$map((1%).(^m))[1..n]
```
[Answer]
# [Stax](https://github.com/tomtheisen/stax), 6 [bytes](https://github.com/tomtheisen/stax/blob/master/docs/packed.md#packed-stax)
```
ê☺σ;vù
```
[Run and debug it](https://staxlang.xyz/#p=8801e53b7697&i=+7+,3%0A+4+,6%0A+5+,5%0A+8+,4%0A+1+,3%0A+3+,8%0A+2+,7%0A+7+,6%0A+8+,2%0A+7+,5%0A&m=2)
just another trivial answer.
[Answer]
# [Ruby](https://www.ruby-lang.org/), 27 bytes
```
->n,m{(1..n).sum{|x|x**-m}}
```
[Try it online!](https://tio.run/##KypNqvyfZvtf1y5PJ7daw1BPL09Tr7g0t7qmoqZCS0s3t7b2f4FCWrSxjnnsfwA "Ruby – Try It Online")
[Answer]
# [Python 3.8](https://docs.python.org/3.8/), 91 bytes
```
import math
f=lambda n,m,N=0,D=1:n and f(n-1,m,N*n**m+D,D*n**m)or(N/(G:=math.gcd(N,D)),D/G)
```
[Try it online!](https://tio.run/##PVHJTsMwEL3nK@YWu0ypl8RLpXCK1Ft/ADiUJKUVxIkSIwFVv73Ybosvb@Z55s02/vjD4KQZp8vl2I/D5KHf@UO2rz53/Vu7A4c9biuGdcXXDnauhT1xSx7ZhVss@oca62TQYSLbFdmsqyjw@N60ZIs1pVivNvTiu9nPUAEhEkFTBKIQioglQhmxQDARwz@PaBBkRI0gbvEpT9ziyujTrPseu8Z3bdIWRoiShwRtrVQxiheMc6kCx4VVLLykzRRnRrCgrbVOdOILybWUBbOhN2Gt4cpeVUJ@MrTRzBQ69KhKU6pbmlBKmVhCq@LK2KDBjLGaxdJWSi4Y@y8jyjhUmRRjy5orZTk38t75PZZmzaFrPropTpe/fAldNDlCsrjMabYfJvDYwdHB73EkacsI953QdQbhOYQegoJPXrxCOGHgaPLH6eg82ecnd0Y49WdYPsFpPsPpVvq5q6r59ZzTyx8 "Python 3.8 (pre-release) – Try It Online")
Inputs \$n\$ and \$m\$ and returns the numerator and denominator of \$H\_{n,m}\$ as a tuple.
Uses the formula from [Arnauld](https://codegolf.stackexchange.com/users/58563/arnauld)'s [JavaScript answer](https://codegolf.stackexchange.com/a/225865/9481).
[Answer]
# [APL(Dyalog Unicode)](https://dyalog.com), ~~31~~ 27 bytes [SBCS](https://github.com/abrudz/SBCS)
```
{(⊢÷∨/){⍵,⍨+/⍵÷x}∧/x←⍵*⍨⍳⍺}
```
[Try it on APLgolf!](https://razetime.github.io/APLgolf/?h=AwA&c=q9Z41LXo8PZHHSv0Nasf9W7VedS7QlsfyDi8vaL2Ucdy/YpHbROAXC2g@KPezY96d9UCAA&f=RY65FQIxDERzqlBoRzqtoyFKISOgAHLqoJStBO@uAUXz/ow02m6PK75f0BSiNwfrbcDozSD7ZNxbgvYWILs7I7IbY6rLdn@CpMhglKhSPwgbMaszspTTnIMmOVMKBUbEgU9uyqFqVGhSley1rjCeIjIoLRR95PDvmrh7zopwW6TmDcqsmEVYqixE/xoZgTL893KwezGnrs9X9gM&i=AwA&r=tryAPL&l=apl-dyalog&m=dfn&n=f)
`(⊢÷∨/){⍵,⍨+/⍵÷x}∧/x←⍵*⍨⍳⍺` is equivelent to `x←(⍳⍺)*⍵⋄(⊢÷∨/){⍵,⍨+/⍵÷x}∧/x`.
APL translated to Python:
```
from math import *
def H(n, m):
    x = [i**m for i in range(1,n+1)] # x←(⍳⍺)*⍵
    tmp = lcm(*x) # ∧/x
    tmp = (sum(tmp//i for i in x), tmp) # {⍵,⍨+/⍵÷x}
    tmp = [e//gcd(*tmp) for e in tmp] # (⊢÷∨/)
    return tmp
```
First I make a list from `1` to `n` -- `⍳⍺`.
Then I raise every element of the list to the power of `m` to calculate the value of all the denominators, and assign it to the variable `x` -- `x ← (⍳⍺)*⍵`.
I then calculate the denominator after all the fractions are added together by taking the least common multiple of all the elements of `x` (all the denominators) -- `∧/x`.
Next I create a two element tuple, the second element of which is the denominator -- `{⍵,⍨...}`. The first element is the numerator, calculated as the sum of the denominator divided by each of the original denominators -- `+/⍵÷x`.
Lastly, I simplify the fraction by dividing it by it's gcd -- `(⊢÷∨/)`.
[Answer]
# Excel, 70 bytes
```
=LET(k,SEQUENCE(A1)^B1,d,PRODUCT(k),n,SUM(d/k),g,GCD(n,d),n/g&"/"&d/g)
```
[Link to Spreadsheet](https://1drv.ms/x/s!AkqhtBc2XEHJnCkQ0jwaai8YU5Ye?e=F4CVhU)
[Answer]
# [Charcoal](https://github.com/somebody1234/Charcoal), 40 bytes
```
≔Ｘ…·¹ＮＮθ≔Πθη≔Σ÷ηθζ⊞υζ⊞υηＷζ«≔﹪ηιζ≔ιη»Ｉ÷υη
```
[Try it online!](https://tio.run/##ZY3LCsIwEEXX5iu6nIAuBBdCV6KbLpSiXxDT0Aykic2jhYrfHlNtUXE3M/eeM1wyyw1TMe6cw1pDaXphodBcBYedODNdC1gvs0Lfgj@F5ppSSv/3luZkVlhTBe6hTWf5OV9Ck7z@gB1WAuSIpMKQCmVwEsLvPIK9RCUyGGh2J4vJckxuZUYcJ3pO8A09SGlRe9gz57/@vZSU5jFuyDauOvUE "Charcoal – Try It Online") Link is to verbose version of code. Explanation:
```
≔Ｘ…·¹ＮＮθ
```
Generate the first `n` powers of `m`.
```
≔Πθη≔Σ÷ηθζ
```
Calculate the product, then divide that by each power and take the sum.
```
⊞υζ⊞υη
```
Save the values for later.
```
Ｗζ«≔﹪ηιζ≔ιη»
```
Find the GCD.
```
Ｉ÷υη
```
Divide the saved values by the GCD.
[Answer]
# [Core Maude](http://maude.cs.illinois.edu/w/index.php/The_Maude_System), 111 bytes
```
fmod H is pr RAT . op __ : Nat Nat -> Rat . vars N M : Nat . eq 0 M = 0 . eq(s N)M = 1 / s N ^ M +(N M) . endfm
```
### Example Session
```
             \||||||||||||||||||/
           --- Welcome to Maude ---
             /||||||||||||||||||\
         Maude 3.1 built: Oct 12 2020 20:12:31
         Copyright 1997-2020 SRI International
           Tue May 18 23:37:06 2021
Maude> fmod H is pr RAT . op __ : Nat Nat -> Rat . vars N M : Nat . eq 0 M = 0 . eq(s N)M = 1 / s N ^ M +(N M) . endfm
Maude> red 3 7 .
reduce in H : 3 7 .
rewrites: 17 in 0ms cpu (0ms real) (17206 rewrites/second)
result PosRat: 282251/279936
Maude> red 6 4 .
reduce in H : 6 4 .
rewrites: 40 in 0ms cpu (0ms real) (~ rewrites/second)
result PosRat: 14011361/12960000
Maude> red 3 1 .
reduce in H : 3 1 .
rewrites: 17 in 0ms cpu (0ms real) (~ rewrites/second)
result PosRat: 11/6
Maude> red 2 8 .
reduce in H : 2 8 .
rewrites: 10 in 0ms cpu (0ms real) (~ rewrites/second)
result PosRat: 257/256
Maude> red 5 7 .
reduce in H : 5 7 .
rewrites: 32 in 0ms cpu (0ms real) (~ rewrites/second)
result PosRat: 2822716691183/2799360000000
```
### Ungolfed
```
fmod GENERALIZED-HARMONIC-NUMBERS is
    protecting RAT .
    op H : Nat Nat -> Rat .
    vars N M : Nat .
    eq H(0, M) = 0 .
    eq H(s N, M) = 1 / (s N ^ M) + H(N, M) .
endfm
```
`H(N, M)` is the naive recursive definition of \$H\_{n, m}\$. Maude's built-in rational number module automatically reduces fractions to lowest common denominator.
There's not *much* golfing we can do because only a handful of characters can separate identifiers (`(`, `)`, `[`, `]`, `{`, `}`, `,`, and space). I've renamed the function operator `H` to `__` (juxtaposition of two values) in the golfed version to save a few bytes.
[Answer]
# MATLAB/Octave, 25 bytes
```
@(n,m)sum(1./sym(1:n).^m)
```
[Try it online!](https://tio.run/##y08uSSxL/f/fQSNPJ1ezuDRXw1BPv7gSSFnlaerF5Wr@T7NNzCu25krTMNYx1wRSZjomIMpUxxREmehYaILlDEGUhY4xiDLXMYKoBGswgigxBfL@AwA "Octave – Try It Online")  
Anonymous function. Returns accurate value of symbolic type, which can represent real numbers (so rational numbers including).  
Unfortunatelly, to my suprise, MATLAB doesn't implement that function, only non-generalized harmonic numbers (so only for ![m=1](https://chart.googleapis.com/chart?cht=tx&chl=%5E%7Bm%3D1%7D&chs=17)).
]