Velocity Reviews > eratosthenos

# eratosthenos

Joe Smith
Guest
Posts: n/a

 04-26-2006
It is nothing short of embarrassing to feel the need to ask for help on
this. I can't see how I would make the main control for this. What I want
is a for loop and a test condition. And while I know, from things I
pondered 2 decades ago, that a fella can write code without a goto, I'm
stuck.

/* sieve1.c */

#define whatever 20
#define N whatever
#include <stdio.h>

int main(void)
{
int i, A[N+1], m, sum;

/* initialize to 0 */

for (i = 0; i <= N; ++ i) A[i] = 0;

/* output */
printf("Primes less than N are:\n");
for (i = 2; i <= N; ++ i)
{
if (A[i] == 0)
printf("%d

Barry Schwarz
Guest
Posts: n/a

 04-30-2006
On Wed, 26 Apr 2006 19:14:03 -0400, "Joe Smith"
<(E-Mail Removed)> wrote:

>It is nothing short of embarrassing to feel the need to ask for help on
>this. I can't see how I would make the main control for this. What I want
>is a for loop and a test condition. And while I know, from things I
>pondered 2 decades ago, that a fella can write code without a goto, I'm
>stuck.
>
>/* sieve1.c */
>
>#define whatever 20
>#define N whatever
>#include <stdio.h>
>
>int main(void)
>{
>int i, A[N+1], m, sum;

If you change it to A[N+1]=0 you can eliminate the initialization loop
below.

>
>/* initialize to 0 */
>
>for (i = 0; i <= N; ++ i) A[i] = 0;
>

For each i that is not prime, you want to set A[i] to a non-zero
value. One way is

for (i = 2; i <= N; i++)
if (A[i] == 0)
for (j = i+i; j <= N; j += i)
A[j] = 1;

>
>
>/* output */
>printf("Primes less than N are:\n");
>for (i = 2; i <= N; ++ i)
>{
>if (A[i] == 0)
>printf("%d
>

It looks like the tail end of your program fell into the bit bucket.

Remove del for email

Dave Thompson
Guest
Posts: n/a

 05-11-2006
On Sat, 29 Apr 2006 17:07:56 -0700, Barry Schwarz <(E-Mail Removed)>
wrote:

> On Wed, 26 Apr 2006 19:14:03 -0400, "Joe Smith"
> <(E-Mail Removed)> wrote:
>

> >int i, A[N+1], m, sum;

>
> If you change it to A[N+1]=0 you can eliminate the initialization loop
> below.
>

Must use braces for an array: ... A[N+1] = {0}

- David.Thompson1 at worldnet.att.net

Joe Smith
Guest
Posts: n/a

 05-15-2006

"Dave Thompson" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> On Sat, 29 Apr 2006 17:07:56 -0700, Barry Schwarz <(E-Mail Removed)>
> wrote:
>
>> On Wed, 26 Apr 2006 19:14:03 -0400, "Joe Smith"
>> <(E-Mail Removed)> wrote:
>>

>
>> >int i, A[N+1], m, sum;

>>
>> If you change it to A[N+1]=0 you can eliminate the initialization loop
>> below.
>>

> Must use braces for an array: ... A[N+1] = {0}

Thanks. Down yonder I think I have this algorithm correct using static and
dynamic mem allocation. joe
-------------
character test here ??????