| Bytes | Lang | Time | Link |
|---|---|---|---|
| 015 | cQuents | 250103T195902Z | Stephen |
| 009 | 05AB1E | 241119T114140Z | Kevin Cr |
| 010 | Japt | 180127T144324Z | Shaggy |
| 022 | J | 180129T032144Z | Jonah |
| 027 | TIBASIC | 180127T222102Z | kamoroso |
| 098 | tinylisp | 180130T035347Z | DLosc |
| 034 | Javascript ES6 | 180127T130644Z | Endenite |
| 049 | Java 8 | 180129T083438Z | Kevin Cr |
| 033 | Octave | 180127T132650Z | Luis Men |
| 040 | Wolfram Language Mathematica | 180128T085403Z | alephalp |
| 010 | Pyth | 180127T221848Z | Mr. Xcod |
| 035 | PHP | 180127T194347Z | axiac |
| 008 | Husk | 180127T125501Z | H.PWiz |
| 027 | Julia 0.6 | 180127T162316Z | LukeS |
| 011 | 05AB1E | 180127T153355Z | Emigna |
| 030 | JavaScript | 180127T144841Z | Shaggy |
| 048 | Haskell | 180127T134214Z | BlackCap |
| 038 | Haskell | 180127T141350Z | BlackCap |
| 039 | R | 180127T134825Z | Giuseppe |
| 035 | Python 3 | 180127T134322Z | Dennis |
| 065 | Swift | 180127T132415Z | Endenite |
| 008 | Jelly | 180127T130348Z | Dennis |
cQuents, 15 bytes
#|1:#A<bN
;/(2$
Times out in TIO for n >= 5. Can save 4 bytes by removing #|1: if appending 1 to the input is allowed.
Explanation
first line
---
#|1:#A<bN
#|1 append 1 to the input
: given a final input n, output the nth term in the sequence
each term is
# the next value of N where
A the first input
< <
b second line ( )
N N
second line
---
;/(2$
; given input n, output the sum of the first n terms of the sequence
each term is
/ 1 /
( ( )
2$ 2 * 1-indexed index of current term
05AB1E, 9 bytes
∞<·zηOÅΔ‹
Explanation:
∞ # Push the infinite positive list: [1,2,3,...]
< # Decrease each by 1 to make it non-negative: [0,1,2,...]
· # Double each: [0,2,4,...]
z # Take 1 divided by each: [0,0.5,0.25,...]
η # Take the prefixes: [[0],[0,0.5],[0,0.5,0.25],...]
O # Sum each inner prefix: [0,0.5,0.75,...]
ÅΔ # Find the first 0-based index that's truthy for:
‹ # Check that the (implicit) input is smaller than this value
# (after which the found index is output implicitly as result)
Japt, 12 10 bytes
@µ½/X c}f1
@µ½/X c}f1 :Implicit input of integer U
@ :Function taking an integer X as argument
µ : Decrement U by
½/X : 0.5 divided by X
c : Round up
} :End function
f1 :Get the first integer >=1 that returns truthy (not zero)
J, 22 bytes
-6 bytes thanks to frownyfrog
I.~0+/\@,1%2*1+[:i.9&^
original answer
Luis's answer in J:
1+]i.~[:<.[:+/\1%2*1+[:i.9&^
Ungolfed
1 + ] i.~ [: <. [: +/\ 1 % 2 * 1 + [: i. 9&^
Mostly curious to see if it can be drastically improved (cough paging miles)
Explanation
1 + NB. 1 plus...
] i.~ NB. find the index of the arg in...
[: <. NB. the floor of...
[: +/\ NB. the sumscan of...
1 % NB. the reciprical of...
2 * NB. two times...
1 + NB. 1 plus...
[: i. NB. the integers up to
9&^ NB. 9 raised to the power of the arg
TI-BASIC, 27 bytes
Prompts user for input and displays output on termination. Note: ⁻¹ is the -1 (inverse) token.
Input N
1
Repeat 2N≤Σ(I⁻¹,I,1,Ans
Ans+1
End
Ans
tinylisp, 98 bytes
(load library
(d _(q((k # N D)(i(l N(* D # 2))(_(inc k)#(+(* N k)D)(* D k))(dec k
(q((#)(_ 1 # 0 1
The last line is an unnamed lambda function that takes the number of book-lengths and returns the number of books needed. Try it online!
Explanation
The only numeric data type tinylisp has is integers, so we calculate the harmonic series as a fraction by keeping track of the numerator and denominator. At each step, N is the numerator, D is the denominator, and k is the sum index. We want the new partial sum to be N/D + 1/k, or (N*k + D)/(D*k). Thus, we recurse with a new numerator of N*K + D, a new denominator of D*k, and a new index of k+1.
The recursion should halt once the partial sum is greater than or equal to #, the desired number of book-lengths. At this point, we've gone one book too far, so we return k-1. The condition is 1/2 * N/D < #; multiplying out the denominator, we get N < D*#*2, which is the golfiest way to write it.
The recursive helper function _ does all these calculations; the main function is merely a one-argument wrapper that calls _ with the correct starting values for k, N, and D.
Javascript (ES6), 34 bytes
n=>eval("for(i=0;n>0;n-=.5/i)++i")
Ungolfed
n => {
for(i = 0; n > 0; ++i)
n -= .5 / i
return i;
}
Test Cases
f=n=>eval("for(i=0;n>0;n-=.5/i)++i")
<button onclick="console.log(f(1))">Run for n = 1</button>
<button onclick="console.log(f(2))">Run for n = 2</button>
<button onclick="console.log(f(3))">Run for n = 3</button>
<button onclick="console.log(f(5))">Run for n = 5</button>
<button onclick="console.log(f(10))">Run for n = 10</button>
Java 8, 49 bytes
n->{float r=0,s=0;for(;s<n;)s+=.5f/++r;return r;}
Explanation:
Try it online. (Times out for test cases above n=7.)
n->{ // Method with integer parameter and float return-type
float r=0, // Result-float, starting at 0
s=0; // Sum-float, starting at 0
for(;s<n;) // Loop as long as the sum is smaller than the input
s+=.5f/++r; // Increase the sum by `0.5/(r+1)`,
// by first increasing `r` by 1 with `r++`
return r;} // Return the result-float
Octave, 41 40 33 bytes
1 byte saved thanks to @Dennis
@(n)find(cumsum(.5./(1:9^n))>n,1)
Explanation
This uses the fact that harmonic numbers can be lower-bounded by a logarithmic function.
Also, the >= comparison can be replaced by > because harmonic numbers cannot be even integers (thanks, @Dennis!).
@(n) % Anonymous function of n
1:9^n % Range [1 2 ... 9^n]
.5./( ) % Divide .5 by each entry
cumsum( ) % Cumulative sum
>n % Is each entry greater than n?
find( ,1) % Index of first true entry
PHP, 35 bytes
while($argv[1]>$s+=.5/++$i);echo$i;
Run it using the CLI:
$ php -d error_reporting=0 -r 'while($argv[1]>$s+=.5/++$i);echo$i;' 5
Husk, 8 bytes
V≥⁰∫m\İ0
Since Husk uses rational numbers when it can, this has no floating point issues
Explanation
İ0 The infinite list of positive even numbers
m\ Reciprocate each
∫ Get the cumulative sum
V Find the index of the first element
≥⁰ that is greater than or equal to the input
Julia 0.6, 30 27 bytes
<(n,i=1)=n>0?n-.5/i<i+1:i-1
Only works up to n = 6, because Julia has no tail call optimization.
-3 bytes thanks to Dennis.
05AB1E, 11 bytes
XµN·zODI›}N
Explanation
Xµ } # loop until counter is 1
N·z # push 1/(2*N)
O # sum the stack
DI› # break if the sum is greater than the input
N # push N
JavaScript, 30 bytes
A recursive function so it'll crap out pretty early.
f=(n,x=0)=>n>0?f(n-.5/++x,x):x
Haskell, 71 49 48 bytes
f x=length.fst.span(<x).scanl(+)0$(0.5/)<$>[1..]
@BMO saved me a whopping 22 bytes!
Haskell, 38 bytes
k!n|n<=0=0|x<-n-1/(2*k)=1+(k+1)!x
(1!)
Swift, 65 bytes
func f(n:Double){var i=0.0,s=i;while s<n{i+=1;s+=0.5/i};print(i)}
Ungolfed
func f(n:Double) {
var i = 0.0, s = 0.0
while s < n {
i += 1;
s += 0.5 / i
}
print(i)
}