Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

beginner help

by derpp (Acolyte)
on Aug 02, 2010 at 19:39 UTC ( [id://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 (Canon) 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 (Chancellor) 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 (Prior) 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 (Deacon) 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?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://852522]
Approved by kennethk
Front-paged by davido
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (5)
As of 2024-03-19 09:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found