in reply to Re^2: Best way to make sure a number is an even multiple of another?
in thread Best way to make sure a number is an even multiple of another?

demerphq,
I am guilty. I am guilty of not spending enough time reading your question to see what you were asking for before jumping to conclusions. The conclusion I jumped to is that you wanted the largest factor greater than or equal to a certain number. To that end, here is code that I believe does that.
#!/usr/bin/perl use strict; use warnings; use Math::Big::Factors 'factors_wheel'; my $next = factor(100, 25); while ( my $factor = $next->() ) { print "$factor\n"; } sub factor { my ($target, $min) = @_; my @list = factors_wheel( $target ); my $by = $#list; my (%seen, @pos, @stop, $end_pos); my ($factor, $next, $continue) = (1, 1, 1); return sub { { $factor = 1; if ( $next ) { return () if ! $by || ! $continue; $continue = 0; @pos = (0 .. $by - 2, $by - 2); @stop = @list - $by .. $#list; $end_pos = $#pos; $by--; } my $cur = $end_pos; { if ( ++$pos[ $cur ] > $stop[ $cur ] ) { $pos[ --$cur ]++; redo if $pos[ $cur ] > $stop[ $cur ]; my $new_pos = $pos[ $cur ]; @pos[ $cur .. $end_pos ] = $new_pos .. $new_pos + +$by; } } $next = $pos[0] == $stop[0] ? 1 : 0; $factor *= $_ for @list[ @pos ]; $continue = 1 if $factor >= $min; redo if $seen{$factor}++ || $factor < $min; } return $factor; } }
While I know that this isn't what you asked for, it might be of benefit to someone.

Cheers - L~R

  • Comment on Re^3: Best way to make sure a number is an even multiple of another?
  • Download Code