Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Count of squarefree numbers in range

Given two numbers x and y , find count of numbers that are squarefree where squarefree number is one divisible by no perfect square, except 1. For example, 10 is square-free but 18 is not, as it is divisible by 9 = 32. Few positive square-free numbers are :

1, 2, 3, 5, 6, 7, 10, 11, 13, 14, 15 ...

limits

1 <= X,Y <= 10^9

0 <= |X-Y| <= 10^6

x=10 , Y=15 

gives

ans=5

My approach is to generate all prime till squareroot(10^9) (sieve of eratosthenes), and check whether each number in given range divisible by square of prime . Count of such numbers is substracted from length of range to give square free numbers .

But this approach time out in complexity , please suggest some other approach

like image 513
user3529205 Avatar asked Apr 13 '14 15:04

user3529205


People also ask

Is 1 a Squarefree number?

A number is said to be squarefree (or sometimes quadratfrei; Shanks 1993) if its prime decomposition contains no repeated factors. All primes are therefore trivially squarefree. The number 1 is by convention taken to be squarefree. The squarefree numbers are 1, 2, 3, 5, 6, 7, 10, 11, 13, 14, 15, ...

How many square numbers are free between 1 and 100?

Therefore on counting we can see that there are 10 perfect square numbers between 1 to 100. Q.

What does Square free mean in math?

In mathematics, a square-free integer (or squarefree integer) is an integer which is divisible by no square number other than 1. That is, its prime factorization has exactly one factor for each prime that appears in it.

What is the probability that the number is square free?

The chance that a "random integer" does not divide by p2 is 1−1/p2. So heuristically the probability that the random integer is square free is the product of 1−1/p2 over all primes.


2 Answers

Use the inclusion-exclusion principle:

Let f(n) = number of non-square-free numbers in 1 ... n. We will only do inclusion-exclusion on the squares of primes, so as to avoid overcounting for squares of squares.

We have:

f(n) = n / 4      => these are divisible by 4, so NOT square-free
       +
       n / 9      => these are divisible by 9, so NOT square-free
       + 
       n / 25     => these are divisible by 16, so NOT square-free
       +
       ...
       -
       n / (4*9)  => these are divisible by both 4 and 9, so we overcounted
       -
       n / (4*25) => these are divisible by both 4 and 25, so we overcounted 
       -
       ...

How efficient is this?

We only need primes p such that p^2 <= 10^9, meaning p < 31623. This already isn't a lot of primes, any trivial sieve (or maybe even trial division) should be fast enough. Then apply inclusion-exclusion, which will also be fast since the products of squared primes will get large fast so you'll be able to terminate prematurely in a a lot of cases (n / something = 0 whenever something > n).

In order to see why you'll be able to terminate prematurely, rewrite the above as:

f(n) = n / (2^2) -
       n / (2^2*3^2) +
       n / (2^2*3^2*5^2) -  <= this becomes 0 very fast. 
                               When it does, 
                               backtrack and increment the previous term.
                               For example, if this were 0,
                               you'd do - n / (2^2*5^2) next
       ...

More info on this here.

like image 175
IVlad Avatar answered Sep 30 '22 10:09

IVlad


To expand on my comment: assume that X <= Y and initialize a boolean array SF[X..Y] to be all true. For every k from 2 to floor(sqrt(Y)) (optionally including the composites; the asymptotic running time stays the same), for every multiple m of k² between X and Y, set SF[m] to false. Return the number of true values remaining in SF.

The running time is O((Y - X) + sqrt(Y)), since Σk=1,...,∞ 1/k² = π²/6.

like image 40
David Eisenstat Avatar answered Sep 30 '22 10:09

David Eisenstat