When does the harmonic sum pass a target

The harmonic series diverges, so it passes every target eventually, but it needs a huge number of terms to do so.

What to build

Find the smallest n for which 1 + 1/2 + … + 1/n is at least a given target.

Requirements

  • Read the target as a whole number.
  • Add 1/k for k = 1, 2, … and stop as soon as the total reaches the target.
  • Print the number of terms and the total with 4 decimal places.

Input

One line: a whole number target from 1 to 10.

Output

Two lines: n N and sum S with S to 4 decimal places.

Constraints

The answer is at most 12367 terms.

Example

Input:

2

Output:

n 4
sum 2.0833

Why it works

The harmonic series diverges, so some n always exists. Its partial sum grows like ln(n) + 0.577, so each extra unit of target needs about e times as many terms as the one before.

Examples

Example 1 · Input
2
Expected output
n 4
sum 2.0833
Example 2 · Input
3
Expected output
n 11
sum 3.0199

Prompts inside input() are ignored when checking, so input("n: ") is fine.

Hints

Hint 1

Keep a running total of 1/k for k = 1, 2, 3, … and stop when it is at least the target.

Hint 2

A while loop is natural. The answer can be about 12000 terms for a target of 10.

Hint 3

Print n, then the total with `:.4f`.

Solution

Show one correct solution (try it yourself first)
target = int(input())
total = 0.0
n = 0
while total < target:
    n += 1
    total += 1 / n
print("n", n)
print(f"sum {total:.4f}")

▶ Open this solution in the compiler, then press Visualize to watch it run step by step

Lesson: Sequences and series →