in reply to Re: Nested loops
in thread Nested loops

Thank you all for your contributions and thank you Laurent_R for your improved code

Replies are listed 'Best First'.
Re^3: Nested loops
by shadowsong (Pilgrim) on Sep 14, 2015 at 22:26 UTC

    Hi robert44444uk,

    Just to further add to what the Monks above have imparted - ever heard of recursion?

    #! perl -slw use strict; sub count_id (\@\@); # "count indivisible dividends" fnc prototype sub count_id (\@\@) { my ($divisors,$range) = (shift,shift); return 0 unless @$range && @$divisors; # check terminal condition # apply the $divisors to the dividend for this recursive call my ($aggregate,$dividend) = (0,pop @$range); for (@$divisors) { return count_id(@$divisors,@$range) if !($divid +end % $_); } return 1 + count_id(@$divisors,@$range); } my @divisors = (13,17,19); my @range = (1..2310); print "Number of indivisible items: ",count_id(@divisors,@range),"\n"; __END__

      Thank you Shadowsong, this is also a very useful piece of code, and I will play with it for sure, when I get the chance.