No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

is-squarenumber.b93 2.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. v### v <
  2. > >$30gv
  3. vp01g03_^#-g01:_^#- <:
  4. >:30p:20 g\/+2/: 30g^*
  5. > >#^ #p #0 2 #<$ v
  6. > 98+ >1+: :88*%"9"`#^_:88+%:9`#v_:2-#v_v 2
  7. > > > > # >$>$0> . " ",, :. 55+,^
  8. |!-8_^#-7:_^#-6:_^#-5:_^# -3:< g
  9. > ^< 0 <^:p010 <^<-!
  10. >:55+%:7-!#^_:3-!#^_2-!#^_:3%2-#^_^>^
  11. [10] pxk
  12. [20] n
  13. [30] xk
  14. Is SquareNumber :: a --> sqrt(a)%1 == 0
  15. (stack) --> (stack)
  16. [https://en.wikipedia.org/wiki/Integer_square_root]
  17. [http://www.ask-math.com/properties-of-square-numbers.html]
  18. [http://www.johndcook.com/blog/2008/11/17/fast-way-to-test-whether-a-number-is-a-square]
  19. [http://stackoverflow.com/questions/295579/fastest-way-to-determine-if-an-integers-square-root-is-an-integer]
  20. ~~~
  21. // Conditions for _NOT_ an square number
  22. x % 64 > 57 OR
  23. x % 16 > 9 OR
  24. x % 16 == 2 OR
  25. x % 16 == 3 OR
  26. x % 16 == 5 OR
  27. x % 16 == 6 OR
  28. x % 16 == 7 OR
  29. x % 16 == 8 OR
  30. x % 10 == 2 OR
  31. x % 10 == 3 OR
  32. x % 10 == 7 OR
  33. x % 3 == 2
  34. ~~~
  35. //#############################################################################
  36. public bool isISqrt(int x)
  37. {
  38. // http://www.ask-math.com/properties-of-square-numbers.html
  39. // http://www.johndcook.com/blog/2008/11/17/fast-way-to-test-whether-a-number-is-a-square/
  40. // http://stackoverflow.com/questions/295579/fastest-way-to-determine-if-an-integers-square-root-is-an-integer
  41. if (x % 16 > 9) return false;
  42. if (x % 64 > 57) return false;
  43. if (x % 16 == 2) return false;
  44. if (x % 16 == 3) return false;
  45. if (x % 16 == 5) return false;
  46. if (x % 16 == 6) return false;
  47. if (x % 16 == 7) return false;
  48. if (x % 16 == 8) return false;
  49. if (x % 10 == 2) return false;
  50. if (x % 10 == 3) return false;
  51. if (x % 10 == 7) return false;
  52. if (x % 3 == 2) return false;
  53. return Math.Pow(isqrt(x), 2) == x;
  54. }
  55. // https://en.wikipedia.org/wiki/Integer_square_root
  56. int isqrt(int n)
  57. {
  58. int pxk = 0;
  59. int xk = n;
  60. for (;;)
  61. {
  62. int nxk = (xk + n / xk) / 2;
  63. if (nxk == xk) return xk;
  64. if (nxk == pxk) return xk;
  65. pxk = xk;
  66. xk = nxk;
  67. }
  68. }