Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
44
Task/Harmonic-series/Haskell/harmonic-series-1.hs
Normal file
44
Task/Harmonic-series/Haskell/harmonic-series-1.hs
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
import Data.List (find)
|
||||
import Data.Ratio
|
||||
|
||||
--------------------- HARMONIC SERIES --------------------
|
||||
|
||||
harmonic :: [Rational]
|
||||
harmonic =
|
||||
scanl1
|
||||
(\a x -> a + 1 / x)
|
||||
[1 ..]
|
||||
|
||||
-------------------------- TESTS -------------------------
|
||||
main :: IO ()
|
||||
main = do
|
||||
putStrLn "First 20 terms:"
|
||||
mapM_ putStrLn $
|
||||
showRatio <$> take 20 harmonic
|
||||
|
||||
putStrLn "\n100th term:"
|
||||
putStrLn $ showRatio (harmonic !! 99)
|
||||
putStrLn ""
|
||||
|
||||
putStrLn "One-based indices of first terms above threshold values:"
|
||||
let indexedHarmonic = zip [0 ..] harmonic
|
||||
mapM_
|
||||
putStrLn
|
||||
$ fmap
|
||||
( showFirstLimit
|
||||
<*> \n -> find ((> n) . snd) indexedHarmonic
|
||||
)
|
||||
[1 .. 10]
|
||||
|
||||
-------------------- DISPLAY FORMATTING ------------------
|
||||
|
||||
showFirstLimit n (Just (i, r)) =
|
||||
"Term "
|
||||
<> show (succ i)
|
||||
<> " is the first above "
|
||||
<> show (numerator n)
|
||||
|
||||
showRatio :: Ratio Integer -> String
|
||||
showRatio =
|
||||
((<>) . show . numerator)
|
||||
<*> (('/' :) . show . denominator)
|
||||
1
Task/Harmonic-series/Haskell/harmonic-series-2.hs
Normal file
1
Task/Harmonic-series/Haskell/harmonic-series-2.hs
Normal file
|
|
@ -0,0 +1 @@
|
|||
Hn=: {{ +/ %1+i.y }}"0
|
||||
5
Task/Harmonic-series/Haskell/harmonic-series-3.hs
Normal file
5
Task/Harmonic-series/Haskell/harmonic-series-3.hs
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
Hn i.4 5
|
||||
0 1 1.5 1.83333 2.08333
|
||||
2.28333 2.45 2.59286 2.71786 2.82897
|
||||
2.92897 3.01988 3.10321 3.18013 3.25156
|
||||
3.31823 3.38073 3.43955 3.49511 3.54774
|
||||
1
Task/Harmonic-series/Haskell/harmonic-series-4.hs
Normal file
1
Task/Harmonic-series/Haskell/harmonic-series-4.hs
Normal file
|
|
@ -0,0 +1 @@
|
|||
Hni=: {{ 0,+/\ %1+i.y}}
|
||||
5
Task/Harmonic-series/Haskell/harmonic-series-5.hs
Normal file
5
Task/Harmonic-series/Haskell/harmonic-series-5.hs
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
4 5$Hni 20
|
||||
0 1 1.5 1.83333 2.08333
|
||||
2.28333 2.45 2.59286 2.71786 2.82897
|
||||
2.92897 3.01988 3.10321 3.18013 3.25156
|
||||
3.31823 3.38073 3.43955 3.49511 3.54774
|
||||
2
Task/Harmonic-series/Haskell/harmonic-series-6.hs
Normal file
2
Task/Harmonic-series/Haskell/harmonic-series-6.hs
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
Hn 1e5
|
||||
12.0901
|
||||
14
Task/Harmonic-series/Haskell/harmonic-series-7.hs
Normal file
14
Task/Harmonic-series/Haskell/harmonic-series-7.hs
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
(Hni 1e5) (] ,. I. ,. I. { [) i.13
|
||||
0 0 0
|
||||
1 1 1
|
||||
2 4 2.08333
|
||||
3 11 3.01988
|
||||
4 31 4.02725
|
||||
5 83 5.00207
|
||||
6 227 6.00437
|
||||
7 616 7.00127
|
||||
8 1674 8.00049
|
||||
9 4550 9.00021
|
||||
10 12367 10
|
||||
11 33617 11
|
||||
12 91380 12
|
||||
2
Task/Harmonic-series/Haskell/harmonic-series-8.hs
Normal file
2
Task/Harmonic-series/Haskell/harmonic-series-8.hs
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
(Hn 91380)-12
|
||||
3.05167e_6
|
||||
73
Task/Harmonic-series/Haskell/harmonic-series-9.hs
Normal file
73
Task/Harmonic-series/Haskell/harmonic-series-9.hs
Normal file
|
|
@ -0,0 +1,73 @@
|
|||
import java.math.BigInteger;
|
||||
|
||||
public class HarmonicSeries {
|
||||
|
||||
public static void main(String[] aArgs) {
|
||||
|
||||
System.out.println("The first twenty Harmonic numbers:");
|
||||
for ( int i = 1; i <= 20; i++ ) {
|
||||
System.out.println(String.format("%2s", i) + ": " + harmonicNumber(i));
|
||||
}
|
||||
|
||||
System.out.println();
|
||||
for ( int i = 1; i <= 10; i++ ) {
|
||||
System.out.print("The first term greater than ");
|
||||
System.out.println(String.format("%2s%s%5s", i, " is Term ", indexedHarmonic(i)));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private static Rational harmonicNumber(int aNumber) {
|
||||
Rational result = Rational.ZERO;
|
||||
for ( int i = 1; i <= aNumber; i++ ) {
|
||||
result = result.add( new Rational(BigInteger.ONE, BigInteger.valueOf(i)) );
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private static int indexedHarmonic(int aTarget) {
|
||||
BigInteger target = BigInteger.valueOf(aTarget);
|
||||
Rational harmonic = Rational.ZERO;
|
||||
BigInteger next = BigInteger.ZERO;
|
||||
|
||||
while ( harmonic.numerator.compareTo(target.multiply(harmonic.denominator)) <= 0 ) {
|
||||
next = next.add(BigInteger.ONE);
|
||||
harmonic = harmonic.add( new Rational(BigInteger.ONE, next) );
|
||||
}
|
||||
|
||||
return next.intValueExact();
|
||||
}
|
||||
|
||||
private static class Rational {
|
||||
|
||||
private Rational(BigInteger aNumerator, BigInteger aDenominator) {
|
||||
numerator = aNumerator;
|
||||
denominator = aDenominator;
|
||||
|
||||
BigInteger gcd = numerator.gcd(denominator);
|
||||
numerator = numerator.divide(gcd);
|
||||
denominator = denominator.divide(gcd);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return numerator + " / " + denominator;
|
||||
}
|
||||
|
||||
private Rational add(Rational aRational) {
|
||||
BigInteger numer = numerator.multiply(aRational.denominator)
|
||||
.add(aRational.numerator.multiply(denominator));
|
||||
BigInteger denom = aRational.denominator.multiply(denominator);
|
||||
|
||||
return new Rational(numer, denom);
|
||||
}
|
||||
|
||||
private BigInteger numerator;
|
||||
private BigInteger denominator;
|
||||
|
||||
private static final Rational ZERO = new Rational(BigInteger.ZERO, BigInteger.ONE);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue