nth number having n number of distinct prime factors
Java, 170 characters in one line
int a(int n) {
int a = 2, t = a, m = 1, i = 1;
Set s = new HashSet();
while (i++ > 0) {
if (t % i == 0) {
s.add(i);
t /= i;
if (t == 1) {
if (s.size() == n) {
if (n == m) {
break;
}
m++;
}
t = ++a;
s.clear();
}
i = 1;
}
}
return a;
}
Update, +77 characters IOL
int[] f(int n) {
int[] f = new int[n];
for (int i = 0; i < n; i++) {
f[i] = a(i+1);
}
return f;
}
Java (Ungolfed)
public class M {
public static void main(String[] a) {
final int N = 100000000;
int[] p = new int[N];
for(int f = 2; f * f < N; f++) {
if(p[f] == 0)
for(int k = f; k < N; k += f)
p[k]++;
}
for(int i = 1; i <= 8; i++) {
int c = 0, j;
for(j = 1; j < N && c < i; j++)
if(p[j] == i)
c++;
if(c == i)
System.out.println(i + " = " + --j);
}
}
}
Uses a sieve algorithm. It's pretty quick. (6 Seconds)
Will work accurately for upto 8
, will probably fail for anything higher.
Python, 144 chars
R=range
P=[x for x in R(2,99)if all(x%i for i in R(2,x))]
for a in R(input()):
x=n=0
while n<=a:n+=sum(x%p==0for p in P)==a+1;x+=1
print x-1
It takes about 2 minutes to run to completion for x=8.