Commits

Shlomi Fish  committed 7953866

Fixed a lot of bugs.

  • Participants
  • Parent commits 6d364e0

Comments (0)

Files changed (1)

File project-euler/466/euler-466-experimental-with-modulo-bit-vec.pl

 
             for my $maj_factor (2 .. $row_idx)
             {
-                my $maj_checkpoint = min($MAJ * $maj_factor, $end_prod);
-                # my $maj_checkpoint = $MAJ * $maj_factor;
+                # my $maj_checkpoint = min($MAJ * $maj_factor, $end_prod);
+                my $maj_checkpoint = $MAJ * $maj_factor;
 
                 my @prev_rows = ($maj_factor .. $row_idx-1);
                 my $prev_rows_and_step_lcm = $lcms[$maj_factor];
 
                 my $prev_rows_div_step = $prev_rows_and_step_lcm / $step;
 
+                if (0) # ($prev_rows_div_step == 1)
+                {
+                    vec($lookup_vec, 0, 1) = 1;
+                }
+
                 foreach my $prev_row (@prev_rows)
                 {
                     for (my $i = 0; $i < $prev_rows_and_step_lcm ; $i += $prev_row)
                     $maj_start_prod_bound_lcm += $prev_rows_div_step;
                 }
 
+                my $dump_all = sub {
+                    print <<"EOF";
+maj_start_prod_bound_lcm == $maj_start_prod_bound_lcm
+maj_start_prod_div == $maj_start_prod_div
+maj_end_prod_bound_lcm == $maj_end_prod_bound_lcm
+maj_end_prod_div == $maj_end_prod_div
+prod == $prod
+maj_checkpoint == $maj_checkpoint
+prev_rows_div_step == $prev_rows_div_step
+prev_rows_and_step_lcm == $prev_rows_and_step_lcm
+step == $step
+
+EOF
+                };
+
+
                 $found_in_next[$next_row][$row_idx] +=
                 (
-                    ($maj_end_prod_bound_lcm - $maj_start_prod_bound_lcm) / $prev_rows_div_step * $_calc_num_mods->(0, $prev_rows_div_step - 1)
-                    + $_calc_num_mods->(1, $maj_end_prod_div - $maj_end_prod_bound_lcm-1)
+                    (
+                        # Going from the segments' start to the one right
+                        # before the start of the next segment.
+                        (
+                            # (($maj_end_prod_bound_lcm < $maj_start_prod_bound_lcm) ? 0 : 1) +
+                            (
+                                ($maj_end_prod_bound_lcm - $maj_start_prod_bound_lcm)
+                                / $prev_rows_div_step
+                            )
+                        )
+                        * $_calc_num_mods->(0, $prev_rows_div_step - 1)
+                    )
                     +
-                    $_calc_num_mods->(($maj_start_prod_bound_lcm - $maj_start_prod_div), $prev_rows_div_step - 1)
+                    (
+                        0 # ($maj_end_prod_bound_lcm == $maj_end_prod_div)
+                        ? 0
+                        : $_calc_num_mods->(0, $maj_end_prod_div - $maj_end_prod_bound_lcm)
+                    )
+                    +
+                    (
+                        0 # ($maj_start_prod_bound_lcm == $maj_start_prod_div)
+                        ? 0
+                        : $_calc_num_mods->(($maj_start_prod_bound_lcm - $maj_start_prod_div)+1, $prev_rows_div_step - 1)
+                    )
+                    # - (($maj_end_prod_bound_lcm == $maj_start_prod_bound_lcm and ($maj_start_prod_bound_lcm != $maj_start_prod_div)) ? 1 : 0)
+                    # - (($maj_end_prod_bound_lcm == $maj_start_prod_bound_lcm) ? 1 : 0)
                 );
 
                 $prod = $maj_end_prod_div * $step;
 
+                if ($prod == $maj_checkpoint)
+                {
+                    $prod += $step;
+                }
+
 =begin removed
                 $found_in_next[$next_row][$row_idx] +=
                 (
     print "P($MIN, $MAJ) = $got (should be $expected)\n";
 }
 
+if (0)
+{
 my_test(3, 4, 8);
+my_test(4, 4, 9);
+}
+my_test(4, 6, 9);
+
+if (0)
+{
 my_test(64, 64, 1263);
 my_test(12, 345, 1998);
 my_test(32, (('1'.('0'x15))+0), 13826382602124302);
-
+}