1

I am not able to reduce the complexity of this problem. Please give some better approach.

Is there a mathematical formula that i am not aware of or it can be done with better approach?

Problem Link

Description:

 A special number is not divisible by any number of the form Z*Z where (Z>1).

Problem: Find the number of special numbers in a given range.

Integer Limit:10^9

I have done it like this:

    import math
    def special(x):
        flag=1
        i=2
        if(x==0 or x==1 or x==2):
            return 1
        while(i*i <= x):               //This is the best i can think to limit the numbers.
            if(x%(i*i)==0):
                flag=0
                break
            i=i+1
        return flag

    t=int(raw_input())
    while(t):
        x,y=map(int,raw_input().split())
        count=0
        for i in xrange(x,y+1):
            if(special(i)):
                count+=1
        print(count)
        t=t-1
Arvind
  • 2,338
  • 1
  • 9
  • 20

1 Answers1

1

In special(x) you only have to iterate over primes less than or equal to sqrt(x). So I would precompute a list of primes (Fastest way to list all primes below N).

Community
  • 1
  • 1
lukas1994
  • 1,709
  • 1
  • 12
  • 17
  • Why only primes? Suppose number is 36 then its divisible by 6*6. So in the special function we need to check for 6 which is not prime. – Arvind Apr 15 '14 at 21:00