-4
\$\begingroup\$

One square root function for unsigned numbers

Write the function sqrti(x) that from one not negative integer number x, it returns the integer square root of that number truncate to the 0 digit, so the result will be the same of

floor(x^(1/2))

You may not use as function and operation nothing other than floor and sign function,+-*/ operations on integers or float numbers (the 4 operation sum, difference, multiplication, division). Are allowed all compare and assignment operation on integer and float numbers as =,<,<=,==,!= etc.

Are allowed the loops, all control flow with goto, the if, the case flow control etc.

Are allowed both recursive and iterative functions.

Win the one write one function with right results, and more short.

Example
Sqrti(0)-->0
Sqrti(1)-->1
Sqrti(2)-->1
Sqrti(3)-->1
Sqrti(4)-->2
Sqrti(5)-->2
Sqrti(999999999999999)=31622776
\$\endgroup\$

closed as unclear what you're asking by Luis Mendo, Shaggy, Kevin Cruijssen, Sriotchilism O'Zaic, mbomb007 Jul 9 at 13:36

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 5
    \$\begingroup\$ This challenge has several issues. (1) It is unclear what functions are allowed and what are not. For example, can we use a function that finds the indices of nonzero elements in an array? (2) Banning certain functions is close to being unobservable. (3) Challenges of the form Do X without Y are generally problematic (4) The challenge assumes language features \$\endgroup\$ – Luis Mendo Jul 9 at 6:33
  • \$\begingroup\$ @LuisMendo the function finds indices nonzero elements in a array, is not one allowed: It is not +-*/ operation, not assigned operation or compare from integers. It is not floor or sign function too \$\endgroup\$ – RosLuP Jul 9 at 6:41
  • \$\begingroup\$ Can we limit ourselves to the sizes of our chosen language's integer types, or are we to implement bignum operations? I would have assumed the former, but the responses by OP on various answers make me curious. \$\endgroup\$ – gastropner Jul 9 at 7:26
  • \$\begingroup\$ @gastropner ok if you have big number unsigned, one sqrti() function on big number unsigned that return big number unsigned... In Nars I have one it seems ok for float cast as unsigned (all values of the input range) and all values of big rational number cast as big unsigned. Where for "cast" I mean that value that is one unsigned (example float part ==0, or denominator =1) \$\endgroup\$ – RosLuP Jul 9 at 7:50
  • \$\begingroup\$ @RosLuP You shouldn't make such assumptions about languages. \$\endgroup\$ – Adám Jul 9 at 8:34
2
\$\begingroup\$

C (gcc), 31 bytes

f(n,r){for(r=n;r*r>n;r--);n=r;}

Try it online!

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

APL (Dyalog Unicode), 19 bytesSBCS

Anonymous tacit prefix function. Recursive.

{⍺<⍵×⍵:⍵-1⋄⍺∇1+⍵}∘0

Try it online!

{}∘0 apply the following anonymous lambda, initially with 0 as right argument () and the given argument as left argument ():

⍺<⍵×⍵: if the left argument is less than the right argument times the right argument:

  ⍵-1 return the right argument minus one

 else:

  ⍺∇1+⍵ call self with same left argument and incremented right argument

\$\endgroup\$
  • \$\begingroup\$ This should be ok \$\endgroup\$ – RosLuP Jul 9 at 7:00
  • \$\begingroup\$ But one number unsigned a little more big as 9999999999999 would not produce stack overflow? \$\endgroup\$ – RosLuP Jul 9 at 7:03
  • \$\begingroup\$ @RosLuP It will take a while, but APL has tail recursion. \$\endgroup\$ – Adám Jul 9 at 7:04
1
\$\begingroup\$

APL (Dyalog Unicode), 18 bytesSBCS

Full program. Prompts for x via stdin.

¯1+1+⍣{x<⍺×⍺}0×x←⎕

Try it online!

x←⎕ prompt for x via the console (stdin)

 multiply that with 0 (gives 0)

1+⍣{} add 1 to that until:

x<⍺×⍺ x is smaller than the current value times itself

¯1+ subtract one

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

APL (Dyalog Unicode), 6 bytesSBCS

Anonymous tacit prefix function.

⌊×⍨⍣¯1

Try it online!

floor of

×⍨ self-multiplication

⍣¯1 applied negative one time

\$\endgroup\$
  • \$\begingroup\$ Iota is one operation not allowed \$\endgroup\$ – RosLuP Jul 9 at 6:50
  • \$\begingroup\$ @RosLuP Is this OK? If not, I'll delete in a a couple of hours. \$\endgroup\$ – Adám Jul 9 at 7:07
  • \$\begingroup\$ Ok, but how many time would pass first return one result for 9999999999999999999999999999999999999999999999999999990999999999999999999999999999999999999999999999999999999999? \$\endgroup\$ – RosLuP Jul 9 at 7:16
  • \$\begingroup\$ @RosLuP That would normally be considered a float, not an int. In 64-bit float mode, it takes less than half a nanosecond. In 128-bit mode, it takes less than a nanosecond. \$\endgroup\$ – Adám Jul 9 at 8:32
  • \$\begingroup\$ 15 digits for 80 bits, if there are more digits 64 bit for me are not enough in one single operation +-*/ or I remember wrong? For 128 will be 30 decimal digits if are more in a single +-*/ operation will be a loss of data and wrong result... Possible I remember wrong \$\endgroup\$ – RosLuP Jul 9 at 8:39

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