Calculation of common algorithms between 1-100

Source: Internet
Author: User

Calculates all the odd and 1-100 between
Class Program

{

static void Main (string[] args)

{

int sum= getodd (1,100);

Console.WriteLine ("1-100 all odd integers and {0}", sum);

Console.readkey ();

}

<summary>

Odd and

</summary>

<param name= "P1" ></param>

<param name= "P2" ></param>

<returns></returns>

private static int getodd (int p1, int p2)

{

int sum = 0;

for (int i = p1; I <= p2; i++)

{

if (i%2!=0)

{

sum + = i;

}

}

return sum;

}

}


For all prime numbers (primes) between 1-100 and:
So first you have to know what a prime number is?
A number is not a prime number "prime" refers to the addition can be 1 and its own whole except, can not be divisible by any other integer natural numbers.
And then know which ones are prime numbers:
Methods of Memory:
100 within the prime number memory method
There are 25 prime numbers within 100, which we often use, and can be remembered in the following two ways.
First, the law of Memory method
First remember 2 and 3, and the product of 2 and 32 prime numbers is 6. Prime numbers within 100 are generally in the front and back positions of multiples of 6. such as 5, 7, 11, 13, 19, 23, 29, 31, 37, 41, 43 ... Only 25, 35, 49, 55, 65, 77, 85, 91, 95 this number of multiples of several 6 is not prime, and these numbers are multiples of 5 or 7. It can be concluded that: 100 within 6 of a multiple before and after the position of two, as long as not a multiple of 5 or 7, it must be a prime number. Based on this feature, you can remember prime numbers within 100.
Ii. Classification and memory method
We can divide the prime numbers within 100 into five categories of memory.
First Category: prime numbers within 20, a total of 8:2, 3, 5, 7, 11, 13, 17, 19.
The second category: single digit is 3 or 9, 10 digits 3 of the prime number, a total of 6:23, 29, 53, 59, 83, 89.
The third category: single digit is 1 or 7, 10 digits 3 of the prime number, a total of 4:31, 37, 61, 67.
Class Fourth: Single digit is 1, 3 or 7, 10 digits 3 of the prime number, a total of 5:41, 43, 47, 71, 73.
Class Fifth: There are also 2 holding numbers of 79 and 97.
Prime numbers between 1-100 and: 1060class program

{

private static void Main (string[] args)

{

int sum = getsumprime (1, 100);

Console.WriteLine ("Prime number between 1-100 and is {0}", sum);

Console.readkey ();

}

<summary>

Seeking prime numbers and

</summary>

<param name= "P1" ></param>

<param name= "P2" ></param>

<returns></returns>

private static int getsumprime (int p1,int p2)

{

int sum = 0;

for (int i = p1; I <= p2; i++)

{

if (IsNo (i))

{

sum + = i;

}

}

return sum;

}

<summary>

Determine if it is a prime number

</summary>

<param name= "Number" ></param>

<returns></returns>

private static bool IsNo (int number)

{

if (number<2)

{

return false;

}

for (int i = 2; i < number; i++)

{

if (number%i = = 0)

{

return false;

}

}

return true;

}

}

Calculation of common algorithms between 1-100

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.