g | x | w | all
Bytes Lang Time Link
039APLNARS250622T161215ZRosario
092Haskell250622T140013ZIbozz91
058Perl 5 MListUtil=sum ap250618T160702ZXcali
014Uiua SBCS250617T220251Zlolad
118C250617T082346ZToby Spe
033GNU coreutils170201T104902ZToby Spe
022Wolfram Language Mathematica201016T230022Zatt
105Python110212T210442ZMrD
011APL Dyalog Extended200919T164321ZRazetime
008Japt x201016T154610ZShaggy
071Python 2141125T205232Zxnor
278Java 278 Characters141125T220706ZCompass
144C 144 Chars110214T053141Zst0le
114Ruby110212T183139ZArnaud L
084Python110213T172820ZKeith Ra
021J110212T234010ZEelvex
nan110212T203607ZAman Zee

APL(NARS), 39 chars

r←h w
r←0
→0×⍳0≥w-←1⋄r+←∼∨/2≤≢¨⊂⍨πw⋄→2

//6+4+29=39

Input the number w, output the count of sqarefree number in the range 1..w-1.

∼∨/2≤≢¨⊂⍨πw is the piece of function that calculate if w is a square free number.

Test:

  h 10
6
  h 1000
608
  h 10000
6083
  h 100000
60794
  h 1000000
607926

Haskell, 92 bytes

import Data.List
f n=n-1-(length$group$sort[y|x<-[2..n],y<-takeWhile(<n)$iterate(+x^2)$x^2])

Try it online!

Not the best language for this challenge.

Perl 5 -MList::Util=sum -ap, 58 bytes

map{$b=0;$r[$b]=1while($b+=$_*$_)<"@F"}2..$_;$_=$_-sum@r,1

Try it online!

Uiua (SBCS), 14 bytes

/+≡⍣(/×◰°/×)0⇡

Pad

Explanation

⇡        # Create range [0, n)
≡        # For every number in the range...
⍣(       # ...try to...
    °/×  # prime factorise
    ◰    # mask by first occurrences of each prime
    /×   # reduce, true iff squarefree
)0       # if error, count as not squarefree 
         # this only affects 0, excluding it from the count
/+       # sum

C, 118 bytes

x[2000000],i=1,j;main(){for(;++i<1e3;)for(j=0;j<1e6;)x[j+=i*i]=1;scanf("%d",&j);for(i=0;--j;)i+=!x[j];printf("%d",i);}

Input via stdin, completes in under 5ms for N=1,000,000 on my machine.

How it works

int x[2'000'000];  /* oversize array so we can write to j+=i*i safely */
int i, j;

int f(int n)
{
    /* write 1 to all multiples of squares */
    for (i = 2;  i < 1'000;  ++i) {
        for (j = 0;  j < 1'000'000; ) {
            j += i*i;
            x[j]=1;
        }
    }
    /* count the zeroes */
    for (i = 0;  --n; ) {
        i += !x[n];
    }
    return i;
}

GNU coreutils, 44 33 bytes

seq $1|factor -h|fgrep -cve^ -e$1

This factors every number up to and including N, and counts lines not containing ^ or the input value (to exclude N itself - no other line can contain that as a substring). It requires a recent enough implementation of factor that has the -h option to gather repeated factors.

With N=1,000,000, this completes on my machine in about ⅕ second.

If the problem spec were inclusive, then we'd be able to do it in just 28 bytes:

seq $1|factor -h|fgrep -cv ^

Wolfram Language (Mathematica), 24 22 bytes

#.#&@*MoebiusMu@*Range

Try it online!

SquareFreeQ is too long anyways.

Python - 105 bytes

s=input()
C=set()
for q in [k*k for k in range(2,s+1)]:
 i=1
 while q*i<=s:C.add(q*i);i+=1
print s-len(C)

APL (Dyalog Extended), 11 bytes

+/≡∘∪⍨⍤⍭¨⍤⍳

Try it online!

Bubbler's tacit form. (-4 bytes)

-1 byte from Adám.

APL (Dyalog Extended), 16 bytes

{+/{(⊢≡∪)⍭⍵}¨⍳⍵}

Try it online!

Same approach as the J answer, 100000 runs under 10 secs on tio, so it should be all good.

Explanation

