Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re^3: Nested loops

by shadowsong (Monk)
on Sep 14, 2015 at 22:26 UTC ( #1141983=note: print w/replies, xml ) Need Help??


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

    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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1141983]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (3)
As of 2019-05-27 05:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you enjoy 3D movies?



    Results (154 votes). Check out past polls.

    Notices?
    • (Sep 10, 2018 at 22:53 UTC) Welcome new users!