Finding prime numbers without using "prime characters"
CJam, 19 18 30 34 33 19 17 21 20 bytes
Try it online.
{_3\#,2>__ff*:~-<N*}
This is probably one of the most horribly inefficient algorithms I've ever implemented. But I did it for the size!
My answer consists of a code block, which acts like an anonymous function in CJam. Run it with an integer immediately preceding it on the stack, and the resulting list is dumped onto the stack. I treat the upper bound on the input as infinite so I don't have to check that bound.
My algorithm starts by raising 3 to the input
th power, which is guaranteed to give a number larger than the input
-th prime if the input is valid. Then a list of the integers from 2 to this number minus one is generated, which is a large enough swath to contain all the prime numbers we want. To get rid of the composite numbers... sigh... we create a list of every pairwise product, which should generate all composite numbers from 4 to some stupidly large value, large enough for our purposes. Then it's just a matter of removing every element from the original list that is in this composite list, trimming it down to the first input
elements, and joining the elements with the newline character.
The algorithm should work for any input. However, whether or not the interpreter/computer has enough memory or time is a whole other question, because the time and space requirements are exponential with respect to the input. So if the input is larger than about 5 for the online interpreter or about 8 for the offline one, then the answer to that question is probably no.
Java. 474 bytes
i\u006dport j\u0061v\u0061.util.*\u003bvoid b(int b\u0029{Lon\u0067 c\u003d2L,d,f[]\u003d{}\u003bfor(f\u003dArr\u0061ys.copy\u004ff(f,b\u0029,Arr\u0061ys.fill(f,0L\u0029\u003bb-->0\u003b\u0029for(d\u003d0L\u003bf[b]<1\u003bf[b]\u003dd<1?c:f[b],d\u003d0L,c\u002b\u002b\u0029for(lon\u0067 h:f\u0029d\u003dh>0&&c\u002fh*h\u003d\u003dc?1:d\u003bj\u0061v\u0061x.x\u006dl.bind.JAXB.un\u006d\u0061rsh\u0061l(""\u002bArr\u0061ys.\u0061sList(f\u0029,Lon\u0067.cl\u0061ss\u0029\u003b}
Takes input via function argument, outputs via thrown exception.
Indented:
i\u006dport j\u0061v\u0061.util.*\u003b
void b(int b\u0029{
Lon\u0067 c\u003d2L,d,f[]\u003d{}\u003b
for(f\u003dArr\u0061ys.copy\u004ff(f,b\u0029,Arr\u0061ys.fill(f,0L\u0029\u003bb-->0\u003b\u0029
for(d\u003d0L\u003bf[b]<1\u003bf[b]\u003dd<1?c:f[b],d\u003d0L,c\u002b\u002b\u0029
for(lon\u0067 h:f\u0029
d\u003dh>0&&c\u002fh*h\u003d\u003dc?1:d\u003b
j\u0061v\u0061x.x\u006dl.bind.JAXB.un\u006d\u0061rsh\u0061l(""\u002bArr\u0061ys.\u0061sList(f\u0029,Lon\u0067.cl\u0061ss\u0029\u003b
}
Escaped characters removed:
import java.util.*;
void b(int b){
Long c=2L,d,f[]={};
for(f=Arrays.copyOf(f,b),Arrays.fill(f,0L);b-->0;)
for(d=0L;f[b]<1;f[b]=d<1?c:0,d=0L,c++)
for(long h:f)
d=h>0&&c/h*h==c?1:d;
javax.xml.bind.JAXB.unmarshal(""+Arrays.asList(f),Long.class);
}
Explanation:
Long c,d,f[]={}; //Initialize variables.
for(f=java.util.Arrays.copyOf(f,b),Arrays.fill(f,0L);b-->0;)
f=java.util.Arrays.copyOf(f,b),Arrays.fill(f,0L) //Initialize f to an array of 0's.
b-->0 //Iterate over the first b primes.
for(d=0L;f[b]<1;f[b]=d<1?c:0,d=0L,c++)
d=0L d=0L //Initialize d to 0.
f[b]<1 c++ //Increment c while the b'th prime is 0.
f[b]=d<1?c:0 //If d = 0, the b'th prime = c, else continue.
for(long h:f) //Iterate over all found primes.
d=h>0&&c/h*h==c?1:d;
h>0 //Ignore non-found primes.
c/h*h==c //Equivalent to c%h==0
?1:d //If h is prime and c is divisible by h, d = 1. Otherwise d stays unchanged.
javax.xml.bind.JAXB.unmarshal(""+Arrays.asList(f),Long.class) //Print solution to stderr
javax.xml.bind.JAXB.unmarshal( ,Long.class) //Prints what's contained to stderr.
Arrays.asList(f) //Convert f to list.
""+ //Convert to string.
My original solution used a return
statement. After asking this question on StackOverflow, regettman was kind enough to provide a way to output/return without using prime letters.
As usual, suggestions are welcome :)
Ruby, 74
->n,*o{o<<[2..n*n][0].find{|x|!o.find{|y|1.>x.^y.*x.div y}}until o[n-1]
o}
Explanation:
*o
initializes an empty output array. until it has n
items, we find the smallest number >=2 that doesn't divide any item currently in o
, then add it to o
. To test for division, yikes. All the good operators are disallowed, and I can't even use divmod
. Best I could see was to use x.div y
, which takes x divided by y and rounds down, then multiply that by y again. If it equals x, there was no rounding, so y divides x. 1.>x.^
is an equality test, checking whether the result of xor is 0. The .
before every operator is because you can't mix .
-free operator calls and parenthesis-free method calls.
Edit: The range-checking specifications were added after I posted this, I think. To comply requires 79 characters:
->n,*o{o<<[*2..-~n*n].find{|x|!o.find{|y|1.>x.^y.*x.div y}}until o[n-1]||n<1
o}