squarefree_divisors.pl 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #!/usr/bin/perl
  2. # Author: Trizen
  3. # Date: 08 July 2018
  4. # https://github.com/trizen
  5. # Generate the squarefree divisors of a given number.
  6. # See also:
  7. # https://oeis.org/A048250
  8. use 5.036;
  9. use ntheory qw(:all);
  10. sub squarefree_divisors($n) {
  11. my @d = (1);
  12. my @pp = map { $_->[0] } factor_exp($n);
  13. foreach my $p (@pp) {
  14. push @d, map { mulint($_, $p) } @d;
  15. }
  16. return sort { $a <=> $b } @d;
  17. }
  18. foreach my $n (1 .. 20) {
  19. my @d = squarefree_divisors($n);
  20. say "squarefree divisors of $n: [@d]";
  21. }
  22. __END__
  23. squarefree divisors of 1: [1]
  24. squarefree divisors of 2: [1 2]
  25. squarefree divisors of 3: [1 3]
  26. squarefree divisors of 4: [1 2]
  27. squarefree divisors of 5: [1 5]
  28. squarefree divisors of 6: [1 2 3 6]
  29. squarefree divisors of 7: [1 7]
  30. squarefree divisors of 8: [1 2]
  31. squarefree divisors of 9: [1 3]
  32. squarefree divisors of 10: [1 2 5 10]
  33. squarefree divisors of 11: [1 11]
  34. squarefree divisors of 12: [1 2 3 6]
  35. squarefree divisors of 13: [1 13]
  36. squarefree divisors of 14: [1 2 7 14]
  37. squarefree divisors of 15: [1 3 5 15]
  38. squarefree divisors of 16: [1 2]
  39. squarefree divisors of 17: [1 17]
  40. squarefree divisors of 18: [1 2 3 6]
  41. squarefree divisors of 19: [1 19]
  42. squarefree divisors of 20: [1 2 5 10]