difference_of_k_powers.pl 643 B

1234567891011121314151617181920212223242526272829303132
  1. #!/usr/bin/perl
  2. # Daniel "Trizen" Șuteu
  3. # License: GPLv3
  4. # Date: 28 April 2017
  5. # https://github.com/trizen
  6. # Find the smallest representations for natural numbers as the difference of some k power.
  7. # Example:
  8. # 781 = 4^5 - 3^5
  9. # 992 = 10^3 - 2^3
  10. # 999 = 32^2 - 5^2
  11. use 5.010;
  12. use strict;
  13. use warnings;
  14. use Math::AnyNum qw(root ceil log2);
  15. OUTER: foreach my $n (1 .. 1000) {
  16. foreach my $i (2 .. ceil(log2($n))) {
  17. my $s = ceil(root($n, $i));
  18. foreach my $k (0 .. $s) {
  19. if ($s**$i - $k**$i == $n) {
  20. say "$n = $s^$i - $k^$i";
  21. next OUTER;
  22. }
  23. }
  24. }
  25. }