Use the Eratost filter to print out prime numbers smaller than n in the C language

Updated on technology 2024-05-04
17 answers
  1. Anonymous users2024-02-09

    Don't mislead people upstairs, do you call it a screening method?

    Let's first explain the steps of the screening method:

    1> Dig out the 1 first (because 1 is not prime).

    2> Use 2 to remove the numbers behind it, and dig out the numbers that are divisible by 2, i.e., dig out multiples of 2.

    3> Use 3 to remove the number behind it and dig out the multiple of 3.

    4> Use ....The numbers are used as divisors to remove the numbers from these numbers.

    The above operation requires a large container to load the set of all numbers, as long as the above conditions are met, i.e., all to the nth power of 2 to 0, 3 to the nth power to 0, and 4 to the nth power to 0. All the way to the end of the data set, so that the number that is not 0 is the prime number, and then press the mark to find it inside.

    The screening procedure is as follows.

    #include

    int main()

    scanf("%d",&n);

    while(n != 0)

    return 0;

    If you don't understand this method, you can use the regular algorithms they wrote above, but if the numbers are too large, the calculation is very slow.

  2. Anonymous users2024-02-08

    Give, it's not as complicated as the above, and the following ** has been compiled and run to confirm:

    #include

    int prime(int n) filter to determine whether it is a prime number int main().

    return 0;}

  3. Anonymous users2024-02-07

    Let me write it in a simple and easy-to-understand way:

    #include

    #include

    int fun(int n)

    return 1;

    void main()

    while(num!=0);}

  4. Anonymous users2024-02-06

    If defined as a[100].

    Then the subscript range for that array is:

    a[0] ~a[99]

    In order to use a[100], we have to define the sentence 101 to execute the following 100 lines**:

    a[1]=1;

    a[2]=2;

    a[3]=3;

    a[100]=100;

    Written in a circular statement, it is:

    for(i=0;i<101;i++)

    a[i]=i;

    Whenever the output goes to the 10th, 20th ......90 is a line break and 10, 20 ......90 These numbers have one thing in common:

    They % 10 ==0

    So it can be like this:

    for (i=1 ; i<101 ;i++)a[i] is not a variable.

    When i take 1, 2, 3 ...... respectively,100

    A[i] means a[1], a[2], a[3] ,...a[100] These 100 variables.

  5. Anonymous users2024-02-05

    Question 1can be defined as a[100] then change to for(i=0; i<=100;i++) to assign a value to the array first.

    The aim is to get a [100].

    Question 2 for(i=0;i<101;i++)a[i]=i;

    This is the problem of assigning a value to the array a[100] in the following procedure3I changed it for you, and I added a statement, and it solved it, and you can see include

    #include

    void main()

    if(a[j]!=0)

    if(count%10==0) printf("");}

  6. Anonymous users2024-02-04

    Questions 1 and 2 are one and the same. Answer: The program finds the prime number in the integer of the interval [2,100], and it directly represents the number with a subscript, such as a[2]=2, which is for convenience and nothing supernatural.

    But since the interval contains 100, and the subscript of the array in C starts at 0, if defined.

    a[100] cannot take a[100]=100 because a[100] is the 101st element (a[0]=0 is the first element).

    count++ after outputting a prime number each time;

    if (count%10==0)

    printf("");Full 10 elements are broken as follows:

    #include

    #include

    void main()

    if(a[j]!=0)

    if(count%10==0) can actually be written directly if(!).(count%10))

    printf("");}

  7. Anonymous users2024-02-03

    Your program requires prime numbers between 0-100, right?

    Problem 1: If you want to store a total of 101 numbers between 0 and 100, you need to use a[101], if it is a[100], then store 100 numbers.

    Problem 2: for(i=0; i<101;i++)

    a[i]=i;i from 0--100, a[0]--a[100] is complex to the corresponding 0--100;

    Make the first changes to your program as follows:

    #include

    #include

    void main()

    for(j=2;j<101;j++)if(a[j]!=0) output prime number} }

  8. Anonymous users2024-02-02

    A prime number. The number of times that a natural number greater than 1 is not divisible by any other natural number except 1 and 100 integers themselves. Prime numbers play an important role in number theory.

    Numbers greater than 1 but without prime channels are called composite numbers. 1 and 0 are neither prime nor composite.

    The source of prime numbers within 100 obtained by filtering is as follows:

    include"stdio.h"

    main()

    main()

    inti,j。

    For (i = 2; I'm < 99; I++

    For (j = 2; I++j +

    if(i%j==0)

    Break; If (j i 1).

    printf(“%4d”,i);

  9. Anonymous users2024-02-01

    main(),a[200]=;

    for(i=1;i<200;i++)a[i]=i+1;*Put all candidate numbers into a sieve*

    for(i=0;i<200;i++)

    for(j=0;j<200;j++)

    if(a[i]!=0)

    i=0;printf("primes:");

    while(p[i]!=0) *Print out all primes* }

  10. Anonymous users2024-01-31

    First of all, you don't have an initial value.

    if(a[j]%k==0&&j!=2) ==> if(j%k==0&&j!=2)

    Also, you can't output the prime number 2 in this program.

    It is possible to output 2 before looping

    I'm also just learning C

  11. Anonymous users2024-01-30

    #include

    A function that determines whether it is a prime number or not*

    int isprime (int a);

    int main()

    int isprime (int a)

    If there are no numbers that are divisible, it means that it is a prime number, and returns 1* if (q ==0).

    return 1;

    Otherwise, 0* is returned

    elsereturn 0;

  12. Anonymous users2024-01-29

    The ** upstairs is a screening method.。。。

    The overall idea is to start with 2 and sift out all the multiples of prime numbers.

    #include

    #define size 201

    int main( )

    for ( i = 100; i < size; i++if ( isprime[ i ] 0 )printf("%d ",i);

    return 0;

  13. Anonymous users2024-01-28

    It is easy to select all the prime numbers between 3 and 200, and finally output only those greater than 100. Here are some examples:

    #include ""//if the vc++,with this line.

    #include ""

    int main(void)

  14. Anonymous users2024-01-27

    I've written it once, and I don't want to write it again.

    Let's look in the know.

  15. Anonymous users2024-01-26

    I guess this is homework, and I have to write my own homework.

    Let me give you some ideas.

    For example, integer numbers n, m

    m is to the open 2nd power of n, and as long as n is not divisible by every integer (excluding 1) less than m, n is prime.

  16. Anonymous users2024-01-25

    #include

    int main()

    loop:int a,i=2;

    printf("Enter any integer:");

    scanf("%d",&a);

    if(a==1)

    printf("%d is neither prime nor composite. ",a);

    elsefor(;a%i!=0;i++)

    if(iprintf("%d is a composite number. ",a);

    elseprintf("%d is a prime number. ",a);

    goto loop;

  17. Anonymous users2024-01-24

    That's right, I've run it, and "25,35,55,65,85,95 these numbers are not culled" doesn't exist.

    if(a[i]==0) break; change to if(a[i]==0) ontinue; This little problem can be corrected, and the result is correct.

Related questions
7 answers2024-05-04

Diego Velazquez was a Spanish Baroque painter and a master of Spanish painting with aristocratic majesty. He is famous for both genre painting and portrait country, and the main reason for his reputation as a master is not his ability to paint subjects, but his revolutionary contribution to the history of European painting. This contribution is manifested in his understanding of the nature of painting as a visual art, in his recognition that the truth in painting is only the effect of light and color that we see with our eyes, and for this reason he rejects the focus on the volume and tactile nature of objects in painting, and does not pay attention to intellectual factors such as line drawing and linear perspective that distinguish the boundaries and distances of objects (which are characteristic of classical oil painting before Velázquez), and only emphasizes the visual properties of painting to our eyes. >>>More

6 answers2024-05-04

Mainly this sentence.

After years of research on these rock paintings, it has been found that the use of tools and materials by the authors of the rock paintings at that time has reached a very high level. For example, powder pigments are mixed and blended with greases for use; Some of the powder pigments are blown onto the rock face with bone tubes. In terms of painting skills, the artistic skills are even more extraordinary. >>>More

7 answers2024-05-04

The basic form of the Slatsky equation:

Let x(p,w) be the Walrthian demand, and u * be the level of effect that the consumer achieves under the premise of **p and income w, then. >>>More

20 answers2024-05-04

Let's use Pyrukida, it's better to practice it a little more, and use two tricks of Rey, and that's fine. Two charges, coupled with steel flying t, it is easy to make a fatal one, and you can make the progression high and you can also second Akxia, you can try.

14 answers2024-05-04

Pony's True Strategy):

1. Pony is not affected by the decline in status. >>>More