prog.pl 953 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. #!/usr/bin/perl
  2. # A triangular number other than 1, 10 and 10011 which contain the digit 0 and 1 only.
  3. # Problem: CYF 34.
  4. # The next term (if it exists) is greater than 10^31.
  5. use 5.036;
  6. use Math::Prime::Util::GMP qw(is_polygonal);
  7. use Algorithm::Combinatorics qw(variations_with_repetition);
  8. my @data = ('0', '1');
  9. foreach my $k (31 .. 1000) {
  10. say "Testing: k = $k";
  11. my $iter = variations_with_repetition(\@data, $k);
  12. while (my $arr = $iter->next) {
  13. if (is_polygonal(join('', '1', @$arr), 3)) {
  14. die "Found new term: ", join('', '1', @$arr), "\n";
  15. }
  16. }
  17. }
  18. __END__
  19. for k in (1..1000) {
  20. say "Testing: k = #{k}"
  21. [0,1].variations_with_repetition(k, {|*a|
  22. var n = Num('1' + a.join)
  23. if (n.is_polygonal(3)) {
  24. say n
  25. }
  26. })
  27. }
  28. __END__
  29. Testing: k = 28
  30. Testing: k = 29
  31. Testing: k = 30
  32. Testing: k = 31
  33. ^C
  34. perl x.sf 9139.72s user 11.58s system 96% cpu 2:37:47.16 total