Number of blind dates in evolutionary process of algorithm
Qiao is clumsy (welcome reprint, but please specify Source: Http://blog.csdn.net/qiaoruozhuo )
The topic comes from the programming forum "Wu Jian Fei" question: ask for 4 digits within the number of blind Date
2,500 years ago, the math master Pythagoras found that the following wonderful links exist between the 220 and the 2,842 numbers:
the sum of the true factors of 220 is 1+2+4+5+10+11+20+22+44+55+110=284
the sum of the true factors of 284 is 1+2+4+71+142=220
Pythagoras calls such a number pair, a, a blind date number: A's true factor (less than its own factor) and B, while the sum of the true factor of B is a.
Moderator Rjsp gave two exquisite algorithms, I have to organize, to make this article. For this article to provide inspiration for the Netizen expressed thanks.
#include <stdio.h> #include <stdlib.h> #include <math.h> #include <time.h>void amicablepair_1 ( int n);//The original method of finding a blind date void amicablepair_2 (int n);//Space Change time void amicablepair_3 (int n);//Use addition operation instead of redundancy operation, more efficient int main (void) {u nsigned n = 1000000; clock_t begin, end; Double cost; begin = Clock (); Amicablepair_1 (n);//The original method of finding the number of blind dates end = Clock (); Cost = (double) (end-begin)/clocks_per_sec; printf ("%lf seconds\n", cost); begin = Clock (); Amicablepair_2 (n);//The original method of finding the number of blind dates end = Clock (); Cost = (double) (end-begin)/clocks_per_sec; printf ("%lf seconds\n", cost); begin = Clock (); Amicablepair_3 (n);//The original method of finding the number of blind dates end = Clock (); Cost = (double) (end-begin)/clocks_per_sec; printf ("%lf seconds\n", cost); return 0;} void amicablepair_1 (int n)//The original method of finding a blind date {unsigned I, J, SA, Sb;for (i=2; i<=n; i++) {sa = 1; For (J=sqrt (i); j>1; j--)//calculate true factor of I and {if (i% J = = 0) sa + = j + I/J } if (sa <= i)//true sa>i to avoid repeated calculation continue; SB = 1; for (J=SQRT (SA); j>1 && sb<=i; j--)//calculate true factor of SA and {if (sa% J = = 0) sb + + J + sa/j; } if (sb = = i) printf ("%u\t%u\n", I, SA); }}void amicablepair_2 (int n)//With space change time {unsigned I, j;int *p = (unsigned*) malloc (sizeof (unsigned) * (n+1)), if (!p) {printf ( "Out of space!"); Exit (0);} for (i=2; i<=n; i++) {p[i] = 1; For (J=sqrt (i); j>1; j--)//calculates the true factor of I and is stored in p[i] {if (i% j = 0) P[i] + = j + i/j; if (P[i] > N) {p[i] = 0; Break }}} for (i=2; i<=n; i++) {if (p[p[i] = = I && i < p[i])//blind number to PRI NTF ("%u\t%u\n", I, p[i]); } free (p);} void amicablepair_3 (int n)//with addition operation instead of redundancy operation, more efficient {unsigned I, j, mid;int *p = (unsigned*) malloc (sizeof (unsigned) *(n+1)); if (!p) {printf ("Out of space!"); Exit (0);} MID = N/2; for (I=1; i<=mid; i++) {for (j=i*2; j<=n; j+=i)//Because J is a multiple of I, so the sum of I is J's true factor and {P[j] + = i; }} for (i=2; i<n; i++) {if (P[i] <= n && p[p[i]] = = I && i < p[i])//Blind Date Several pairs of printf ("%u\t%u\n", I, p[i]); } free (p);}
Number of blind dates in evolutionary process of algorithm