Pastebin launched a little side project called HostCabi.net, check it out ;-)Don't like ads? PRO users don't see any ads ;-)
Guest

Perfect powers in J

By: a guest on Mar 14th, 2012  |  syntax: J  |  size: 0.51 KB  |  hits: 78  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  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'