Project Euler 95:amicable chains affinity number Chain

Source: Internet
Author: User

Amicable chains

The proper divisors of a number is all the divisors excluding the number itself. For example, the proper divisors of 1, 2, 4, 7, and 14. As the sum of these divisors is equal to +, we call it a perfect number.

Interestingly the sum of the proper divisors of are 284 and the sum of the proper divisors of 284 is, forming a cha In of numbers. For this reason, 284 is called an amicable pair.

Perhaps less well known is longer chains. For example, starting with 12496, we form a chain of five numbers:

12496→14288→15472→14536→14264 (→12496→ ...)

Since this chain returns to its starting point, it's called an amicable chain.

Find the smallest member of the longest amicable chain with no element exceeding one million.

Affinity number Chain

A factor other than itself is called the true factor. For example, the true factor of 28 is 1, 2, 4, 7, and 14. These true factors are exactly 28, so we call 28 a complete number.

Interestingly, the sum of 220 of the true factor is 284, while the sum of 284 of the true factor is 220, constituting a chain of length 2, which we also call affinity number pairs.

There are some longer sequences that are not very well known. For example, starting from 12496, you can form a chain of length 5:

12496→14288→15472→14536→14264 (→12496→ ...)

Since this chain is finally back to the starting point, we call it the affinity number chain.

Find the longest bar in the affinity chain with no more than 1 million of all elements, and give the smallest number of them.

Solving

Violence should be possible, intuition is simple. If the true factor is calculated for each number and then the true factor and the real factor, then the time complexity is good, indicating that there is no run-out result. And then find here the calculation of the true factor and the number of repetitions in the process is many. So you can first find the number of true factors and, this is an array, the following question is the first longest chain in the array to find the beginning of the value, here began to not understand, thought to be the longest chain in many of the minimum value of the one, but found that the longest chain of the minimum value is its first value. There is also said to be asked the first longest chain, so the main length of less than 1 million of the longest chain is 28 have a good number of.

So how do qiuqin and chains?

Attention:

1. Not every number has affinity number chain, as this link said is also more interesting

Problem or how to find the chain?

It is clear that we have found that the chain starting from a ends with a, which is ordered.

It can be found that the affinity number chain will not appear when the ring, this is obvious, but also the key to solve the problem

So when we find the ring, it's over.

It then determines whether the value at the end is equal to the original starting value, and the same length is obtained.

Java

 PackageLevel3;ImportJava.util.TreeSet; Public classpe094{ Public Static voidrun2 () {intMAX = 1000000; int[] Next =New int[MAX]; //true-Factor and         for(inti=2;i<max;i++) Next[i]= 1;  for(inti=2;i<max/2+1;i++)             for(intj=2*i;j<max;j+=i) {Next[j]+=i; }        intLongest =-1; intSubmin =Integer.max_value; intMIN =Integer.max_value; intJ =2; TreeSet<Integer> set =NewTreeset<integer>();  for(inti=2+8;i<max;i++) {set.clear (); J=i; //Record Minimum valueSubmin =J;            Set.add (j);  while(true) {J=Next[j]; if(J>=max) Break; //Record Minimum valueSubmin = submin>j?j:submin; //end of Loop appears                if(Set.contains (j) = =true)                     Break;            Set.add (j); }            if(j==i) {                //This is not good, it was updated at the same length//longest = Math.max (Set.size (), longest); //The one with the smallest record length only records the smallest at the same length .                if(Set.size () >longest) {Longest=set.size (); MIN=submin; System.out.println (Longest+ "+min +" +j); }}} System.out.println (Longest+"   "+MIN); }     Public Static voidMain (string[] args) {LongT0 =System.currenttimemillis ();        Run2 (); LongT1 =System.currenttimemillis (); Longt = T1-t0; System.out.println ("Running Time=" +t/1000+ "s" +t%1000+ "MS"); }}

Results

1  282  2205   12496  1249628   14316   1431628 14316 Running Time =2s415ms

The first number is the length of the chain, the minimum value of the chain, the first number of the chain

Actually is more, the topic asks is the first longest chain, the direct view topic is easy to misunderstand.

In the above and in the calculation of the true factor, and the selection method to find the prime number of the idea is very similar to know the law of numbers, to solve.

Python is found directly in the forum

#CODING=GBKImport time as timedefrun (): MAX= 1000000D= [1]*MAX forIinchXrange (2,MAX//2):         forJinchXrange (i,max,i): D[j]+=I max_cl=0 forIinchXrange (2, MAX): N,chain=i,[] whiled[n]<Max:d[n],n= Max+1, D[n]Try: k=chain.index (n)exceptValueError:chain.append (n)Else:                ifLen (chain[k:]) >Max_cl:max_cl,min_link=Len (chain[k:]), Min (chain[k:])PrintMin_link t0=Time.time () run () T1=time.time ()Print "running Time=", (T1-T0),"s"

14316
Running Time= 5.47899985313 S

Project Euler 95:amicable chains affinity number Chain

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.