The realization of the Eratosthenes algorithm for the determination of prime number

Source: Internet
Author: User

#include "stdafx.h"
#include <stdio.h>
#include <iostream>
using namespace Std;
#define MAXNUM 1000//For all primes within 1000

int main ()
{
int I, j, C = 0;
int prime[maxnum+1];//defines an array to hold prime numbers
for (i = 2; I <= maxnum; i++)//Initialize array
{
Prime[i] = 1;//flag is 1 primes
}
for (i = 2; i*i <= maxnum; i++)//cycle processing before I
{
if (prime[i] = = 1)//If it is a prime number, filter
{
for (j = 2 * i; J <= Maxnum; j + +)
{
if (prime[j]==0)//Remove composite
{
Continue
}

if (j%i = = 0)//If it is not a prime number, it is composite, the flag of the prime number is changed to 0
{
PRIME[J] = 0;
}
}
}
}
for (i = 2; i < maxnum; i++)//value and number of output primes
{
if (prime[i] = = 1)
{
printf ("%4d", I);
C + +;
if (c% 10 = = 0)
printf ("\ n");
}
}
printf ("\ n A total of%d prime numbers", c);
System ("pause");//means pause
return 0;
}

The realization of the Eratosthenes algorithm for the determination of prime number

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.