# eratosthenos

Discussion in 'C Programming' started by Joe Smith, Apr 27, 2006.

1. ### Joe SmithGuest

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 = 0;

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

Joe Smith, Apr 27, 2006

2. ### Barry SchwarzGuest

On Wed, 26 Apr 2006 19:14:03 -0400, "Joe Smith"
<> 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 = 0;
>

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

for (i = 2; i <= N; i++)
if (A == 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 == 0)
>printf("%d
>

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

Remove del for email

Barry Schwarz, Apr 30, 2006

3. ### Dave ThompsonGuest

On Sat, 29 Apr 2006 17:07:56 -0700, Barry Schwarz <>
wrote:

> On Wed, 26 Apr 2006 19:14:03 -0400, "Joe Smith"
> <> 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

Dave Thompson, May 11, 2006
4. ### Joe SmithGuest

"Dave Thompson" <> wrote in message
news:...
> On Sat, 29 Apr 2006 17:07:56 -0700, Barry Schwarz <>
> wrote:
>
>> On Wed, 26 Apr 2006 19:14:03 -0400, "Joe Smith"
>> <> 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 ??????

Joe Smith, May 15, 2006