fmny94 has asked for the wisdom of the Perl Monks concerning the following question:

This node falls below the community's threshold of quality. You may see it by logging in.
  • Comment on How do I write a program that calculates the sum of all numbers to a number, n?

Replies are listed 'Best First'.
Re: How do I write a program that calculates the sum of all numbers to a number, n?
by chacham (Prior) on Feb 24, 2015 at 21:22 UTC

    How silly. To calculate the numbers, you would use n(n+1)/2, no loop required. Maybe the loop is to ask for more input. :P

    Anyway, it sounds like the requirements for this work are: get a command line argument, create a loop, and although not mentioned, print it out. It should be easy enough to write this program. Though, if you are having any difficulty, show us your work, and perhaps we can help. :)

Re: How do I write a program that calculates the sum of all numbers to a number, n?
by BrowserUk (Patriarch) on Feb 24, 2015 at 22:59 UTC
    $n = 13; $sum = 0; $sum += $n-- while $n; print $sum;; 91

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority". I'm with torvalds on this
    In the absence of evidence, opinion is indistinguishable from prejudice. Agile (and TDD) debunked
Re: How do I write a program that calculates the sum of all numbers to a number, n?
by ikegami (Patriarch) on Feb 24, 2015 at 21:46 UTC

    The following calculates the sum efficiently (sum(1..N) = N(N+1)/2), yet finds a legitimately use for foreach:

    foreach my $n (@ARGV) { print($n*($n+1)/2, "\n"); }
Re: How do I write a program that calculates the sum of all numbers to a number, n?
by Tux (Canon) on Feb 24, 2015 at 21:15 UTC
    say [+] ^$n

    Enjoy, Have FUN! H.Merijn
      Perl 6 mojo?

        We told you guys; it’s fun! Though I’m not positive the answer shouldn’t have been 91 instead… to v through.

        ~>perl6 > $n = 13 13 > say [+] ^$n 78
        "...mojo?"

        Or Voodoo? Perhaps a servant of the spirits?

        Best regards, Karl

        «The Crux of the Biscuit is the Apostrophe»

Re: How do I write a program that calculates the sum of all numbers to a number, n?
by AppleFritter (Vicar) on Feb 24, 2015 at 22:39 UTC

    Just do it in a slightly more complicated manner than is strictly necessary...

    #!/usr/bin/perl use strict; use warnings; use feature qw/say/; while(my $n = shift @ARGV) { say grep { $n == $_ } map { $n += $_ } map { $n-- } map { ($n) x $_ } $n ;}
Re: How do I write a program that calculates the sum of all numbers to a number, n?
by james28909 (Deacon) on Feb 24, 2015 at 21:41 UTC
    A few clues, you will need an infinite loop, and you will need to read and sum user input. Hope that helps

    EDIT: And depending on how you want to do this, you can either set it up to accept one set of numbers per loop, or you can input them all at once with a "+" in between them like "1+2+21+46+3+5" (second example). You would need to split user input to an array, then sum the array.
    use strict; use warnings; my $sum = 0; while(1){ print "Please input a number: "; chomp(my $number = <STDIN>); add $number to $sum; print "Your sum is now: $sum"; }
    OR
    use strict; use warnings; print "Please input your string of numbers seperated by "+"; my @array = split(/[+]/, <STDIN>); my $sum = 0; foreach(@array){ add each number to the $sum } print "This is your sum: $sum";
    This is non usable code and used only as a reference, Good Luck!
Re: How do I write a program that calculates the sum of all numbers to a number, n?
by Anonymous Monk on Feb 24, 2015 at 21:56 UTC
    use strict; use warnings; use 5.016; my $n = $ARGV[0] or die "Give a damn! Give a number."; require List::Util; say List::Util::sum( 1 .. $n );

      Now with Extra Fracking for-Loop!

      ... say List::Util::sum( 1 .. $n ) for 0;
Re: How do I write a program that calculates the sum of all numbers to a number, n?
by hdb (Monsignor) on Feb 25, 2015 at 07:46 UTC

    Here is a simulation approach:

    use strict; use warnings; my $n = shift; my $sum = 0; my $count = 0; while( $sum != ($n+1)*$n/2 ) { $sum = 1 + int( rand( $n**2 ) ); $count++; } print "Sum of 1 to $n: $sum (found after $count iterations)\n";
Re: How do I write a program that calculates the sum of all numbers to a number, n?
by Anonymous Monk on Feb 24, 2015 at 21:00 UTC
    We don't do homework for you. What have you tried so far and where are you stuck?
Re: How do I write a program that calculates the sum of all numbers to a number, n?
by Anonymous Monk on Feb 24, 2015 at 21:14 UTC
    Read the assigned reading, attend the lecture , talk to your classmates, then try it, give it a try, write some stuff, see what happens
Re: How do I write a program that calculates the sum of all numbers to a number, n?
by karlgoethebier (Abbot) on Feb 25, 2015 at 10:52 UTC
    "We don't do homework for you..." (Anonymous Monk)

    This ain't true, as you can see - but it may be the provided solutions don't meet your specs sometimes ;-)

    So i feel free to put in my 2 ˘:

    Edit: In German this sounds much better: Ich bin so frei, meinen Senf dazu zu geben.

    #!/usr/bin/env perl use strict; use warnings; use List::Util qw(reduce); use feature qw (say); my $n = shift || die qq(Usage $0 [number]); say reduce { $a + $b } 1 .. $n; __END__

    Please see also There's more than one way to do it as well as List::Util.

    Best regards, Karl

    «The Crux of the Biscuit is the Apostrophe»

Re: How do I write a program that calculates the sum of all numbers to a number, n? (evil eval)
by Discipulus (Canon) on Feb 25, 2015 at 12:14 UTC
    This satisfy all requiremts (It must contain a foreach or while command.) (modded from my evil answer to the almost same question)...
    WinZ>perl -e " print eval join '+',(1..$ARGV[0]);exit while 1" 13 91


    Serious answer: in some monk's signature there is something like: think a solution without a computer and then translate in our preferred language. for example: I have a Sum, empty for the moment. Then I expand numbers from 1 to GivenNumber (list expansion). Now for each number in the list I add it to Sum. I write down Sum.

    HtH
    L*
    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
Re: How do I write a program that calculates the sum of all numbers to a number, n?
by Don Coyote (Hermit) on Feb 25, 2015 at 09:50 UTC

    This response is not intended to be taken literally. :p

    The sum of ALL numbers being negative 1/12, would the sum of all numbers up to N not be some type of a function? In the manner of

    sum( N,N-1,N-2..N=1 ) = N / ( -1/12 )

    Oh yes, I see now n(n+1)/2, overthinking it again.

    Although that might be N * -1/12 (but is probably neither). For another undefined definition, maybe just bitmasking the sum of all numbers to length N. This could work well if soan is truly irrational.

    sub donotrun_this_is_an_example{ use constant Soan = (-1/12); my $bitmask = ~ -256**($n/8); return Soan & $bitmask; }

    similar idea would be to return the of multiplying Soan by 10 to power of N: int( Soan*10**$n )

    Edit: Soan is negative 1/12


    Alert! vague conjecture recollection syndrome and Random thought process combination.