Summary of Mathematical thinking functions in program design (code in C # for example)

Source: Internet
Author: User

In the recent case of C #, we have learned the basics of programming, which involves some mathematical thinking, and we can skillfully convert these logic problems into code and give them to computer operations.

Now will often use the basic function to do a summary, for everyone to share, their own backup.

1. Determine if a number is odd

Definition: Integers, numbers that can be divisible by 2 are even, numbers that cannot be divisible by 2 are odd

Point of thinking: N%2!=0 is an odd number

        /// <summary>        ///determine if an integer is odd/// </summary>        /// <param name= "n" >the integer to Judge</param>        /// <returns>is odd</returns>        Static BOOLIsOdd (intN) {//cannot be divisible by 2, it is an odd number            if(n%2!=0)            {                return true; }            //Otherwise it is not an odd number            Else            {                return false; }        }

2. Determine whether a number is prime

Definition: In a natural number greater than 1, there are no other factors other than 1 and itself, and such numbers are called prime numbers.

Method One: Lookup method

Idea point: Set the loop, I is greater than or equal to 2 and less than N, if there is a number of n%i==0, it is not prime

        /// <summary>        ///determine if an integer is prime/// </summary>        /// <param name= "n" >natural number greater than 1</param>        /// <returns>whether Prime number</returns>        Static BOOLIsPrime (intN) {//in a range greater than or equal to 2 to less than N, look for the existence of a number that can be divisible by n             for(inti =2; I < n; i++)            {                //returns False if there is a number that can be divisible by n                 if(n%i==0)                 {                     return false; }            }            //returns True if the loop ends and no number is found that can be divisible by n             return true; }

Method Two: Counting method

Point of thinking: Set the loop, I is greater than or equal to 2 and less than N, if there is a number of n%i==0, then count; After the loop ends, the count is greater than 0, not prime

        /// <summary>        ///determine if an integer is prime/// </summary>        /// <param name= "n" >natural number greater than 1</param>        /// <returns>whether Prime number</returns>        Static BOOLIsPrime (intN) {//set the variable sum to hold the number of integers that can be n            intsum =0; //in a range greater than or equal to 2 to less than N, look for the existence of a number that can be divisible by n             for(inti =2; I < n; i++)            {                //sum is increased by 1 if there is a number that can be divisible by n                 if(n%i==0) {sum++; }            }            //The loop ends, if sum is greater than 0, is not a prime, returns false; otherwise a prime number, returns True            if(Sum >0)            {                return false; }            Else            {                return true; }        }

3. Determine if a number is a self-power number

Definition: An n-bit (n≥3), where the sum of the n power of the number on each bit is equal to itself (for example: 1^3 + 5^3+ 3^3 = 153), and a 3-digit 3 power number is called Narcissus number.

Point of thinking: A. Convert the positive integer to be judged to a string type, to get the number of bits Len;

B. The positive integer%10 to get a single digit,/10%10 get 10 bits,/100%10 get the Hundred, and so on, until/10^ (len-1)%10 get the largest bit;

C. Add the number of Len powers to determine if it is equal to a positive integer and equal to the self-power number.

        /// <summary>        ///determine whether a number is a narcissus number/// </summary>        /// <param name= "n" >positive integers with a number of digits greater than or equal to 3</param>        /// <returns>whether the number of daffodils</returns>        Static BOOLIsnarcissisticnumber (intN) {//define Len, the number of digits assigned to n            intLen =(N.tostring ()).            Count (); //define power, assign a value of 10 (len-1) to power            intPower =1;  for(inti =1; i < Len; i++) {Power*=Ten; }            //defines sum, which is assigned to the sums of Len powers of the number of Members            intsum =0;  for(intj =1; J <= Power; j*=Ten)            {                //define multiple, and assign values to the Len power of the number of Members                intmultiple =1;  for(inti =1; I <=len; i++) {Multiple*=n/j%Ten; } Sum+=multiple; }            //if sum equals N, it is the number of daffodils, return turn; otherwise, return false            if(n = =sum) {                return true; }            Else            {                return false; }        }

4. Reverse-Order output

Definition: Export demand from right to left in left-to-right output

Point of thinking: think of a string as an array of char, set a loop, start output from the last element, until the first element is output

        /// <summary>        ///Reverse order Output/// </summary>        /// <param name= "str" >a string that needs to be reversed output</param>        Static voidAntitone (stringstr) {             for(inti = str. length-1; I >=0; i--) {Console.Write (str[i]); }        }

5. Fibonacci Sequence

Definition: The Fibonacci sequence is defined as a recursive method: F (0) =1,f (1) =1, f (n) =f (n-1) +f (n-2)

Point of thinking: function calls itself for operation

        /// <summary>        ///find the number of nth bits of the Fibonacci sequence/// </summary>        /// <param name= "str" >Number of digits</param>        /// <return>number of nth bits of the Fibonacci sequence</return>>        Static intFibonaccisequence (intN) {if(n = =1|| n = =2)            {                return 1; }            Else            {                returnFibonaccisequence (N-1) + fibonaccisequence (N-2); }        }

6. Factorial

Definition: The factorial of a positive integer, which is the product of all positive integers less than and equal to the number, and the factorial of 0 is 1. Factorial writing n! of natural number n

Point of thinking: function calls itself for operation

        /// <summary>        ///calculate factorial/// </summary>        /// <param name= "str" >positive integers</param>        /// <return>factorial Results</return>>        Static intFactorial (intN) {if(n = =1)            {                return 1; }            Else            {                returnN*factorial (N-1); }        }

Summary of Mathematical thinking functions in program design (code in C # for example)

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.