Advertisement
Guest User

Perfect powers in J

a guest
Mar 14th, 2012
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
J 0.51 KB | None | 0 0
  1. NB. Perfect powers
  2.  
  3. NB. Integer logs and roots; work for extended precision.
  4. ilog=: <.@^.
  5. iroot=: <.@%:
  6.  
  7. NB. 'x isEvenRoot y' is true if the xth root of y is an integer.
  8. isEvenRoot=: ] = iroot ^ [
  9.  
  10. NB. 'primesTo y' returns the primes up to the first prime >= y.
  11. primesTo=: 3 : 'p: i.>: _1 p: y'
  12.  
  13. NB. Return a list of the prime powers that "factor" y.
  14. perfectPowersOf=: 3 : '(primesTo 2 ilog y) (isEvenRoot # [) y'
  15.  
  16. NB. Just check if that list is longer than 0.
  17. isPerfectPower=: 3 : '0<#perfectPowersOf y'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement