Implementation of Eratosthenes sieve in C language

Asked 2 years ago, Updated 2 years ago, 33 views

I understood the principle of reading the hints or listening to the teacher's explanation, but I don't know where to start writing the program at all.

By the way, here are some tips given:

hints:
Step 1: Sift all integers from 2 to N.Let m=1.
Step 2: Let n be the minimum number greater than m left on the sieve.n>If でN, go to Step 4
Step 3: Remove all multiples of n from the sieve (they are multiples of n, so they are not prime numbers).
m = n. Go to Step 3.
Step 4: Finish.The number remaining on the sieve is prime.

As mentioned above, not knowing where to start writing programs means that you don't know what to use to get closer to solving problems out of countless c languages such as int..., void..., int main()..., while statements.
It hasn't been long since I started programming, and I'm looking forward to hearing from you.

c

2022-09-30 16:23

2 Answers

First, consider what kind of data you need to use.There is a word "sieve" in the hint, but it is not provided in C language.What kind of mechanism should I use to make a sieve?

Looking at the conditions of the sieve in the hint, it seems that the sieve requires the ability to express whether a certain natural number remains or not.

There are several ways to express this in C language, but how about using an array, for example?In other words, if you create an array such as int furui[], the array subscript becomes a natural number, so you can express 数no natural number i いない as if the array element furui[i] is 0.Also, I chose 0 for 0. If it's 0, I'll make up my mind that if it's not 0, it's still there.

Now that we've decided, let's make the steps in the hint into a program.For example, "Sieve all integers from 2 to N" means "remaining" from 2 to N for a sieve, so I think I can write it as follows.

for(inti=2;i<=N;i++){
    furui[i] = 1;
}

Now furui[2], furui[3], ..., furui[N] is one, and it seems that the situation of remaining on the sieve is expressed.

No, wait a minute.To write like this, you must first define an array furui and initialize it.Since the subscript N is required, is the array length N+1?I have to write this.

Come to think of it, what is N?What I want now is a prime number up to 100, so I think I can express it without using N.

In addition, in C language, it is common to write processing in a function.As is often the case with intmain(){...}.

If you break down the processing written in natural language into small pieces and apply detailed concepts to each of them, you will be able to write the program.


2022-09-30 16:23

For now, let's prepare a place (array, etc.) where we put values from 2 to N.
(If there is an array of size N, it will fit all of them.)

Then, set the second to N value of the array to 1 (indicating that it has not yet been sifted by 1)

By making the number of multiples of n in the array zero, the multiples of n are dropped from the sieve.

Well, how about thinking like this?


2022-09-30 16:23

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.