Task

Write a function to test if a number is   square-free.



A   square-free   is an integer which is divisible by no perfect square other
than   1   (unity).

For this task, only positive square-free numbers will be used.



Show here (on this page) all square-free integers (in a horizontal format) that are between:

   1   ───►   145     (inclusive)
   1 trillion   ───►   1 trillion + 145     (inclusive)


(One trillion = 1,000,000,000,000)



Show here (on this page) the count of square-free integers from:

   1   ───►   one hundred     (inclusive)
   1   ───►   one thousand     (inclusive)
   1   ───►   ten thousand     (inclusive)
   1   ───►   one hundred thousand     (inclusive)
   1   ───►   one million     (inclusive)


See also

   the Wikipedia entry:   square-free integer


 
# syntax: GAWK -f SQUARE-FREE_INTEGERS.AWK
# converted from LUA
BEGIN {
    main(1,145,1)
    main(1000000000000,1000000000145,1)
    main(1,100,0)
    main(1,1000,0)
    main(1,10000,0)
    main(1,100000,0)
    main(1,1000000,0)
    exit(0)
}
function main(lo,hi,show_values,  count,i,leng) {
    printf("%d-%d: ",lo,hi)
    leng = length(lo) + length(hi) + 3
    for (i=lo; i<=hi; i++) {
      if (square_free(i)) {
        count++
        if (show_values) {
          if (leng > 110) {
            printf("\n")
            leng = 0
          }
          printf("%d ",i)
          leng += length(i) + 1
        }
      }
    }
    printf("count=%d\n\n",count)
}
function square_free(n,  root) {
    for (root=2; root<=sqrt(n); root++) {
      if (n % (root * root) == 0) {
        return(0)
      }
    }
    return(1)
}
 