The sq-free? word merits some explanation. Per the Wikipedia entry on square-free integers, A positive integer n is square-free if and only if in the prime factorization of n, no prime factor occurs with an exponent larger than one.

For instance, the prime factorization of 12 is 2 * 2 * 3, or in other words, 22 * 3. The 2 repeats, so we know 12 isn't square-free.

For brevity and in order to highlight some points of interest regarding jq,
this entry focuses on solving the task in a manner that reflects
the specification as closely as possible (no prime sieves or calls
to sqrt), with efficiency concerns playing second fiddle.

Once a suitable generator for squares and a test for divisibility have been written, the
test for whether a number is square-free can be written in one line:

def is_square_free: . as $n | all( squares; divides($n) | not);

In words: to verify whether an integer, $n, is square_free, check that no admissible square divides $n.

squares

We could define the required `squares` generator using `while`:

def squares: . as $n | 2 | while(.*. <= $n; .+1) | .*.;

(Here `.*.` calculates the square of the input number.)

However, this entails performing an unnecessary multiplication, so the question becomes whether there
is a more economical solution that closely reflects the specification of the required generator. Since jq supports tail-recursion optimization for 0-arity filters, the answer is:

function squarefreecount(intervals, maxnum) count = 0 for n in 1:maxnum for i in 1:length(intervals) if intervals[i] < n println("There are $count square free numbers between 1 and $(intervals[i]).") intervals[i] = maxnum + 1 end end if issquarefree(n) count += 1 end end println("There are $count square free numbers between 1 and $maxnum.") end

The prime factoring algorithm is not really the best option for finding long runs of sequential square-free numbers. It works, but is probably better suited for testing arbitrary numbers rather than testing every sequential number from 1 to some limit. If you know that that is going to be your use case, there are faster algorithms.

function square_free(atom start, finish) sequence res = {} if start=1 then res = {1} start = 2 end if while start<=finish do sequence pf = prime_factors(start, duplicates:=true) for i=2 to length(pf) do if pf[i]=pf[i-1] then pf = {} exit end if end for if pf!={} then res &= start end if start += 1 end while return resend function

The number of square─free numbers between 1 and 100 (inclusive) is: 61
The number of square─free numbers between 1 and 1000 (inclusive) is: 608
The number of square─free numbers between 1 and 10000 (inclusive) is: 6083
The number of square─free numbers between 1 and 100000 (inclusive) is: 60794
The number of square─free numbers between 1 and 1000000 (inclusive) is: 607926

In Sidef, the functions is_square_free(n) and square_free_count(min, max) are built-in. However, we can very easily reimplement them in Sidef code, as fast integer factorization methods are also available in the language.