| Bytes | Lang | Time | Link |
|---|---|---|---|
| 039 | APLNARS | 250622T161215Z | Rosario |
| 092 | Haskell | 250622T140013Z | Ibozz91 |
| 058 | Perl 5 MListUtil=sum ap | 250618T160702Z | Xcali |
| 014 | Uiua SBCS | 250617T220251Z | lolad |
| 118 | C | 250617T082346Z | Toby Spe |
| 033 | GNU coreutils | 170201T104902Z | Toby Spe |
| 022 | Wolfram Language Mathematica | 201016T230022Z | att |
| 105 | Python | 110212T210442Z | MrD |
| 011 | APL Dyalog Extended | 200919T164321Z | Razetime |
| 008 | Japt x | 201016T154610Z | Shaggy |
| 071 | Python 2 | 141125T205232Z | xnor |
| 278 | Java 278 Characters | 141125T220706Z | Compass |
| 144 | C 144 Chars | 110214T053141Z | st0le |
| 114 | Ruby | 110212T183139Z | Arnaud L |
| 084 | Python | 110213T172820Z | Keith Ra |
| 021 | J | 110212T234010Z | Eelvex |
| nan | 110212T203607Z | Aman 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])
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
Uiua (SBCS), 14 bytes
/+≡⍣(/×◰°/×)0⇡
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 ^
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
+/≡∘∪⍨⍤⍭¨⍤⍳
Bubbler's tacit form. (-4 bytes)
-1 byte from Adám.
APL (Dyalog Extended), 16 bytes
{+/{(⊢≡∪)⍭⍵}¨⍳⍵}
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â
õ_=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:
- Removed long variable names
- Removed the use of a Reader in favor of using an argument (saves a huge amount of characters, including import, Exception handling, and casting the reader in general)
- Removed extra brackets and parentheses using single-line actions for ifs and fors where applicable
- Declare ints beforehand together to reduce calls to int
- Declare a double and optimize square calculation
- Extracted ++ where possible
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