Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.programming > #1627

Re: Project Euler - add all natural numbers below 1000

From "BartC" <bc@freeuk.com>
Newsgroups comp.programming
Subject Re: Project Euler - add all natural numbers below 1000
Date 2012-05-27 01:52 +0100
Organization A noiseless patient Spider
Message-ID <jprtsj$q3u$1@dont-email.me> (permalink)
References <4fbcc3ed$0$283$14726298@news.sunsite.dk>

Show all headers | View raw



"arnuld" <sunrise@invalid.address> wrote in message 
news:4fbcc3ed$0$283$14726298@news.sunsite.dk...
> AIM: To write a program to add all natural numbers below 1000 that are
> multiples of 3 or 5.
>
> WHAT I GOT:  it works
>
> Question-1:  Look at the for loop, instead of looping for 1000 times and
> checking the value inside, I am simply looping it 1000/3 = 333 times. Ist
> that impressive ?
>
> Question-2: Can it be improved ?

It could be more straightforward. Working in C as you seem to, it can be 
just:

#include <stdio.h>

int main(void){
int i,sum=0;

for (i=1; i<1000; ++i)
 if (i%3==0 || i%5==0) sum+=i;

printf("Sum = %d\n",sum);

}

Although not efficient (and the result can anyway be calculated without 
loops), this at least gives the right answer by not trying anything clever.

-- 
Bartc 

Back to comp.programming | Previous | NextPrevious in thread | Find similar


Thread

Project Euler - add all natural numbers below 1000 arnuld <sunrise@invalid.address> - 2012-05-23 11:03 +0000
  Re: Project Euler - add all natural numbers below 1000 Bjoern Hoehrmann <bjoern@hoehrmann.de> - 2012-05-23 13:30 +0200
  Re: Project Euler - add all natural numbers below 1000 "H.J. Sander Bruggink" <sander.bruggink@uni-due.de> - 2012-05-23 14:11 +0200
    Re: Project Euler - add all natural numbers below 1000 arnuld <sunrise@invalid.address> - 2012-05-23 12:44 +0000
      Re: Project Euler - add all natural numbers below 1000 Ike Naar <ike@sverige.freeshell.org> - 2012-05-23 13:55 +0000
      Re: Project Euler - add all natural numbers below 1000 bob <bob@coolfone.comze.com> - 2012-05-23 06:56 -0700
  Re: Project Euler - add all natural numbers below 1000 jt@toerring.de (Jens Thoms Toerring) - 2012-05-25 09:29 +0000
    Re: Project Euler - add all natural numbers below 1000 bob <bob@coolfone.comze.com> - 2012-05-25 06:26 -0700
      Re: Project Euler - add all natural numbers below 1000 jt@toerring.de (Jens Thoms Toerring) - 2012-05-26 21:52 +0000
  Re: Project Euler - add all natural numbers below 1000 "BartC" <bc@freeuk.com> - 2012-05-27 01:52 +0100

csiph-web