9
\$\begingroup\$

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.

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

8 Answers 8

2
\$\begingroup\$

CJam, 17 bytes

rie40mQsJ~>'4f>1#

Try it online.

\$\endgroup\$
2
\$\begingroup\$

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 <
\$\endgroup\$
1
\$\begingroup\$

CJam, 29 26 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).

\$\endgroup\$
1
\$\begingroup\$

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.

\$\endgroup\$
0
1
\$\begingroup\$

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.

\$\endgroup\$
1
\$\begingroup\$

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.

\$\endgroup\$
1
\$\begingroup\$

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

\$\endgroup\$
2
  • \$\begingroup\$ You can remove the whitespace around the Apply. \$\endgroup\$ Nov 7, 2015 at 12:24
  • \$\begingroup\$ Thanks. The byte count stays the same because I had not counted those spaces. \$\endgroup\$
    – DavidC
    Nov 7, 2015 at 13:04
-2
\$\begingroup\$

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]/
\$\endgroup\$
3
  • \$\begingroup\$ What is the output on 59752? \$\endgroup\$
    – Tim
    Nov 6, 2015 at 21:25
  • \$\begingroup\$ nil, as no digit past 4 is in the entire string. It may depend on the ruby version. \$\endgroup\$
    – MegaTom
    Nov 6, 2015 at 21:30
  • \$\begingroup\$ It needs to support 59752 - so needs 17 dps \$\endgroup\$
    – Tim
    Nov 6, 2015 at 21:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.