Jump to content

Harmonic series: Difference between revisions

m
→‎{{header|R}}: Syntax highlighting.
(added AWK)
m (→‎{{header|R}}: Syntax highlighting.)
Line 1,373:
===Direct Summation===
The talk page helpfully points out that we can be remarkably lazy here.
<lang Rrsplus>HofN <- function(n) sum(1/seq_len(n)) #Task 1
H <- sapply(1:100000, HofN)
print(H[1:20]) #Task 2
Line 1,387:
===Cumulative Sums===
As for doing this properly, R provides a handy cumsum function.
<lang Rrsplus>firstNHarmonicNumbers <- function(n) cumsum(1/seq_len(n)) #Task 1
H <- firstNHarmonicNumbers(100000) #Runs stunningly quick
print(H[1:20]) #Task 2
331

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.