IamLupo

Euler Project - Problem 73

Apr 27th, 2016
377
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.00 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <math.h>
  5. #include <string.h>
  6. #include <stdlib.h>
  7.  
  8. using namespace std;
  9.  
  10. /*
  11.     How many fractions lie between 1/3 and 1/2 in the sorted set of
  12.     reduced proper fractions for d ≤ 12,000?
  13. */
  14.  
  15. vector<vector<int>> dividers(int l) {
  16.     int i, j;
  17.     vector<vector<int>> r;
  18.    
  19.     //zero has no dividers
  20.     r.push_back({0});
  21.    
  22.     //All the others have 1 as divider
  23.     for(i = 1; i <= l; i++)
  24.         r.push_back({1});
  25.    
  26.     //Find the other dividers
  27.     for(i = 2; i <= l / 2; i++)
  28.         for(j = 2; i * j <= l; j++)
  29.             r[i * j].push_back(i);
  30.    
  31.     return r;
  32. }
  33.  
  34. /*
  35.     Data:
  36.         1, -1, -1, 0, -1, 1, -1, 0, 0, 1, -1, 0, -1, 1, 1, 0, -1, 0,
  37.         -1, 0, 1, 1, -1, 0, 0, 1, 0, 0, -1, -1, -1, 0, 1, 1, 1, 0, -1,
  38.         1, 1, 0, -1, -1, -1, 0, 0, 1, -1, 0, 0, 0, 1, 0, -1, 0, 1, 0,
  39.  
  40.     Property:
  41.         μ(n) = 1 if n is a square-free positive integer with an even number of prime factors.
  42.         μ(n) = −1 if n is a square-free positive integer with an odd number of prime factors.
  43.         μ(n) = 0 if n has a squared prime factor.
  44.    
  45.     Reference:
  46.         https://oeis.org/A008683
  47.         https://en.wikipedia.org/wiki/M%C3%B6bius_function
  48. */
  49. vector<int> Mobius(long long n) {
  50.     int i, j;
  51.     vector<int> r(n + 1, 0);
  52.    
  53.     r[1] = 1;
  54.  
  55.     for(i = 1; i <= n; i++)
  56.         for(j = 2 * i; j <= n; j += i)
  57.             r[j] -= r[i];
  58.    
  59.     return r;
  60. }
  61.  
  62. /*
  63.     Data:
  64.         1, 0, 1, 1, 1, 1, 2, 1, 2, 2, 2, 2, 3, 2, 3, 3, 3, 3, 4, 3, 4, 4,
  65.         4, 4, 5, 4, 5, 5, 5, 5, 6, 5, 6, 6, 6, 6, 7, 6, 7, 7, 7, 7, 8, 7,
  66.         8, 8, 8, 8, 9, 8, 9, 9, 9, 9, 10, 9, 10, 10, 10, 10,
  67.    
  68.     Formula:
  69.         a(n) = floor(n/6)+1 unless n=1(mod6); if n=1(mod6), a(n) = floor(n/6)
  70.    
  71.         Bob Selcoe, Sep 27 2014
  72.    
  73.     Reference:
  74.         https://oeis.org/A103221
  75. */
  76. long long A103221(long long n) {
  77.     if(n % 6 == 1)
  78.         return n / 6;
  79.     else
  80.         return (n / 6) + 1;
  81. }
  82.  
  83. /*
  84.     Data:
  85.         0,  1,  0,  0,  1,  0,  1,  1,  1,  0,  2,  1,  2,  1,  1,  1,  3,  1,
  86.         3,  2,  2,  1,  4,  1,  3,  2,  3,  2,  5,  2,  5,  3,  3,  2,  4,  2,
  87.         6,  3,  4   2,  7,  2,  7,  4,  4,  3,  8,  3,  7,  4,  5,  4,  9,  3,
  88.         6,  4,  6,  4,  10, 2,  10, 5,  6,  5,  8,  4,  11, 6,  7,  4,  12, 4,
  89.         12, 6,  7,  6,  10, 4,  13, 6,  9,  6,  14, 4,  10, 7,  9,  6,  15, 4,
  90.         12, 8,  10, 7,  12, 5,  16, 7
  91.    
  92.     Formula:
  93.         SUM_{d|n} mu(d) * A103221(n/d), where mu is Mobius function (A008683).
  94.    
  95.         Andrew Baxter, Jun 06 2008
  96.    
  97.     Reference:
  98.         https://oeis.org/A128115
  99.         https://en.wikipedia.org/wiki/M%C3%B6bius_inversion_formula
  100. */
  101. long long A128115(long long n, vector<int> &mu, vector<int> &div) {
  102.     int i;
  103.     long long r;
  104.    
  105.     //Init
  106.     r = 0;
  107.    
  108.     if(n == 3)
  109.         return 0;
  110.    
  111.     for(i = 0; i < div.size(); i++)
  112.         r += mu[div[i]] * A103221(n / div[i]);
  113.    
  114.     return r;
  115. }
  116.  
  117. long long countProperFractions(int l) {
  118.     int i;
  119.     long long r;
  120.     vector<int> mu;
  121.     vector<vector<int>> div;
  122.    
  123.     //Init
  124.     r = 0;
  125.     div = dividers(l);
  126.     mu = Mobius(l);
  127.    
  128.     for(i = 3; i <= l; i++)
  129.         r += A128115(i, mu, div[i]);
  130.    
  131.     return r;
  132. }
  133.  
  134. int main() {
  135.     cout << "result = " << countProperFractions(12000) << endl;
  136.    
  137.     return 0;
  138. }
Advertisement
Add Comment
Please, Sign In to add comment