Harmonic series: Difference between revisions

Add bruijn
No edit summary
(Add bruijn)
 
(7 intermediate revisions by 7 users not shown)
Line 233:
250 next i
260 end</syntaxhighlight>
 
==={{header|Craft Basic}}===
<syntaxhighlight lang="basic">precision 5
 
print "the first twenty harmonic numbers are:"
 
for n = 1 to 20
 
let h = h + 1 / n
print n, tab, h
 
next n
 
print newline, "the nth index of the first harmonic number that exceeds the nth integer:"
 
let h = 1
let n = 2
 
for i = 2 to 10
 
do
 
if h < i then
 
let h = h + 1 / n
let n = n + 1
 
endif
 
wait
 
loop h < i
 
print tab, n - 1,
 
next i</syntaxhighlight>
{{out| Output}}<pre>
the first twenty harmonic numbers are:
1 1
2 1.50000
3 1.83333
4 2.08333
5 2.28333
6 2.45000
7 2.59286
8 2.71786
9 2.82897
10 2.92897
11 3.01988
12 3.10321
13 3.18013
14 3.25156
15 3.31823
16 3.38073
17 3.43955
18 3.49511
19 3.54774
20 3.59774
 
The nth index of the first harmonic number that exceeds the nth integer:
4 11 31 83 227 616 1674 4550 12375
</pre>
 
==={{header|Gambas}}===
Line 371 ⟶ 433:
end</syntaxhighlight>
 
=={{header|Bruijn}}==
<syntaxhighlight lang="bruijn">
:import std/List .
:import std/Combinator .
:import std/Math/Rational Q
:import std/Number N
 
# fun Church iteration hack
harmonic [0 &[[(Q.add 1 op) : N.++0]] start [[1]]] ⧗ Unary → Rational
op (+1) : N.--0
start (+0.0f) : (+1)
 
custom-gt? &[[[N.gt? 2 (N.mul 0 N.++1)]]] ⧗ Rational → Νumber → Boolean
 
main [φ cons first-20 first-10-above (harmonic <$> (iterate [[[1 (2 1 0)]]] (+0u)))]
first-20 take (+20)
first-10-above [take (+10) first-above]
first-above [find-index [custom-gt? 0 1] 1] <$> (iterate N.inc (+0))
</syntaxhighlight>
 
Takes a *long* time, but will return the correct result.
 
