Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.programming > #1616
| From | bob <bob@coolfone.comze.com> |
|---|---|
| Newsgroups | comp.programming |
| Subject | Re: Project Euler - add all natural numbers below 1000 |
| Date | 2012-05-23 06:56 -0700 |
| Organization | http://groups.google.com |
| Message-ID | <7146406e-511b-41e5-81a0-386f9d79564b@googlegroups.com> (permalink) |
| References | <4fbcc3ed$0$283$14726298@news.sunsite.dk> <a242erF8c7U1@mid.dfncis.de> <4fbcdbbd$0$283$14726298@news.sunsite.dk> |
On Wednesday, May 23, 2012 7:44:45 AM UTC-5, arnuld wrote:
> > On Wed, 23 May 2012 14:11:07 +0200, H.J. Sander Bruggink wrote:
> >> arnuld wrote:
>
> > Does it? How did you test it?
> >
> > Let's test it with a limit of 16 instead of 1000. What is the correct
> > answer? I suspect you want it to be
> >
> > 3 + 5 + 6 + 9 + 10 + 12 + 15 = 60
> >
> > but the output of your program is 75 because it will add 15 twice (it is
> > a multiple of both 3 and 5).
>
> Down here is the corrected version.
>
>
>
> >> Question-2: Can it be improved ?
> >
> > Yes, programs that don't work can always be improved.
>
> :)
>
>
>
> /* Add all the natural numbers below one thousand that are multiples of 3
> or 5.
> * version 0.1
> */
>
> #include <stdio.h>
> #include <stdlib.h>
>
> int main(void)
> {
> int i;
> unsigned long s = 0;
>
> for (i = 3; i <= 1000; i += 3)
> s += i;
>
> for (i = 5; i < 1000; i += 5)
> {
> if(i % 3) s += i;
> }
>
> printf("%lu\n", s);
>
> return EXIT_SUCCESS;
> }
>
>
> --
> arnuld
> http://LispMachine.Wordpress.com
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int i;
unsigned long s = 0;
for (i = 3; i <= 1000; i += 3)
s += i;
for (i = 5; i < 1000; i += 5)
{
s += i;
}
for (i = 15; i < 1000; i += 15)
{
s -= i;
}
printf("%lu\n", s);
return EXIT_SUCCESS;
}
Back to comp.programming | Previous | Next — Previous in thread | Next in thread | Find similar
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