| Bytes | Lang | Time | Link |
|---|---|---|---|
| 080 | CASIO BASIC CASIO fx9750GIII | 250408T133857Z | madeforl |
| 013 | APLNARS | 250407T080524Z | Rosario |
| 027 | APLDyalog Unicode | 210521T142126Z | Ruan |
| 025 | MATLAB/Octave | 210520T205429Z | elementi |
| 111 | Core Maude | 210519T034851Z | Chris Bo |
| 138 | Java | 210518T012313Z | Unmitiga |
| 091 | Python 3.8 | 210518T120712Z | Noodle9 |
| 040 | Charcoal | 210518T112509Z | Neil |
| 070 | Excel | 210518T111519Z | Axuary |
| 081 | Nim | 210518T090013Z | xigoi |
| 014 | Vyxal | 210518T065326Z | wasif |
| 027 | Ruby | 210518T063448Z | G B |
| 006 | Stax | 210518T044541Z | Razetime |
| 011 | J | 210518T032333Z | Bubbler |
| 013 | J | 210518T024357Z | Jonah |
| 024 | Factor + math.unicode | 210518T020048Z | chunes |
| 014 | Wolfram Language Mathematica | 210518T021322Z | att |
| 068 | JavaScript ES7 | 210518T013108Z | Arnauld |
| 012 | Jelly | 210518T011838Z | Unrelate |
| 028 | Haskell | 210518T012357Z | NoLonger |
| 029 | Raku | 210518T011758Z | Sean |
| 004 | M | 210518T004605Z | hyperneu |
CASIO BASIC (CASIO fx-9750GIII), 80 bytes
?→N
?→M
Prod Seq(K^M,K,1,N,1→B
Σ(B÷K^M,K,1,N→A
A→Q
B→R
While R
Q-RInt (Q÷R
R→Q
Ans→R
WhileEnd
A÷Q◢
B÷Q
prompts you for N first, then M
outputs the numerator, then the denominator after you press the EXE button
works for all the tests except for N=6, M=7. the output for such is:
2.022868017ᴇ+13
2.006122601ᴇ+13
I'm pretty sure this is a problem with the calculator and not the code, but I could be wrong.
Due to how large numbers are handled in CASIO BASIC, I had to calculate GCD and Modulo in the code, despite there being functions already for the two. (in fact, there's 2 functions for modulo, MOD( and Rmdr lol)
APL(NARS), 13 chars
{+/÷⍵*⍨⍳⍺+0x}
test:
3{+/÷⍵*⍨⍳⍺+0x}7
282251r279936
5{+/÷⍵*⍨⍳⍺+0x}7
2822716691183r2799360000000
APL(Dyalog Unicode), 31 27 bytes SBCS
{(⊢÷∨/){⍵,⍨+/⍵÷x}∧/x←⍵*⍨⍳⍺}
(⊢÷∨/){⍵,⍨+/⍵÷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 -- (⊢÷∨/).
MATLAB/Octave, 25 bytes
@(n,m)sum(1./sym(1:n).^m)
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 ).
Core Maude, 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.
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);}
$$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;}
Python 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)
Inputs \$n\$ and \$m\$ and returns the numerator and denominator of \$H_{n,m}\$ as a tuple.
Uses the formula from Arnauld's JavaScript answer.
Charcoal, 40 bytes
≔X…·¹NNθ≔Πθη≔Σ÷ηθζ⊞υζ⊞υηWζ«≔﹪ηιζ≔ιη»I÷υη
Try it online! Link is to verbose version of code. Explanation:
≔X…·¹NNθ
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.
Wζ«≔﹪ηιζ≔ιη»
Find the GCD.
I÷υη
Divide the saved values by the GCD.
Excel, 70 bytes
=LET(k,SEQUENCE(A1)^B1,d,PRODUCT(k),n,SUM(d/k),g,GCD(n,d),n/g&"/"&d/g)
Nim, 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
J, 11 bytes
1#.#\@$^-@]
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
Factor + math.unicode, 35 24 bytes
[ [1,b] 0 rot - v^n Σ ]
-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 from1ton.0 rotPush0and bringmto the top of the stack.-Subtractmfrom0.v^nRaise every element in the range to the-mpower.ΣSum.
Wolfram Language (Mathematica), 14 bytes
HarmonicNumber
Naturally.
Alternatively, 16 bytes without the built-in:
Tr[Range@#^-#2]&
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]
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]
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]
Jelly, 13 12 bytes
*€:@S,:gɗɗP$
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.
Haskell, 28 bytes
n#m=sum$map((1%).(^m))[1..n]
Raku, 29 bytes
{sum((1/1..$^n)X**-$^m).nude}
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.
M, 4 bytes
Rİ*S
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