in reply to stupid question about switching signs every number
It is possible to write a closed form expression that generates $sum for any X without using a computing loop. I leave that part of your homework for you to figure out.#!/usr/bin/perl -w use strict; my $sum=0; foreach my $num (1..1000) { if ($num % 2) #odd { $sum += $num } else #even { $sum -= $num; } } print $sum;
|
|---|