21
2
Your task, if you choose to accept it, is to write a program/function that accepts an integer N as input. The program/function should output/return a list of the first N prime numbers. But here's the catch: you are not allowed to use prime characters in your code. A prime character is a character whose Unicode code point is a prime number. In the printable ASCII range, these are:
%)+/5;=CGIOSYaegkmq
But the rule also applies to non-ASCII characters if your code uses those.
- A valid input is an integer N where 0 < N <= T, where you can choose T, but it has to be greater than or equal to 10000. T does not have to be finite.
- For invalid inputs (non-integers, integers out of range), throw an exception or output/return nothing/null.
- An integer with leading/trailing whitespace as input is considered invalid.
- An integer with a
+
as sign character as input is considered invalid. - An integer with leading zeros as input is considered valid.
- If your language allows you to pass an already-parsed integer as input, the above parsing rules (except the range one) don't apply, because the int is already parsed.
- The input is always base-10.
- Use of built-in prime generators and primality testers (this includes prime factorization functions) is not allowed.
- The source restriction is imposed on Unicode characters, but the byte counting for the score can be in another encoding if you wish.
- The output can contain a single trailing newline, but this is not required.
- If you output/return the prime number list as a string, then every prime number must be delimited by one or multiple non-digit char(s). You can choose which delimiter you use.
- This is a code-golf challenge, the shortest code in bytes wins.
Stack Snippet to verify your code
You can use the below Stack Snippet to verify that your code does not contain prime chars:
var primes=[],max=10000;for(var i=2;i<=max;i++){primes.push(i);}for(var N=2;N<Math.sqrt(max);N++){if(primes.indexOf(N)===-1){continue;}primes=primes.filter(function (x){return x===N||x%N!==0;});}function setText(elem,text){var z=('innerText' in elem)? 'innerText' : 'textContent';elem[z]=text;}function verify(inputCode,resultSpan){var invalidChars=[];var success=true;for(var i=0;i<inputCode.length;i++){var cc = inputCode.charCodeAt(i);if (cc>max){setText(resultSpan,"Uh oh! The char code was bigger than the max. prime number calculated by the snippet.");success = false;break;}if (primes.indexOf(cc)!==-1){invalidChars.push(inputCode[i]);}}if (invalidChars.length===0&&success){setText(resultSpan, "Valid code!");}else if(success) { var uniqueInvalidChars = invalidChars.filter(function (x, i, self){return self.indexOf(x)===i;});setText(resultSpan, "Invalid code! Invalid chars: " + uniqueInvalidChars.join("")); }}document.getElementById("verifyBtn").onclick=function(e){e=e||window.event;e.preventDefault();var code=document.getElementById("codeTxt").value;verify(code,document.getElementById("result"));};
Enter your code snippet here:<br /><textarea id="codeTxt" rows="5" cols="70"></textarea><br /><button id="verifyBtn">Verify</button><br /><span id="result"></span>
10It's pretty cruel that
;
happens to be banned... – ɐɔıʇǝɥʇuʎs – 2015-02-20T16:55:18.807If primality testers aren't allowed, what about prime factorization functions. – Maltysen – 2015-02-20T17:06:59.993
@Maltysen From prime factorization functions, you can very quickly see whether a number is a prime or not, so I'm afraid that's disallowed. I'll clarify that. – ProgramFOX – 2015-02-20T17:10:16.800
Are we mandated to throw out some of these invalid inputs? For instance, if our language's string->int function allows a leading
+
, it seems disappointing to be required to manually throw these out. – Runer112 – 2015-02-20T17:28:50.497@Runer112 You don't have to throw. As the rules state, you can also simply exit/return with no/empty output. – ProgramFOX – 2015-02-20T17:30:10.170
@ProgramFOX My point is, I'm going to have to add code to disallow what my language thinks (and I think) is a valid integer, but that your spec doesn't. And that seems a bit silly. I would suggest that the spec presented a more minimal set of valid/invalid input rules, and left the rest up to user's choice. – Runer112 – 2015-02-20T17:33:24.327
11I got all excited about this and started on a solution, then realized closed parens are disallowed. Well, I'm out. – Alex A. – 2015-02-20T17:35:01.070
@Runer112 I changed the rules and now the input range is 0 < N <= T, where you can choose T, but it has to be greater than or equal to 10000. This rule change does not invalidate existing answers. – ProgramFOX – 2015-02-20T17:42:09.207
@ProgramFOX But we're still required to treat inputs that start with a
+
as invalid, even if our language's string->int function parses them fine? – Runer112 – 2015-02-20T17:45:39.120@Runer112 Yes, you are. I will not drop that requirement, because it makes a nice addition and makes the challenge a bit more difficult. – ProgramFOX – 2015-02-20T17:47:25.937
Do we have to treat multiple lines of input as invalid as well? – Runer112 – 2015-02-20T17:57:43.057
@Runer112 How do you mean, multiple lines? Just trailing/leading newlines, or the integer split up? – ProgramFOX – 2015-02-20T17:59:49.610
@ProgramFOX I assumed trailing/leading, since if the integer was split up, then it seems that the input couldn't be treated as a valid integer anyways. – Runer112 – 2015-02-20T18:02:44.757
@Runer112 In that case, it's invalid. I'll change "spaces" into "whitespace". – ProgramFOX – 2015-02-20T18:03:12.310
I bet it's possible in Brainfuck. But you'd have to use subtraction instead of addition. Nvm, it'd be easier to just use Blub. – mbomb007 – 2015-02-20T22:33:18.803
In groovy, got this far
it.findAll{x->y=2..x**0.5;y.every{x%it}}
then realized % was one of the characters excluded and facepalmed... – Magic Octopus Urn – 2016-10-17T15:32:55.917In JavaScript, you can't possibly assign an object, as
=
is banned, so you'd have to make it a single expression. In addition, you can't call a function with()
(or make a function in any way), so you'd have to use tagged templates. I'd be surprised if it's actually possible. (Note: You can start by getting input withpro\u006dpt``
.) – ETHproductions – 2016-11-08T23:40:17.923