Harmonic series: Difference between revisions

Add bruijn
imported>Maxima enthusiast
No edit summary
(Add bruijn)
 
(3 intermediate revisions by 3 users not shown)
Line 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 674 ⟶ 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 2,565 ⟶ 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