Project Euler #135: Same differences

Sort by

recency

|

11 Discussions

|

  • + 0 comments

    Here's my C++ solution:

    int main() {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT */   
        unsigned T; scanf("%u", &T);
        
        // x, y, z
        // z, y = z + d, x = z + 2d
        // z^2 + 4dz + 4d^2 - z^2 - 2dz - d^2 - z^2 = n
        // -z^2 + 2dz + 3d^2 = n
        // z^2 - 2dz - 3d^2 + n = 0
        // (z - d)^2 - 4d^2 + n = 0
        // (z - d - 2d)(z - d + 2d) = -n
        // (z - 3d)(z + d) = -n
        // d1*d2 = n
        // z - 3d = -d1
        // z + d = d2
        // 4d = d2 + d1
        // z = d2 - d
        // and d2 >= (d2 + d1)/4
        // 4d2 >= d2 + d1
        // 3d2 > d1
        
        unsigned N = 8e6;
        std::vector<unsigned> sieve(N + 1);
        
        for(unsigned i = 1; i <= N; ++i)
            for(unsigned j = i; j <= N; j += i) {
                if((j/i + i) % 4 == 0 and 3*(j / i) > i)
                    ++sieve[j];
            }
        
        
        for(unsigned i = 0; i < T; ++i) {
            unsigned n; scanf("%u", &n);
            printf("%u\n", sieve[n]);
        }
        
        return 0;
    
  • + 0 comments

    100/- Points C++

    #include <iostream>
    #include <vector>
    
    //#define ORIGINAL
    
    int main()
    {
    #ifdef ORIGINAL
      unsigned int limit = 1000000; // "less than one million"
    #else
      unsigned int limit = 8000001; // up to 8 million (inclusive)
    #endif
    
      // precompute solutions
      std::vector<unsigned int> solutions(limit, 0);
      for (unsigned int a = 1; a < limit; a++)
        for (auto b = (a + 3) / 4; b < a; b++)
        {
          auto current = a * (4*b - a);
          if (current >= limit)
            break;
    
          solutions[current]++;
        }
    
    #ifdef ORIGINAL
      // count all with exactly 10 solutions
      unsigned int count = 0;
      for (auto s : solutions)
        if (s == 10)
          count++;
      std::cout << count << std::endl;
    
    #else
    
      // look up number of solutions
      unsigned int tests;
      std::cin >> tests;
      while (tests--)
      {
        unsigned int pos;
        std::cin >> pos;
        std::cout << solutions[pos] << std::endl;
      }
    #endif
    
      return 0;
    }
    
  • + 0 comments

    For last 5 test case, common method,to iterate from 1 to sqrt(n) to fetch all divisors, would fail with time out. Thus, we need an efficient way to find all divisors. Here's my method, hope this helps.

    1. calculate all primes less than sqrt(8000000) +1
    2. find all prime divisors and how many times each prime divisor shows up for n.
    3. With the result in step 2, we can find all divisors less than sqrt(n) + 1 for n,
    4. Iterate divisors we got from step 3, calculate how many solutions for n.
  • + 0 comments

    Very easy mathematical solution: check the delta on two variable equations. There are solutions where delta is positive or zero, and there are integer solutions where delta is a perfect square of an integer. then simply calculate the solutions and be careful not to count the same solution twice by mistake. Unfortunately I am getting timeout at the last 5 tests even tho this is the fastest I have tried :/

    int solutions = 0;
            for(long double i = (sqrt(n/4)-fmod(sqrt(n/4),1)); i<=(n/4)+(1-fmod(n/4,1));i++)
            {
                if(is_perfect_square(4*i*i-n)){
                    long double x1 = 2*i -sqrt(4*i*i-n);//solution one
                    long double x2 = sqrt(4*i*i-n) +2*i;//solution two
                    
                    //solutions have to be bigger than the difference orelse Xs go negative
                    if(x1 > i){solutions++;}
                    if(x2 > i){solutions++;}
                    if(x1 == x2){solutions--;}//remove inserted duplicate solution where x1 = x2
                }       
            }
    
  • + 0 comments
    hello everyone, i am getting timeout on last 5 test cases. any pointers will be appreciated