Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <math.h>
- #include <stdio.h>
- #include <stdlib.h>
- typedef long long ll;
- /* Calculates gcd(a,b): the greatest common divisor of a and b */
- static ll gcd(ll a, ll b)
- {
- while (b)
- {
- ll tmp = a%b;
- a = b;
- b = tmp;
- }
- return a;
- }
- static ll *memo;
- /* Calculates number of ordered pairs (a,b) such that gcd(a,b) == 1 */
- static ll calc2(ll a, ll lim)
- {
- ll *m, b;
- m = &memo[a];
- if (*m == 0)
- {
- for (b = 0; b < lim; ++b)
- {
- if (gcd(a, b) == 1) ++*m;
- }
- }
- return *m;
- }
- /* Calculates number of ordered triples (a,b,c) such that gcd(a,b,c) == 1 */
- static ll calc(ll lim)
- {
- ll res = 0, a, b;
- memo = calloc(lim, sizeof(ll));
- memo[0] = 1;
- memo[1] = lim;
- for (a = 0; a < lim; ++a)
- {
- res += calc2(a, lim);
- for (b = 0; b < a; ++b)
- {
- res += 2*calc2(gcd(a, b), lim);
- }
- }
- free(memo);
- return res;
- }
- ll main()
- {
- ll depth;
- for (depth = 1; depth <= 16; ++depth)
- {
- ll lim = 1 << depth;
- ll cnt = calc(lim);
- ll tot = lim*lim*lim;
- printf("%2lld: %15lld / %15lld = %.9f\n", depth, cnt, tot, (double)cnt/tot);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment