A small program that obtains all prime numbers of less than 100 according to the 'distinct' Method

Source: Internet
Author: User
// Obtain all prime numbers of less than 100 according to the growth method. The so-called growth method is used to screen all multiples of prime numbers from small to large,
// For example, we can sieve 4, 6, 8,... 98,100 and so on according to 2. Then we can sieve 9, 15,... 99 and so on according to 3 (note 6 at this time,
// The number such as 12 has long been screened.) Because 4 has been screened, the next prime number used for filtering is 5... and so on.
// It is a prime number within 100.
/*
* Auther starshus
*
* Date 04/11/20
*/
// 6.7.4
Public class prime
{
Private Static final int max = 100;
Public static void Init (INT [] num) // defines the array, which is loaded from 1 to 100
{
Int I = 0;
For (; I <Max; I ++)
{
Num [I] = I + 1;
}
}
Public static int count (INT [] num) // after the program runs, calculate the number of prime numbers
{
Int n = 0, I = 0;
For (; I <Max; I ++)
{
If (Num [I]! = 0) // The non-prime number is set to zero.
N ++;
}
Return N;
}
Public static void printf (INT [] num) // output all prime numbers, every 10 transposed rows
{
Int I = 0, n = 0;
For (; I <Max; I ++)
If (Num [I]! = 0)
{
System. Out. Print (Num [I] + "");
N ++;
If (N % 10 = 0)
System. Out. println ();
}
}
Public static void main (string [] ARGs) // master Method
{
Int I = 2, j = 1;
Int count;
Int [] numbers = new int [Max];
Init (numbers );
While (j <max)
{
If (numbers [J]! = 0)
While (I <max)
{
If (numbers [I]! = 0)
{
If (numbers [I] % numbers [J] = 0)
Numbers [I] = 0; // if it is not a prime number, set it to zero.
}
I ++;
}
J ++;
I = J + 1;
}
Count = count (numbers );
Printf (numbers );
System. Out. println ();
System. Out. println ("OK, we found" + Count + "primes in" + MAX + "numbers .");
}
}

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.