Simple and fun algorithm

Source: Internet
Author: User
Tags gcd greatest common divisor

The method of calculating the greatest common divisor by dividing
    publicstaticlonggcd(long a,long b){        long max=a>b?a:b;        long min=a>b?b:a;        if(max%min==0return min;        return gcd(min,max%min);    }

The theorem used is:
F (A, A, a, b) is the greatest common divisor of a and a, then F (b) =f (b,a%b).
Prove:
For A=b*q+r, there is a-b*q=r. If the greatest common divisor of A and B are C, then A and B must be divisible by C, then m*a±n*b must be divisible by C, so R can be divisible by C.

The method of finding the greatest common divisor of the more subtractive loss
    publicstaticlonggcd1(long a,long b){        ifreturn a;        long max=a>b?a:b;        long min=a>b?b:a;        return gcd1(min,max-min);    }

Compared to the Euclidean method, this method does not need to be compared to the subtraction of more time-consuming modulus operations, but this method is not stable, two number difference is very large or two number of hours (in fact, the difference is very small will change to a large difference), this method is degraded to O (n).
The principle of more subtractive loss method is not very understanding ...

Test the hardness of an egg with a floor

Give two eggs, there are 100 floors, test eggs in the first floor of the floor will be broken, if not broken, eggs can continue to throw. The optimal strategy, the number of times to throw in the worst case.
Maybe the problem description is not clear, can not understand Baidu a bit

     Public Static intGettimeinworstcase (intN) {//For the k layer, the egg is broken, from 1 to k-1 layer by layer to throw, this is k times, if the eggs are not broken, the n-k layer to continue before the operation. For each k, take both a larger value, and a minimum value for all K.         //map[n]= math.min (Math.max (k,map[n-k]+1)), k=1...n-1, eggs in the n layer will be broken, this is the topic set, so throw to the n-1 is enough. The egg may be broken on the 1 floor, so throw it from 1 onwards.         //map[1]=1        int[]Map=New int[n+1];Map[1]=1; for(intI=2; i<=n;i++) {intCurmin=integer.max_value; for(intj=1; j<i;j++) {intTemp=math.max (J,Map[i-j]+1);if(temp<curmin) Curmin=temp; }Map[I]=curmin; }return Map[n]; }

can also expand to a lot of egg situation.
For two eggs, there is a method that does not have a dynamic plan, but does not understand why this can be done.
Divided into several paragraphs, if at the first point A is broken, from 1 to A-1, altogether a, if the first point is not broken, in the second point B throw, b broken, from the a+1 to b-1 throw; This is to ensure that the length of the paragraph is b-a=a-1 after a number of paragraphs and so on.
So for n-tier, n<=a+ (A-1) + (a-2) + ... 1, when n=100, a>=14, result 14

Fast Power
    publicstaticintmyPow(intint b){        int res=1,base=a;        while(b!=0){            if((b&1)!=0){                res*=base;            }            base*=base;            b=b>>1;        }        return res;    }

Principle:
2^11= (2^ (2^0)) * (2^ (2^1)) * (2^ (2^3))
When bits is multiplied by 1 o'clock, it is 0 times base.

Simple and fun algorithm

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.