SHARE
TWEET

Perfect powers in J

a guest Mar 14th, 2012 103 Never
  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'
RAW Paste Data
Top