Harmonic series: Difference between revisions
(→{{header|Phix}}: added a standard floating point version) |
|||
Line 265: | Line 265: | ||
One Hundredth: |
One Hundredth: |
||
14466636279520351160221518043104131447711/2788815009188499086581352357412492142272 |
14466636279520351160221518043104131447711/2788815009188499086581352357412492142272 |
||
(one based) Index of first value: |
|||
greater than 1: 2 (second term) |
|||
greater than 2: 4 (fourth term) |
|||
greater than 3: 11 (eleventh term) |
|||
greater than 4: 31 (thirty-first term) |
|||
greater than 5: 83 (eighty-third term) |
|||
greater than 6: 227 (two hundred and twenty-seventh term) |
|||
greater than 7: 616 (six hundred and sixteenth term) |
|||
greater than 8: 1,674 (one thousand, six hundred and seventy-fourth term) |
|||
greater than 9: 4,550 (four thousand, five hundred and fiftieth term) |
|||
greater than 10: 12,367 (twelve thousand, three hundred and sixty-seventh term) |
|||
</pre> |
|||
=== using standard floats === |
|||
<!--<lang Phix>(phixonline)--> |
|||
<span style="color: #004080;">integer</span> <span style="color: #000000;">n</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">gn</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span> |
|||
<span style="color: #004080;">atom</span> <span style="color: #000000;">hn</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span> |
|||
<span style="color: #004080;">sequence</span> <span style="color: #000000;">gt</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span> |
|||
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"First twenty harmonic numbers as fractions:\n"</span><span style="color: #0000FF;">)</span> |
|||
<span style="color: #008080;">while</span> <span style="color: #000000;">gn</span><span style="color: #0000FF;"><=</span><span style="color: #000000;">10</span> <span style="color: #008080;">do</span> |
|||
<span style="color: #008080;">if</span> <span style="color: #000000;">n</span><span style="color: #0000FF;"><=</span><span style="color: #000000;">20</span> <span style="color: #008080;">then</span> |
|||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%18.15f%s"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">hn</span><span style="color: #0000FF;">,</span><span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">mod</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5</span><span style="color: #0000FF;">)?</span><span style="color: #008000;">" "</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\n"</span><span style="color: #0000FF;">)})</span> |
|||
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span> |
|||
<span style="color: #008080;">if</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">=</span><span style="color: #000000;">100</span> <span style="color: #008080;">then</span> |
|||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\nOne Hundredth: %18.15f\n\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">hn</span><span style="color: #0000FF;">})</span> |
|||
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span> |
|||
<span style="color: #008080;">if</span> <span style="color: #000000;">hn</span><span style="color: #0000FF;">></span><span style="color: #000000;">gn</span> <span style="color: #008080;">then</span> |
|||
<span style="color: #000000;">gt</span> <span style="color: #0000FF;">&=</span> <span style="color: #000000;">n</span> |
|||
<span style="color: #000000;">gn</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span> |
|||
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span> |
|||
<span style="color: #000000;">n</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span> |
|||
<span style="color: #000000;">hn</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">/</span><span style="color: #000000;">n</span> |
|||
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span> |
|||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"(one based) Index of first value:\n"</span><span style="color: #0000FF;">)</span> |
|||
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">gt</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span> |
|||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">" greater than %2d: %,6d (%s term)\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">i</span><span style="color: #0000FF;">,</span><span style="color: #000000;">gt</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">],</span><span style="color: #7060A8;">ordinal</span><span style="color: #0000FF;">(</span><span style="color: #000000;">gt</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">])})</span> |
|||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span> |
|||
<span style="color: #0000FF;">{}</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">wait_key</span><span style="color: #0000FF;">()</span> |
|||
<!--</lang>--> |
|||
{{out}} |
|||
<pre> |
|||
First twenty harmonic numbers as fractions: |
|||
1.000000000000000 1.500000000000000 1.833333333333333 2.083333333333333 2.283333333333333 |
|||
2.450000000000000 2.592857142857143 2.717857142857143 2.828968253968254 2.928968253968254 |
|||
3.019877344877345 3.103210678210678 3.180133755133755 3.251562326562327 3.318228993228994 |
|||
3.380728993228994 3.439552522640758 3.495108078196314 3.547739657143682 3.597739657143682 |
|||
One Hundredth: 5.187377517639621 |
|||
(one based) Index of first value: |
(one based) Index of first value: |
Revision as of 13:22, 27 May 2021
This page uses content from Wikipedia. The original article was at Harmonic number. The list of authors can be seen in the page history. As with Rosetta Code, the text of Wikipedia is available under the GNU FDL. (See links for details on variance) |
In mathematics, the n-th harmonic number is the sum of the reciprocals of the first n natural numbers:
- Hn = 1 + 1/2 + 1/3 + ... + 1/n
The series of harmonic numbers thus obtained is often loosely referred to as the harmonic series.
Harmonic numbers are closely related to the Riemann zeta function, and roughly approximate the natural logarithm function; differing by γ (lowercase Gamma), the Euler–Mascheroni constant.
The harmonic series is divergent, albeit quite slowly, and grows toward infinity.
- Task
- Write a function (routine, procedure, whatever it may be called in your language) to generate harmonic numbers.
- Use that procedure to show the values of the first 20 harmonic numbers.
- Find and show the position in the series of the first value greater than the integers 1 through 5
- Stretch
- Find and show the position in the series of the first value greater than the integers 6 through 10
- Related
Factor
This solution uses the following (rather accurate) approximation of the harmonic numbers to find the first indices greater than the integers:
Hn ≈ ln(n) + γ + 1/2n - 1/12n2
where γ
is the Euler-Mascheroni constant, approximately 0.5772156649
.
<lang factor>USING: formatting grouping io kernel lists lists.lazy math math.functions math.ranges math.statistics math.text.english prettyprint sequences tools.memory.private ;
! Euler-Mascheroni constant CONSTANT: γ 0.5772156649
- Hn-approx ( n -- ~Hn )
[ log γ + 1 2 ] [ * /f + 1 ] [ sq 12 * /f - ] tri ;
- lharmonics ( -- list ) 1 lfrom [ Hn-approx ] lmap-lazy ;
- first-gt ( m -- n ) lharmonics swap '[ _ < ] lwhile llength ;
"First twenty harmonic numbers as mixed numbers:" print 100 [1,b] [ recip ] map cum-sum [ 20 head 5 group simple-table. nl ] [ "One hundredth:" print last . nl ] bi
"(zero based) Index of first value:" print 10 [1,b] [
dup first-gt [ commas ] [ 1 + number>text ] bi " greater than %2d: %6s (term number %s)\n" printf
] each</lang>
- Output:
First twenty harmonic numbers as mixed numbers: 1 1+1/2 1+5/6 2+1/12 2+17/60 2+9/20 2+83/140 2+201/280 2+2089/2520 2+2341/2520 3+551/27720 3+2861/27720 3+64913/360360 3+90653/360360 3+114677/360360 3+274399/720720 3+5385503/12252240 3+2022061/4084080 3+42503239/77597520 3+9276623/15519504 One hundredth: 5+522561233577855727314756256041670736351/2788815009188499086581352357412492142272 (zero based) Index of first value: greater than 1: 1 (term number two) greater than 2: 3 (term number four) greater than 3: 10 (term number eleven) greater than 4: 30 (term number thirty-one) greater than 5: 82 (term number eighty-three) greater than 6: 226 (term number two hundred and twenty-seven) greater than 7: 615 (term number six hundred and sixteen) greater than 8: 1,673 (term number one thousand, six hundred and seventy-four) greater than 9: 4,549 (term number four thousand, five hundred and fifty) greater than 10: 12,366 (term number twelve thousand, three hundred and sixty-seven)
Julia
<lang julia>const memoizer = [BigFloat(1.0), BigFloat(1.5)]
"""
harmonic(n::Integer)::BigFloat
Calculates harmonic numbers. The integer argument `n` should be positive. """ function harmonic(n::Integer)::BigFloat
if n < 0 throw(DomainError(n)) elseif n == 0 return BigFloat(0.0) # by convention elseif length(memoizer) >= n return memoizer[n] elseif length(memoizer) + 1 == n h = memoizer[end] + BigFloat(1.0) / n push!(memoizer, h) return h else return harmonic(n - 1) + BigFloat(1.0) / n end
end
function testharmonics(upperlimit = 11)
n = 1 while (h = harmonic(n)) < upperlimit nextintegerfloor = h < 1.8 ? h > 1.0 : floor(h) > floor(memoizer[n - 1]) if n < 21 || nextintegerfloor println("harmonic($n) = $h") nextintegerfloor && println(" $n is also the term number for the first harmonic > $(floor(h))") end n += 1 end
end
testharmonics()
</lang>
- Output:
harmonic(1) = 1.0 harmonic(2) = 1.5 2 is also the term number for the first harmonic > 1.0 harmonic(3) = 1.833333333333333333333333333333333333333333333333333333333333333333333333333339 harmonic(4) = 2.083333333333333333333333333333333333333333333333333333333333333333333333333356 4 is also the term number for the first harmonic > 2.0 harmonic(5) = 2.283333333333333333333333333333333333333333333333333333333333333333333333333363 harmonic(6) = 2.450000000000000000000000000000000000000000000000000000000000000000000000000041 harmonic(7) = 2.592857142857142857142857142857142857142857142857142857142857142857142857142913 harmonic(8) = 2.717857142857142857142857142857142857142857142857142857142857142857142857142913 harmonic(9) = 2.828968253968253968253968253968253968253968253968253968253968253968253968254009 harmonic(10) = 2.928968253968253968253968253968253968253968253968253968253968253968253968253995 harmonic(11) = 3.019877344877344877344877344877344877344877344877344877344877344877344877344889 11 is also the term number for the first harmonic > 3.0 harmonic(12) = 3.103210678210678210678210678210678210678210678210678210678210678210678210678211 harmonic(13) = 3.180133755133755133755133755133755133755133755133755133755133755133755133755123 harmonic(14) = 3.251562326562326562326562326562326562326562326562326562326562326562326562326542 harmonic(15) = 3.318228993228993228993228993228993228993228993228993228993228993228993228993199 harmonic(16) = 3.380728993228993228993228993228993228993228993228993228993228993228993228993199 harmonic(17) = 3.439552522640757934875581934405463817228523110876052052522640757934875581934384 harmonic(18) = 3.495108078196313490431137489961019372784078666431607608078196313490431137489932 harmonic(19) = 3.547739657143681911483769068908387793836710245378976029130827892437799558542556 harmonic(20) = 3.597739657143681911483769068908387793836710245378976029130827892437799558542549 harmonic(31) = 4.027245195436520102759838180253409570739320924649712368107240380481568735938418 31 is also the term number for the first harmonic > 4.0 harmonic(83) = 5.002068272680166053728324750753870264345455215566438587478989543061001039767003 83 is also the term number for the first harmonic > 5.0 harmonic(227) = 6.004366708345566023376436217157408474650893771305512336984772241757969069086895 227 is also the term number for the first harmonic > 6.0 harmonic(616) = 7.001274097134160381487068933022945074864048309674852535721112060499845844673362 616 is also the term number for the first harmonic > 7.0 harmonic(1674) = 8.000485571995779067790304796519697445800341927883408389172647367923220595045883 1674 is also the term number for the first harmonic > 8.0 harmonic(4550) = 9.000208062931140339164179501268928624268799275400095995932594006439583360896694 4550 is also the term number for the first harmonic > 9.0 harmonic(12367) = 10.00004300827580769470675707492981720768686887243344211163998834649135547210551 12367 is also the term number for the first harmonic > 10.0
Using rationals
<lang julia>const harmonics = accumulate((x, y) -> x + big"1" // y, 1:12370)
println("First twenty harmonic numbers as rationals:") foreach(i -> println(rpad(i, 3), " => ", harmonics[i]), 1:20)
println("\nThe 100th harmonic is: ", harmonics[100], "\n")
for n in 1:10
idx = findfirst(x -> x > n, harmonics) print("First Harmonic > $n is: ", harmonics[idx], "\n\n")
end
</lang>
- Output:
First twenty harmonic numbers as rationals: 1 => 1//1 2 => 3//2 3 => 11//6 4 => 25//12 5 => 137//60 6 => 49//20 7 => 363//140 8 => 761//280 9 => 7129//2520 10 => 7381//2520 11 => 83711//27720 12 => 86021//27720 13 => 1145993//360360 14 => 1171733//360360 15 => 1195757//360360 16 => 2436559//720720 17 => 42142223//12252240 18 => 14274301//4084080 19 => 275295799//77597520 20 => 55835135//15519504 The 100th harmonic is: 14466636279520351160221518043104131447711//2788815009188499086581352357412492142272 First Harmonic > 1 is: 3//2 First Harmonic > 2 is: 25//12 First Harmonic > 3 is: 83711//27720 First Harmonic > 4 is: 290774257297357//72201776446800 First Harmonic > 5 is: 3672441655127796364812512959533039359//734184632222154704090370027645633600 First Harmonic > 6 is: 7210530454341478178114292924106791866448071719960766673184657267908514585008387695857601640547547//1200881092808579751109445892858157237623011602251376919557525378451885327053551694768211209584000 First Harmonic > 7 is: 32418148234584438506244669620559801586453637524407441287287956498408167324384836135154335608450103705554364675635095796463552565723833053990601655182066284053456674137000162471237344506126617371216079120095971345235097691744446098739930413505848076716148051590567//4630321250792651667959778962730272725135972962879403462498154236164728708558353376685441262094879300811505205446228182535680317471673227192574715015221837802633281044802046825192844588381756912000006314261374254853934981337617963082619967597058012665881508170240 First Harmonic > 8 is: 138141199730356031917751723608451630140926572041533266161867229808423134343694448776284861362361049658120307218628922472967074569643922410693236711646552594017799177544898923160715275467074773052672543691485042344006328346792717577331346270889819765648723902241194752204176829233330048155240810379245143159803746553605910654524991868894244655125224374793405027930983653080599361646248333997230466686371943554425849838679719632376766617639339476485319908534320267241696294676687857612211224929536683459738832437894009105247288571220395942120501422698013842452465990861198601652372525840473807472136998877762025722745653584203415416989419985647929410780044991971375219261681255528981314607252039408935696726675566989805504130221402879//17266601943998720215934555231097205020805360283925021776131771421603665580550992851730546192114435876915764081799263950036774496079790573533431946151195888729825227967898856935376506079740151630837994958679444177020560516804785312006925540853535681555388532126434276232761609235943363516882249874992501288848101663246418034482830782765799910587627015490435618612817485429045075697874945013812237906815356826216080083918931352849328226757321069450237292900402036556169613799106635542806391854137317272454464723030848621364955770185788658456980359320481573473968897297574022422610618929844739054324826111937140267616883649616842817036401014150739236179076410461198210703989272527503945999049527912447331310263532711648780174245760000 First Harmonic > 9 is: 2803922057204811543989535496612889221797101689401339733425779071893883726062730194418722759045899108563649222513327676111056373937430194191361767957531399947657882950860187200723323262653682200418079207248717851019834955117510807290247517344853824182604853734193915236890382944826941254750728989773414206859570065960640186792890664839103798433780558510849879082382309565191620282894846917562664482131202291813204472491830923673050493632880376511556753354676402403084278296415856939754434734260623967677761451430096042117604187063669249143393076890252523001247824132437003474131230135624887874799541971404156392012052221712958314528322355541629626096561834856317166367946519841625273109689443740339231545297896759513984852874078072318627511091395704617775835151474534768408675032414448963002972590468720049363107497714657975180640114587539853156735035634359134035816625004885128358508857789053399591546078553132392557669776329295754833648165907192091248381853554990310275294735796601315160268572473977239894244510958924644461475601889585298550864600912608515846981406115574447937712467306028124552530287697424685820096894634400504911945644842525080877704081133701601474836419212357427281193595545654309059483619700606698188346807113314394305087007324692154551457096864441413765832492015835459544797423019196304195496229963766867559041682555605485756372169731071238590755894190062578370776223859945908951232125318924762539326986977852885933136208484015119262988152215227210365689996539057011922584498047033647706625149283085817504520106280560129638082705378996661011957957116006944584486439354147266379272818775057457727700062964206158298239058238442033871955799108732891594547873095023995742714560400884479305635074935996707008596929664779547461570776674459690821497530879010144224813067523793975748941040890361283562073364309054362072137372458221297707667641071756676535258762295067395479041974831597965979613081474695278123265026663091006352059826017007472704096220374679625403//311539693038123496722215310551186904103082524726970170658802584453586817702845967012058559851648961565162714074357248615113237727615741409397613403942762027416418250197869235139807433555974792925305500675331577565501246819843747067123329275977858482555209853394601249731116273203314723492820718920663543363350452108859588055589207296367576316485295250028686463172112871115738305282269830733309445640796833467195761172540352742868324044527033042113461149657815168691435568328007659916683562048346389048741726140068665849102873300936534794943981012153540913055621237325128266901087332680399450826342551172911601227269655973249675723840414635853583858640961466968497444812002669481333031399627561658239662030094505016308687209615676717899935366285911202891060019766201048122485091665890488010192945266722188069264271709669076333959826410108104815827793429989042734394907438794428685359256959974813630940895555145059961227687918900885009633119015801758204737016204228694445315173976166116707848897795690334687813067638452214980721143416739719469158748498888549650173439285892765248219730073273036517725939409971809083058041780264560576764115967213013380206017646943405476909576091357482868476761698005324735669002986992261514164471647621973497050651105016702049275202360233254308086080840811811270395182950866854592166254651076234420662875446908943737749367221987863265181700223872816922018831416550743452655599236620182838774266078307310527709836462300005876748274266928980195184478251265986886488170211071786897470230189921260226806813948222307759474759342997757077589856537503856731410785456607398210129322637205537554981139121822428297380197652187006595903988741596680008103907700803965881407891948670316751093348441767470344198247057046756517630261990510817072323901911147038583224901683429072731095666462995891134319532658460042039832285713339493699639220237857309737109028479215274870712938102667472652582674547746273957596211401299308669708874527010828161490844069542400000 First Harmonic > 10 is: 4534503430707033513455566663502488777398404081163864262146240513258488614453237798607335814524420102703735793862177214220961640516162786810577801568379848400841651563172393145644747756934779147852785761076984123029496399397933274328333851023356832490595994957906245622634743917027121730163673444620574773571394586325343348374089752330084853480910846154523214600594885010321970738793199335121376742737688996290912022884023314845989317485564897100378511371007284095835544913458386488350273752793299623280953681770210860670497293958742056567860493303919432966767309289555005524116155997967505009262492686887145801834344158217659625052992919107429112826740068763017007260951761133678087563009777021924759295735860359747992008280482368678698363760290512075843263479257144567423473653608660780306498167138240689075702661825821881383897412095062894498190524562267366610500376186508574923546863369628573748735962288669973834867370052620567124452691818158059495529870775349719430883965813897867964261906884105709015328654134185094852649077443558343796100212309241953109323566593312098467538550988320147927458982568637261872251614826312806756190545537354754639105220779296735305508074404777698608072480025436626465940783557587776095412527179972733639880928504521162532474790119331444432691186539731972229608857034838322577825073128558410147097241778325338487130870125908822766048925435961031468685135925285730005192072367106087421540485758689853548032678218115860840827961919559051579906147674926980159462587970312884204058339415498440724533667854443260894961415867322707773766644126293587970041291934576744798355640180480297466389631305725091134483147261680364583681456904845585042916227359755995566890651618068412417567353169223028238974716410284035466524218357680139996958854138926338447160349489618026519182480829051291094774428369589524841439952414899018430629415759045664629576805779430276126299459472265505596409675499333035181404107887631586541773720233030773460233097013905563454586768052944023799630803277004868637818739278212959623313318901685226624666106128149541383179968968079822908437213133926503243634564555471480742603760558963949447568409555153275556694530840495402464574679819190097934082680991390359677270683921719809487985411431453913318562222280325934810896976109622317012462673397599719705029993095451945802745111845994127113737531302666646673629309399879563721820841693763990040297218084280469660603933736914103580592334169009905536955683757799785465521029810481052093318974648597912401819690931945953441013940611090922220615447632315575131416623986489801748397222861527442923686453815927754305883939843644528744788311441317667800082400625247002131690240576547715553985566084722165603169180546444489624520529458589659642945674302803518493446186365401036652868957343947436300845516052248544996767002703372227801331704347861429376714087429190841582994100706608870432263478840976847125656598528047665054259600221287735441697634905261794158123419440836603187910006400385658161609560077480916666767855249401496376453813742910897685304674607932781480153350592305086143517293124048321908049033338641165570949920967640973467814840149770609739430967880836391708342980969076413706547475361151566568646558729953788076269958878680638806411347398270002468450490060229923546495153668589442405650268354841362770166414940668952342796856334297184594689014220369402540999903691567293267477507617730573216005494288894386265419609930709688051786459868855999927483488595292377625472492052607166011299662062394773003492445503755319504334407628427786801438298993197490802949903537017176795900523366221528304387658556186131452558642788326427005831945047208416461903276903155157668071084197085232239303753932511966601799642086840010635787283417484115163601638040747720429853554897965062666245152120838888282696144547158569268308151508594359471912253952609211269960896327745083608820919865564411609729050094802982153483731969811821363213666501089803250069914862591069398131811421717727581994465281157595132213790173740210606315458787942312889500024592644472634497210343312196800423898410785211559075127039914648882418769844305126070628158439406823975898202123936987508547712323439814808927188496139029784726424065836399799566957465329325900733347158011735890776039622239371179265322102988095792334295987922829904316643744024475158366697011639022690011158534933281478551427221827338035502405739017675964995339845575688974247145430375550691172792583150675040459847494992816879937873922797432917540308251685230808170901253598248750205022912447198383112847277587880630475986296945942943527602129139602849994318853893879911800330577976183782475767979870512555206262710949997353103772493020471652674012338306053325512634115113811440908388247422072755018950413835394298503429471638926223466724120693416489874608276360557167546047009826979490836032462574788898996808959092152232893733971469326230223411125857734727852907092167761679250398986918637504310219629562470003641972668110649513078347115663607119611611006136091187311740932841583114177470218100285066869010543963406829140648421377028252417899384810247910009920738953581331407151723899810868371969278374342093106625781466735739058011357436506412309273928847791977164882301609474829416331188046487042397727216661613196578592821103409588225020153177686173957843637300970946602788598104463189226384477573248319694609//453448392867348851861091842716460781522665326792904183722427862157021725614855278045402902750948524499248477906574954049723047247594567849381505559142259985012563911748565504645288824396149684911357398473040997444532446072249205183698951114867996915790953480940595875198164176514792266197724950029214098402750523319656510658391308781455532640143957171928728555028441113976098936445243888308518334732068287540969911145046182240997844332542069999808052999118116686423274752490237063716145951662497086525502824771022018772683298755747961799756488875473662436395793972289765688786340400767092625088976215404821615642169932407067470102277881619113300360581665503419953488853618786486589079218434762164433122957020446969147378233696729003877954852548129234246125950262402365717815616819562222237997997268259525610560218332639694073974306248739683910746615242978454786299588282966741711638421737040643849869111250119460946958342235754393754105273921365540335675025520532198380359075366480578543033141107171068864236199764731934475297409089137917767464818172280194587098425992699315337227143311157563983501099319586806728853272646048761957836787873218028733894581386787413379357471544256309173104246757027297103897425448969584443551192032578044112779996916037308469908337849261001566062681902158730668479447685029245562145758050351907230317974951652391896463264624212612669169958584934650809482293303230830931776045695753750127961326467827548499522738908622830870349146611375298940888140985679637170914965525299929769378946655485787070387076631251882242836138520842058399796780766046354724533075054753447434289238707943437708102115231865561988793697866081966627386198255657705081440529935972313855299293571946459814547702790931940409505592380201618395383529120246923953942192402976907753500903555076008341911303039471562108255892056978939810785607998919899547795388848467866707335193584754101418435349055093490132220307797847159336930832196642264566173052472986519467903662478761577267524782906438677344301510628707749510764610883151848961955324250103497542040100381108000294328086486819670637824004877822719162400898805753721906246725669673698667303029853553151086095673161642688637369399134211142673038004309271354569340138672196310045673847468188540492497939082450727559160669323441829424345229568861519806871166312840505922345924286612289038405635960847312841549454914894505350287965178933448588642960151653234597627993111784598027362458363048975481887472153211279255977999546404147533745281771529556720982887239728930885170033344102088264480888466142172463254195760915805324061034675182591049442628706041312626422073008073380371673927489824737483560681019534670531017902066234272224653619745905599447903061483059896873406142453228071022111502256443781491460846164401184358168702828324631516293734123540067969597713270070892954605122527765020904360443840604089525630920883767509762055795186065284871898250633904172393012014014535429691316180843284005611214029789777910907448104608381684001408038080004630598753746420393898907687445970436071633844317405361945225336609003279748456376641015025188398108421593478229298294410838458456380669395413849296975844767158362010790056013282230712593275083853985899993525361915338786353135213679507571177474865535450360805151635896650877459021597451290179824388706852573538022338292357056094650723608655495776391045556275691435553166984429143943751883621223890112143444263384709343311021515999913133625576607936737600163990937589076505709728288249796177860701551483858105729270343952736046045949895941189683687455280876075868089128908950093274631325285867675575977007649875882080181911556426324514888965309812114552424769046963560561519998228670210152775853158532301504121266893422531369102008133615525509443494794446512860320064722889237146201868974019228593633572436903033783142941154726917069945760001298893890438043207045074201509279648515350335803482110210899536912444564551795055524051376159340142763939561343534419114897944064189816231618709358147206686123692571007252707031453516920926510941282670271991207431645277787732194166279571541705091430384118591739755560716583929251984774474735360846706097322805037517607175954157003966139895604444104180849566439510557422016972444504568924435928285183045942516523723723922905128092098303510237702313418511361585451364656754043639465582976252775658243115735311521322732866960027744814046142646066362389168699571924976468952738225372771528877335171820809981966462557399840199438944317868064756592423558340956344597149662019813621964301710551795219655353560019310354145741006960293249850509760942103140771516196121166914225789961295123942408454678165630224229246096815718964524008072338418161910957019466321162530778568751489056122708488740001553449809520216651409154456422832210306621326600466249277216434300763703544991709516688672829002850684750220066717750016340009488155733886564204666531055424099636022638066094074880147267683122477920693324135470681030117673286007072403853987896516881673522241375339519109365317259361633140780497503747668264390394453428165247265288377265912593743778917613302228374835985611202269353962434983431127134732498447341566434664481796653135468887881140453881191678527573715771628361883084372307861539269288978542389654945192750390734178119092263987452761982138487929715357003527512352779194620327380792004034424949320206418355200000
Phix
requires("0.8.4") include mpfr.e integer n = 1, gn = 1 mpq hn = mpq_init_set_si(1) sequence gt = {} puts(1,"First twenty harmonic numbers as rationals:\n") while gn<=10 do if n<=20 then printf(1,"%18s%s",{mpq_get_str(hn),iff(mod(n,5)?" ","\n")}) end if if n=100 then printf(1,"\nOne Hundredth:\n%s\n\n",{mpq_get_str(hn)}) end if if mpq_cmp_si(hn,gn)>0 then gt &= n gn += 1 end if n += 1 mpq_add_si(hn,hn,1,n) end while printf(1,"(one based) Index of first value:\n") for i=1 to length(gt) do printf(1," greater than %2d: %,6d (%s term)\n",{i,gt[i],ordinal(gt[i])}) end for
- Output:
First twenty harmonic numbers as rationals: 1 3/2 11/6 25/12 137/60 49/20 363/140 761/280 7129/2520 7381/2520 83711/27720 86021/27720 1145993/360360 1171733/360360 1195757/360360 2436559/720720 42142223/12252240 14274301/4084080 275295799/77597520 55835135/15519504 One Hundredth: 14466636279520351160221518043104131447711/2788815009188499086581352357412492142272 (one based) Index of first value: greater than 1: 2 (second term) greater than 2: 4 (fourth term) greater than 3: 11 (eleventh term) greater than 4: 31 (thirty-first term) greater than 5: 83 (eighty-third term) greater than 6: 227 (two hundred and twenty-seventh term) greater than 7: 616 (six hundred and sixteenth term) greater than 8: 1,674 (one thousand, six hundred and seventy-fourth term) greater than 9: 4,550 (four thousand, five hundred and fiftieth term) greater than 10: 12,367 (twelve thousand, three hundred and sixty-seventh term)
using standard floats
integer n = 1, gn = 1 atom hn = 1 sequence gt = {} puts(1,"First twenty harmonic numbers as fractions:\n") while gn<=10 do if n<=20 then printf(1,"%18.15f%s",{hn,iff(mod(n,5)?" ","\n")}) end if if n=100 then printf(1,"\nOne Hundredth: %18.15f\n\n",{hn}) end if if hn>gn then gt &= n gn += 1 end if n += 1 hn += 1/n end while printf(1,"(one based) Index of first value:\n") for i=1 to length(gt) do printf(1," greater than %2d: %,6d (%s term)\n",{i,gt[i],ordinal(gt[i])}) end for {} = wait_key()
- Output:
First twenty harmonic numbers as fractions: 1.000000000000000 1.500000000000000 1.833333333333333 2.083333333333333 2.283333333333333 2.450000000000000 2.592857142857143 2.717857142857143 2.828968253968254 2.928968253968254 3.019877344877345 3.103210678210678 3.180133755133755 3.251562326562327 3.318228993228994 3.380728993228994 3.439552522640758 3.495108078196314 3.547739657143682 3.597739657143682 One Hundredth: 5.187377517639621 (one based) Index of first value: greater than 1: 2 (second term) greater than 2: 4 (fourth term) greater than 3: 11 (eleventh term) greater than 4: 31 (thirty-first term) greater than 5: 83 (eighty-third term) greater than 6: 227 (two hundred and twenty-seventh term) greater than 7: 616 (six hundred and sixteenth term) greater than 8: 1,674 (one thousand, six hundred and seventy-fourth term) greater than 9: 4,550 (four thousand, five hundred and fiftieth term) greater than 10: 12,367 (twelve thousand, three hundred and sixty-seventh term)
Raku
Using Lingua::EN::Numbers from the Raku ecosystem. <lang perl6>use Lingua::EN::Numbers;
my @H = [\+] (1..*).map: { FatRat.new( 1, $_ ) };
say "First twenty harmonic numbers as rationals:\n",
@H[^20].&ratty.batch(5)».fmt("%18s").join: "\n";
put "\nOne Hundredth:\n", @H[99].&ratty;
say "\n(zero based) Index of first value:"; printf " greater than %2d: %6s (%s term)\n",
$_, comma( my $i = @H.first(* > $_, :k) ), ordinal 1 + $i for 1..10;
sub ratty (*@a) { @a.map: { .narrow.^name eq 'Int' ?? .narrow !! .nude.join('/') } }</lang>
- Output:
First twenty harmonic numbers as rationals: 1 3/2 11/6 25/12 137/60 49/20 363/140 761/280 7129/2520 7381/2520 83711/27720 86021/27720 1145993/360360 1171733/360360 1195757/360360 2436559/720720 42142223/12252240 14274301/4084080 275295799/77597520 55835135/15519504 One Hundredth: 14466636279520351160221518043104131447711/2788815009188499086581352357412492142272 (zero based) Index of first value: greater than 1: 1 (second term) greater than 2: 3 (fourth term) greater than 3: 10 (eleventh term) greater than 4: 30 (thirty-first term) greater than 5: 82 (eighty-third term) greater than 6: 226 (two hundred twenty-seventh term) greater than 7: 615 (six hundred sixteenth term) greater than 8: 1,673 (one thousand, six hundred seventy-fourth term) greater than 9: 4,549 (four thousand, five hundred fiftieth term) greater than 10: 12,366 (twelve thousand, three hundred sixty-seventh term)
Wren
<lang ecmascript>import "/big" for BigRat import "/fmt" for Fmt
var harmonic = Fn.new { |n| (1..n).reduce(BigRat.zero) { |sum, i| sum + BigRat.one/i } }
BigRat.showAsInt = true System.print("The first 20 harmonic numbers and the 100th, expressed in rational form, are:") var numbers = (1..20).toList numbers.add(100) for (i in numbers) Fmt.print("$3d : $s", i, harmonic.call(i))
System.print("\nThe first harmonic number to exceed the following integers is:") var i = 1 var limit = 10 var n = 1 var h = 0 while (true) {
h = h + 1/n if (h > i) { Fmt.print("integer = $2d -> n = $,6d -> harmonic number = $9.6f (to 6dp)", i, n, h) i = i + 1 if (i > limit) return } n = n + 1
}</lang>
- Output:
The first 20 harmonic numbers and the 100th, expressed in rational form, are: 1 : 1 2 : 3/2 3 : 11/6 4 : 25/12 5 : 137/60 6 : 49/20 7 : 363/140 8 : 761/280 9 : 7129/2520 10 : 7381/2520 11 : 83711/27720 12 : 86021/27720 13 : 1145993/360360 14 : 1171733/360360 15 : 1195757/360360 16 : 2436559/720720 17 : 42142223/12252240 18 : 14274301/4084080 19 : 275295799/77597520 20 : 55835135/15519504 100 : 14466636279520351160221518043104131447711/2788815009188499086581352357412492142272 The first harmonic number to exceed the following integers is: integer = 1 -> n = 2 -> harmonic number = 1.500000 (to 6dp) integer = 2 -> n = 4 -> harmonic number = 2.083333 (to 6dp) integer = 3 -> n = 11 -> harmonic number = 3.019877 (to 6dp) integer = 4 -> n = 31 -> harmonic number = 4.027245 (to 6dp) integer = 5 -> n = 83 -> harmonic number = 5.002068 (to 6dp) integer = 6 -> n = 227 -> harmonic number = 6.004367 (to 6dp) integer = 7 -> n = 616 -> harmonic number = 7.001274 (to 6dp) integer = 8 -> n = 1,674 -> harmonic number = 8.000486 (to 6dp) integer = 9 -> n = 4,550 -> harmonic number = 9.000208 (to 6dp) integer = 10 -> n = 12,367 -> harmonic number = 10.000043 (to 6dp)