RosettaCodeData/Task/Harmonic-series/Factor/harmonic-series.factor
2023-07-01 13:44:08 -04:00

24 lines
763 B
Factor
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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