207 Integer partition equations -- v2.pl 984 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #!/usr/bin/perl
  2. # Author: Daniel "Trizen" Șuteu
  3. # Date: 24 August 2016
  4. # Edit: 18 September 2019
  5. # License: GPLv3
  6. # Website: https://github.com/trizen
  7. # https://projecteuler.net/problem=207
  8. # Runtime: 2.689s
  9. use 5.010;
  10. use strict;
  11. use warnings;
  12. my %table;
  13. sub pp_ratio {
  14. my ($n) = @_;
  15. my $perfect = 0;
  16. my $non_perfect = 0;
  17. for (my $i = 1 ; ; ++$i) {
  18. my $k = $i * ($i + 1);
  19. last if $k > $n;
  20. exists($table{$k}) ? ++$perfect : ++$non_perfect;
  21. }
  22. $perfect / ($perfect + $non_perfect);
  23. }
  24. my $ratio = 1 / 12345;
  25. foreach my $i (1 .. 30) {
  26. undef $table{4**$i - 2**$i};
  27. }
  28. my $j = 1;
  29. my $k = 1;
  30. my $upper_bound;
  31. for (my $i = 1 ; ; $i += $k) {
  32. if (pp_ratio($i * ($i + 1)) < $ratio) {
  33. $upper_bound = $i * ($i + 1);
  34. last;
  35. }
  36. $j += 1;
  37. $k += $j;
  38. }
  39. for (my $i = int(sqrt($upper_bound) - $j) ; ; ++$i) {
  40. if (pp_ratio($i * ($i + 1)) < $ratio) {
  41. say $i * ($i + 1);
  42. last;
  43. }
  44. }