{+/{(⊢≡∪)⍭⍵}¨⍳⍵} 
   {(⊢≡∪)⍭⍵}     square-free checking function
   {     ⍭⍵}     prime factors of ⍵
    (⊢≡∪)        are the unique values equal to the originals?
                 function ends
            ¨⍳⍵  map function to range 1-n
 +/              sum the resulting boolean array

Japt -x, 8 bytes

õ_=k)eZâ

Try it

õ_=k)eZâ     :Implicit input of integer U
õ            :Range [1,U]
 _           :Map each Z
  =          :  Reassign to Z
   k         :  Prime factors of Z
    )        :  End reassignment
     e       :  Test equality with
      Zâ     :  Z deduplicated
             :Implicit output of sum of resulting array

Python 2: 71 chars

i=n=input()
A=[1]*n
while~-i:A[i*i::i*i]=~-n/i/i*[0];i-=1
print~-sum(A)

Basically an optimization of Keith Randall's solution of zeroing out numbers that are multiples of squares. The main improvement is directly zeroing out the sublist A[i*i::i*i], which requires awkwardly calculating its length or else Python refuses to do the slice assignment.

Lots of ~- are used for -1. The ~-sum(A) corrects for 0 being falsely counted as a squarefree number.

Java - 278 Characters

A golfed version of the Java answer already demonstrated here without changing the spirit of the answer, because the original poster's idea golfed could have saved 50%, which is huge compared to most other languages!

class T {public static void main(String[] a) {int n,i,j;double k;n=Integer.parseInt(a[0]);int l[]=new int[n];for(i=0;i<n;)l[i]=1+i++;for (i=1;i<n;i++) {k=Math.sqrt(l[i]);if(k==Math.floor(k))for (j=i;j<n;j+=i+1)l[j]=-1;}j=0;for(i=0;i<n;)if(l[i++]!=-1)j++;System.out.println(j);}}

Specifically, a few Java golfing tricks were used to optimize this:

This shows that the original ungolfed solution of 565 characters can be brought down to a very respectable <300 character answer even with a more structured language like Java.

Ungolfed

class T {
    public static void main(String[] a) {
        int n, i, j;
        double k;
        n = Integer.parseInt(a[0]);
        int l[] = new int[n];
        for (i = 0; i < n;)
            l[i] = 1 + i++;
        for (i = 1; i < n; i++) {
            k = Math.sqrt(l[i]);
            if (k == Math.floor(k))
                for (j = i; j < n; j += i + 1)
                    l[j] = -1;
        }
        j = 0;
        for (i = 0; i < n;)
            if (l[i++] != -1)
                j++;
        System.out.println(j);
    }
}

C

144 Chars

#define m 1000001
x[m];
main(s,o,f){
for(o=2;o*o<m;o++)
for(f=s=o*o;f<m;f+=s)x[f]=1;
scanf("%d",&f);
for(s=0,o=1;o<f;o++)if(!x[o])s++;
printf("%d",s);
}

Instantaneous Solution on my crummy PC! :)

Ruby - 119 114

I=gets.to_i
m=[]
(s=->i{(I>i*i)?s[i+1]+[i*i]:[]})[2].map{|s|(1..I).map{|x|s*x<I||break;m<<s*x}}
p ([*1...I]-m).size

Python, 84 characters

n=input()
R=range(n)
A=[1]*n
for i in R[2:]:
 for x in R[::i*i]:A[x]=0
print sum(A)

J, 21 chars

+/(]=&#~.)@:q:"0>:i.N

Checks for each integer from 1 to N if any prime factor appears more than once.

eg:

+/(]=&#~.)@:q:"0>:i.10000
 6083
+/(]=&#~.)@:q:"0>:i.100000
 60794

Takes ~3secs for N=1,000,000 (@2GHz,1core)

Java Solution

import java.io.*;
    public class Main{
            public static void main(String[] args)throws Exception {
              int N=Integer.parseInt(new BufferedReader(new InputStreamReader(System.in)).readLine());
              int list[]=new int[N];
    for (int i=0;i<N;i++){
    list[i]=(i+1);
    }
    for (int i=1;i<N;i++){
    if (Math.sqrt(list[i])-Math.floor(Math.sqrt(list[i]))==0){
    for(int j=i;j<N;j+=(i+1)){
    list[j]=-1;
    }}}
    int c=0;
    for (int i=0;i<N;i++){
    if (list[i]!=-1){
    c++;
    }}System.out.println(c);}}

IDEONE http://ideone.com/Yf467