# How many decimals are there before I'd need to add 1 when rounding?

When you round a number, if the next digit is >= 5 you add 1. For example:

3.1415926535 rounded to 1dp is 3.1
3.1415926535 rounded to 4dp is 3.1416    <-- Note the 5 changed to 6
3.1415926535 rounded to 5dp is 3.14159

3.1415926535 rounded to 9dp is 3.141592654  <-- Note the 3 changed to 4


You challenge is to receive an integer as input and output the number of decimal places before which you would have to round the square root of the number - i.e. the number of decimal places before anumber digit which is >= 5 occurs.

The integer will be between 0 and 100,000 inclusive so for the edge case of 59752 you need to support 17 decimal points (to check the 17th).

If you programming language can't change the number of decimal points, you can display a "?" message to the user.

Example:

Input    Root                     Output

5 -> 2.23 606797749979     -> 2
41 -> 6.40312423 743284     -> 8      (Largest gap under 100)
596 -> 24.4131112314 674     -> 10     (Largest gap under 1000)
59752 -> 244.44222221212112029 -> 16     (Largest gap under 100000)


Do what you want on perfect squares.

This is so shortest code wins.

For anyone interested, the number 310,617 is the biggest under 1,000,000 and has 18 before you reach a digit >= 5.

• How many decimal places do we need to support at maximum? - No languages store to infinite precision. – Blue Nov 6 '15 at 20:22
• Uncertain why, but I get 17 digits for SQRT(59752) (in two different languages). The other results come out properly. – Jonathan Leech-Pepin Nov 6 '15 at 20:47
• @JonathanLeech-Pepin are you including the last digit for some reason? or does your program not support enough decimal places. – Tim Nov 6 '15 at 20:49
• It never has a digit bigger than 5. My program for example will exit with -1 – Blue Nov 6 '15 at 21:08
• @muddyfish that's fine. – Tim Nov 6 '15 at 21:09

# CJam, 17 bytes

rie40mQsJ~>'4f>1#


Try it online.

# Pyth, 13 bytes

f<5e@=*QC\d2Z


Test suite

Start with Q equal to the input. At each time step, multiply Q by 100, calculated as chr('d'). Take its square root. Take this mod 10. If the result is greater than 5, terminate. Print the number of iterations it took to terminate, 0-indexed.

In detail:

f<5e@=*QC\d2Z
Q = eval(input())
f           Z      Filter for the first truthy result over the infinite sequence
starting at Z (= 0)
=*Q           Q *=
C\d             chr('d') (= 100)
---------------------
@  Q   2          Q ^ (1/2)
e                            % 10
<5               5 <


# CJam, 2926 28 bytes

rimqs_'.+'.#)>{'5<}%0#]W'?er


Try it Online.

Puts a "?" if the number does not appear that can be rounded up (perfect square or too long).

## Pyth, 22 bytes

J%@Q2 1x.e<\4@Jbr2lJ1


Explanation

                       - Autoassign Q to evaluated input
@Q2                 - Get the square root of Q
J%    1               - Get the stuff after the decimal point and put it in a string. Store in J
.e      r2lJ  - Create a range between 2 and the length of the string (forget about the 0. bit) and enumerate over it
@Jb      - Get the current decimal place
<\4         - Is it bigger than 4
x            1 - Find the position of the first True value


I'm absolutely certain this can be golfed. If the input doesn't have a digit higher than 4, it will print -1. Supports 17dp.

## Javascript, 59 bytes

f=a=>(a=/\.(.*?)[5-9]/.exec(Math.sqrt(a)),a?a[1].length:'?')


Returns ? for 59752 because JavaScript only uses double precision.

# Linux shell, 52 bytes

dc -e'34k?vp'|cut -d. -f2|sed 's/.[5-9\s].*//'|wc -m


I tried for a pure dc solution, but failed. Precision is adjustable (first number).

As the OP kindly specifies that "you can do what you want on perfect squares", in this case this solution outputs the precision + 1, in this case 35.

# Mathematica 60 bytes

(Position[Drop@@RealDigits[N[Sqrt@#,99]],x_/;x>4][[1,1]]-1)&


Example

(Position[Drop@@RealDigits[N[Sqrt@#, 99]], x_ /; x > 4][[1, 1]] - 1) &[59752]


16

• You can remove the whitespace around the Apply. – LegionMammal978 Nov 7 '15 at 12:24
• Thanks. The byte count stays the same because I had not counted those spaces. – DavidC Nov 7 '15 at 13:04

# Ruby, 46 Bytes

This may not be valid, as it only fits 16 digits.

p (gets.to_i**0.5).to_s.split('.')[1]=~/[5-9]/

• What is the output on 59752? – Tim Nov 6 '15 at 21:25
• nil, as no digit past 4 is in the entire string. It may depend on the ruby version. – MegaTom Nov 6 '15 at 21:30
• It needs to support 59752 - so needs 17 dps – Tim Nov 6 '15 at 21:31