Advertisement
Revolucent

Some J verbs for handling repunits

Apr 3rd, 2015
2,831
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
J 0.79 KB | None | 0 0
  1. is_integral =: = <.
  2.  
  3. repunit =: 3 : 0 "0
  4.   10 repunit y
  5. :
  6.   NB. Gives the y-th repunit in the x base, with the result expressed
  7.   NB. in base 10. E.g., 3 repunit 4 gives 40, because 40 in base 3 is 1111.
  8.   ((x ^ y) - 1) % x - 1
  9. )
  10.  
  11. which_repunit =: 3 : 0 "0
  12.   10 which_repunit y
  13. :
  14.   NB. An integer solution to this equation means it's a repunit,
  15.   NB. and returns which one it is for that base. E.g., which_repunit 111
  16.   NB. gives the integer 3, so 111 is the third repunit in base 10.
  17.   NB. By contrast, 112 gives 3.00389, which means it is not a repunit.
  18.   NB.
  19.   NB. repunit which_repunit y will always give back y, regardless of whether
  20.   NB. y is a repunit in the given base.
  21.   x ^. (y * (x - 1)) + 1
  22. )
  23.  
  24. is_repunit =: 3 : 0 "0
  25.   10 is_repunit y
  26. :
  27.   is_integral x which_repunit y
  28. )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement