Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

beginner help

by derpp (Acolyte)
on Aug 02, 2010 at 19:39 UTC ( #852522=perlquestion: print w/replies, xml ) Need Help??
derpp has asked for the wisdom of the Perl Monks concerning the following question:

hi guys, i'm a beginner at perl and i'm stuck on a simple (to you, probably) problem.

i'm trying to write a short program that, simply put, finds all the numbers between two entered numbers and adds them.

ex: if i enter 1 and 100, i'd want my program to get the sum of 1+2+3+4..100.

the part where i'm stuck is..getting all the numbers between.

please help D:

Replies are listed 'Best First'.
Re: beginner help
by salva (Abbot) on Aug 02, 2010 at 20:00 UTC
    Probably, this is not what you need but I can't keep from pointing out that...
    my $sum = ($n2 + $n1) * ($n2 - $n1 + 1) / 2;

        (If you're not interested in computer science subtleties, please ignore this node - for normal programming tasks the O(1) estimate is good).

        For big numbers the algorithm is slower than O(n), while the summing all the values is O(2^n).

        In computer science, if you are talking about O(f(n)), n is the input length, not the number that the input presents.

        So adding two numbers with length n is O(n), multiplying two numbers naively is O(n^2), there are a few more elaborate algorithms that do it a bit faster.

        Summing all numbers from 1 to $number is O(2^n), because the length of $number is what we call n.

        (This calculation kinda assumes use bigint;, because otherwise on the perl vm adding and multiplying is O(1) indeed)

Re: beginner help
by toolic (Bishop) on Aug 02, 2010 at 19:46 UTC
Re: beginner help
by FunkyMonk (Canon) on Aug 02, 2010 at 19:48 UTC
    for my $num ($first .. $last) { # do something with $num }

    That for loop will iterate over the numbers in the range, $first to $last.

    It sounds like you aren't using a book to learn from. I'd recommend Learning Perl.

Re: beginner help
by VinsWorldcom (Parson) on Aug 02, 2010 at 19:49 UTC

    You can start with this, then add in the START number and some error checking ...

    #!/usr/bin/perl use strict; my $end = $ARGV[0] || 1; my $sum = 0; for (1..$end) { $sum += $_ } print "Summation = $sum\n";

    ... and some example runs ...

    {Z} \\vmware-host\Shared Folders > test Summation = 1 {Z} \\vmware-host\Shared Folders > test 4 Summation = 10
Re: beginner help
by baxy77bax (Chaplain) on Aug 03, 2010 at 10:09 UTC
    this could help :)
    my $x = 1; my $y = 100; my $sum += $_ for $x..$y; print $sum;
Re: beginner help
by perlCrazy (Monk) on Aug 03, 2010 at 11:19 UTC
    use List::Util qw[sum ];<br> my $totalSum = sum (1..100); print "$totalSum\n";
Re: beginner help
by nvivek (Vicar) on Aug 03, 2010 at 04:59 UTC

    You could do this easily.As others said, you can use range operator to get all the intermediate numbers.If you're getting the start and end numbers from user.You need to check whether the end no is greater than start no.If not, you can't get the range in between them.

Re: beginner help
by jdrago999 (Pilgrim) on Aug 04, 2010 at 18:23 UTC
    #!/usr/bin/perl -w use strict; use warnings 'all'; my ($n1, $n2) = @ARGV; die "Usage:\n$0 <n1> <n2>\n" unless defined($n1) && defined($n2); my $total = 0; map { $total += $_ } $n1..$n2; print "Total from $n1 to $n2: $total\n";

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://852522]
Approved by kennethk
Front-paged by davido
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (6)
As of 2017-11-19 18:47 GMT
Find Nodes?
    Voting Booth?
    In order to be able to say "I know Perl", you must have:

    Results (282 votes). Check out past polls.