0

I've been trying to solve this rather easy problem on SPOJ: http://www.spoj.com/problems/HS08PAUL/.

It requires the number of prime numbers (less than n) which can be expressed in the form x^2+y^4 (where x and y are integers) to be found out.

I've whipped up a brute force solution which takes up quite a while for (n ~= 1000000), resulting in a TLE (time limit exceeded) error being thrown by the engine. Here's the source code:

import java.io.*;
import java.util.*;

class HS08PAUL  {
   public static int[] sieve(int n){

        boolean[] prime = new boolean[n+1];
        int[] primeNumbers = new int[n];
        int index = 0;
        Arrays.fill(primeNumbers, 0);
        Arrays.fill(prime,true);

        prime[0] = false;
        prime[1] = false;
        int m = (int)Math.sqrt(n);
        for(int i = 2; i <= m; i++){
            if(prime[i])
            for(int k = i*i; k<=n; k+=i)
                prime[k] = false;

        }

        for(int j = 2; j <= n; j++) {
            if(prime[j]) {
                primeNumbers[index] = j;
                index++;
            }
        }
        return primeNumbers;
    }

    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);
        try{
            double numberOfTestCases = in.nextDouble();
            while(numberOfTestCases -- > 0) {
                int index = 0, y = 0, count = 0;
                int num = in.nextInt();
                int[] primes = sieve(num);
                while(index < num/3 ) {
                    for(y = 1; y < 57   ; y ++) {
                        if(Math.ceil(Math.sqrt(primes[index] - Math.pow(y,4))) == Math.floor(Math.sqrt(primes[index] - Math.pow(y,4)))) {
                                count++;
                                break;
                        }   

                    }
                    index++;
                }
                System.out.println(count);
            }   
        }
        catch(Exception e) {
        }
    }   
}   

Is there a way in which I can make this approach work?

P.S.:Please ignore the unruly exception handling.

Satyam Garg
  • 156
  • 9
AbhyudayaR
  • 51
  • 4

2 Answers2

0

I used bitset to get all the primes upto 10^7. Generate all the possible values of x^2 + y^4 upto 10^7, while generating each number I checked if it was prime then added it to a set , set was used to get unique numbers. Then used upper_bound to answer the queries. This is a bit overkill but I managed to get an accepted solution with 0.13s.

This is my accepted solution.

#include<bits/stdc++.h>
using namespace std;
#define int long long int
#define all(x) begin(x),end(x)
#define v vector
#define fast_io ios_base::sync_with_stdio(false);cin.tie(NULL)
const long long inf = 1e7;
bitset<inf>primes;
signed main() {
    fast_io;
    set<int>s;
    s.insert(2);
    primes.set();
    primes[0] = primes[1] = 0;

// Using sieve for getting all primes upto 10^7 
    for (int i = 2; i <= inf; i++) {
        if (primes[i]) {
            for (int j = i * i; j <= inf; j += i)primes[j] = 0;
        }
    }

// generating all the values x^2+y^4 upto 10^7
    for (int i = 0; i * i <= inf; i++)
        for (int j = 0; j * j * j * j <= inf; j++) {
            int value = i * i + j * j * j * j ;
            if (value <= inf and value > 1 and value & 1 and primes[value])s.insert(value);
        }
    v<int>a;
    for (auto i : s)a.push_back(i);
    s.clear();

// answering queries 
    int t; cin >> t;
    while (t--) {
        int n, ans = 0;  cin >> n;
        ans = distance(begin(a), upper_bound(all(a), n));
        cout << ans << '\n';
    }
}
-1

How many numbers of the form x^2+y^4 are there below 1000000? How many prime numbers are there below 1000000? What do these two numbers tell you about how you should approach the solution?

@isnot2bad's comment is also relevant.

rossum
  • 15,344
  • 1
  • 24
  • 38