in reply to Re^2: Nested loops
in thread Nested loops
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__
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: Nested loops
by robert44444uk (Acolyte) on Sep 20, 2015 at 18:58 UTC |