=={{header|C#}}==
{{trans|Go}}
<syntaxhighlight lang="C#">
using System;
using System.Numerics;
 
public class BigRational
{
public BigInteger Numerator { get; private set; }
public BigInteger Denominator { get; private set; }
 
public BigRational(BigInteger numerator, BigInteger denominator)
{
if (denominator == 0)
throw new ArgumentException("Denominator cannot be zero.", nameof(denominator));
 
BigInteger gcd = BigInteger.GreatestCommonDivisor(numerator, denominator);
Numerator = numerator / gcd;
Denominator = denominator / gcd;
 
if (Denominator < 0)
{
Numerator = -Numerator;
Denominator = -Denominator;
}
}
 
public static BigRational operator +(BigRational a, BigRational b)
{
return new BigRational(a.Numerator * b.Denominator + b.Numerator * a.Denominator, a.Denominator * b.Denominator);
}
 
public override string ToString()
{
return $"{Numerator}/{Denominator}";
}
}
 
class Program
{
static BigRational Harmonic(int n)
{
BigRational sum = new BigRational(0, 1);
for (int i = 1; i <= n; i++)
{
BigRational r = new BigRational(1, i);
sum += r;
}
return sum;
}
 
static void Main(string[] args)
{
Console.WriteLine("The first 20 harmonic numbers and the 100th, expressed in rational form, are:");
int[] numbers = new int[21];
for (int i = 1; i <= 20; i++)
{
numbers[i - 1] = i;
}
numbers[20] = 100;
foreach (int i in numbers)
{
Console.WriteLine($"{i,3} : {Harmonic(i)}");
}
 
Console.WriteLine("\nThe first harmonic number to exceed the following integers is:");
const int limit = 10;
for (int i = 1, n = 1; i <= limit; n++)
{
double h = 0;
for (int j = 1; j <= n; j++)
{
h += 1.0 / j;
}
if (h > i)
{
Console.WriteLine($"integer = {i,2} -> n = {n,6} -> harmonic number = {h,9:F6} (to 6dp)");
i++;
}
}
}
}
</syntaxhighlight>
{{out}}
<pre>
The first 20 harmonic numbers and the 100th, expressed in rational form, are:
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
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 = 1674 -> harmonic number = 8.000486 (to 6dp)
integer = 9 -> n = 4550 -> harmonic number = 9.000208 (to 6dp)
integer = 10 -> n = 12367 -> harmonic number = 10.000043 (to 6dp)
 
</pre>
 
=={{header|C++}}==
Line 612 ⟶ 817:
</pre>
 
 
=={{header|EasyLang}}==
{{trans|BASIC256}}
<syntaxhighlight>
numfmt 5 2
print "The first twenty harmonic numbers are:"
for n = 1 to 20
h += 1 / n
print n & " " & h
.
print ""
print "The first harmonic number greater than: "
h = 1
n = 2
for i = 2 to 10
while h < i
h += 1 / n
n += 1
.
print i & " is " & h & ", at position " & n - 1
.
</syntaxhighlight>
{{out}}
<pre>
The first twenty harmonic numbers are:
1 1
2 1.50000
3 1.83333
4 2.08333
5 2.28333
6 2.45000
7 2.59286
8 2.71786
9 2.82897
10 2.92897
11 3.01988
12 3.10321
13 3.18013
14 3.25156
15 3.31823
16 3.38073
17 3.43955
18 3.49511
19 3.54774
20 3.59774
 
The first harmonic number greater than:
2 is 2.08333, at position 4
3 is 3.01988, at position 11
4 is 4.02725, at position 31
5 is 5.00207, at position 83
6 is 6.00437, at position 227
7 is 7.00127, at position 616
8 is 8.00049, at position 1674
9 is 9.00021, at position 4550
10 is 10.00004, at position 12367
</pre>
 
=={{header|Factor}}==
Line 1,063 ⟶ 1,325:
 
=={{Header|Java}}==
Java does not have a built-in fraction type, so we create our own class Rational.
 
<syntaxhighlight lang="java">
import java.math.BigInteger;
 
/**
* Java does not have a built-in fraction type, so we create our own class Rational.
*/
public class HarmonicSeries {
Line 1,087 ⟶ 1,346:
}
 
private static Rational harmonicNumber(int aNumber) {
// PRIVATE //
private static Rational harmonicNumber(int aNumber) {
Rational result = Rational.ZERO;
for ( int i = 1; i <= aNumber; i++ ) {
Line 1,471 ⟶ 1,728:
<pre>{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}
{2, 4, 11, 31, 83, 227, 616, 1674, 4550, 12367}</pre>
 
=={{header|Maxima}}==
<syntaxhighlight lang="maxima">
harmonic(n):=apply("+",1/makelist(i,i,n))$
 
first_greater_than_n(len):=block(i:1,result:[],while harmonic(i)<=len do (result:endcons(i,result),i:i+1),last(result)+1)$
 
/* Test cases */
/* First 20 harmonic numbers */
makelist(harmonic(j),j,20);
 
/* First harmonic number that exceeds a positive integer from 1 to 5 */
makelist(first_greater_than_n(k),k,5);
</syntaxhighlight>
{{out}}
<pre>
[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]
 
[2,4,11,31,83]
</pre>
 
=={{header|Nim}}==
Line 2,391 ⟶ 2,668:
Position of first term > 10 is 12367
</pre>
 
=={{header|Tcl}}==
<syntaxhighlight lang="tcl># Task 1
proc harmonic {n} {
if {$n < 1 || $n != [expr {floor($n)}]} {
error "Argument to harmonic function is not a natural number"
}
set Hn 1
for {set i 2} {$i <= $n} {incr i} {
set Hn [expr {$Hn + (1.0/$i)}]
}
return $Hn
}
 
# Task 2
for {set x 1} {$x <= 20} {incr x} {
set Hx [harmonic $x]
puts "$x: $Hx"
}
 
# Task 3 /stretch
set x 0
set lastInt 1
while {$lastInt <= 10} {
incr x
set Hx [harmonic $x]
if {$Hx > $lastInt} {
puts -nonewline "The first harmonic number above $lastInt"
puts " is $Hx at position $x"
incr lastInt
}
}
</syntaxhighlight>
{{out}}
<pre>1: 1
2: 1.5
3: 1.8333333333333333
4: 2.083333333333333
5: 2.283333333333333
6: 2.4499999999999997
7: 2.5928571428571425
8: 2.7178571428571425
9: 2.8289682539682537
10: 2.9289682539682538
11: 3.0198773448773446
12: 3.103210678210678
13: 3.180133755133755
14: 3.251562326562327
15: 3.3182289932289937
16: 3.3807289932289937
17: 3.439552522640758
18: 3.4951080781963135
19: 3.547739657143682
20: 3.597739657143682
The first harmonic number above 1 is 1.5 at position 2
The first harmonic number above 2 is 2.083333333333333 at position 4
The first harmonic number above 3 is 3.0198773448773446 at position 11
The first harmonic number above 4 is 4.02724519543652 at position 31
The first harmonic number above 5 is 5.002068272680166 at position 83
The first harmonic number above 6 is 6.004366708345567 at position 227
The first harmonic number above 7 is 7.001274097134162 at position 616
The first harmonic number above 8 is 8.000485571995782 at position 1674
The first harmonic number above 9 is 9.000208062931115 at position 4550
The first harmonic number above 10 is 10.000043008275778 at position 12367</pre>
 
=={{header|Verilog}}==
Line 2,424 ⟶ 2,765:
{{libheader|Wren-big}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "./big" for BigRat
import "./fmt" for Fmt
 
var harmonic = Fn.new { |n| (1..n).reduce(BigRat.zero) { |sum, i| sum + BigRat.one/i } }
55

edits