Is this number a prime?

202

46

Believe it or not, we do not yet have a code golf challenge for a simple primality test. While it may not be the most interesting challenge, particularly for "usual" languages, it can be nontrivial in many languages.

Rosetta code features lists by language of idiomatic approaches to primality testing, one using the Miller-Rabin test specifically and another using trial division. However, "most idiomatic" often does not coincide with "shortest." In an effort to make Programming Puzzles and Code Golf the go-to site for code golf, this challenge seeks to compile a catalog of the shortest approach in every language, similar to "Hello, World!" and Golf you a quine for great good!.

Furthermore, the capability of implementing a primality test is part of our definition of programming language, so this challenge will also serve as a directory of proven programming languages.

Task

Write a full program that, given a strictly positive integer n as input, determines whether n is prime and prints a truthy or falsy value accordingly.

For the purpose of this challenge, an integer is prime if it has exactly two strictly positive divisors. Note that this excludes 1, who is its only strictly positive divisor.

Your algorithm must be deterministic (i.e., produce the correct output with probability 1) and should, in theory, work for arbitrarily large integers. In practice, you may assume that the input can be stored in your data type, as long as the program works for integers from 1 to 255.

Input

  • If your language is able to read from STDIN, accept command-line arguments or any other alternative form of user input, you can read the integer as its decimal representation, unary representation (using a character of your choice), byte array (big or little endian) or single byte (if this is your languages largest data type).

  • If (and only if) your language is unable to accept any kind of user input, you may hardcode the input in your program.

    In this case, the hardcoded integer must be easily exchangeable. In particular, it may appear only in a single place in the entire program.

    For scoring purposes, submit the program that corresponds to the input 1.

Output

Output has to be written to STDOUT or closest alternative.

If possible, output should consist solely of a truthy or falsy value (or a string representation thereof), optionally followed by a single newline.

The only exception to this rule is constant output of your language's interpreter that cannot be suppressed, such as a greeting, ANSI color codes or indentation.

Additional rules

  • This is not about finding the language with the shortest approach for prime testing, this is about finding the shortest approach in every language. Therefore, no answer will be marked as accepted.

  • Submissions in most languages will be scored in bytes in an appropriate preexisting encoding, usually (but not necessarily) UTF-8.

    The language Piet, for example, will be scored in codels, which is the natural choice for this language.

    Some languages, like Folders, are a bit tricky to score. If in doubt, please ask on Meta.

  • Unlike our usual rules, feel free to use a language (or language version) even if it's newer than this challenge. If anyone wants to abuse this by creating a language where the empty program performs a primality test, then congrats for paving the way for a very boring answer.

    Note that there must be an interpreter so the submission can be tested. It is allowed (and even encouraged) to write this interpreter yourself for a previously unimplemented language.

  • If your language of choice is a trivial variant of another (potentially more popular) language which already has an answer (think BASIC or SQL dialects, Unix shells or trivial Brainfuck derivatives like Headsecks or Unary), consider adding a note to the existing answer that the same or a very similar solution is also the shortest in the other language.

  • Built-in functions for testing primality are allowed. This challenge is meant to catalog the shortest possible solution in each language, so if it's shorter to use a built-in in your language, go for it.

  • Unless they have been overruled earlier, all standard rules apply, including the http://meta.codegolf.stackexchange.com/q/1061.

As a side note, please don't downvote boring (but valid) answers in languages where there is not much to golf; these are still useful to this question as it tries to compile a catalog as complete as possible. However, do primarily upvote answers in languages where the author actually had to put effort into golfing the code.

Catalog

The Stack Snippet at the bottom of this post generates the catalog from the answers a) as a list of shortest solution per language and b) as an overall leaderboard.

To make sure that your answer shows up, please start your answer with a headline, using the following Markdown template:

## Language Name, N bytes

where N is the size of your submission. If you improve your score, you can keep old scores in the headline, by striking them through. For instance:

## Ruby, <s>104</s> <s>101</s> 96 bytes

If there you want to include multiple numbers in your header (e.g. because your score is the sum of two files or you want to list interpreter flag penalties separately), make sure that the actual score is the last number in the header:

## Perl, 43 + 2 (-p flag) = 45 bytes

You can also make the language name a link which will then show up in the snippet:

## [><>](http://esolangs.org/wiki/Fish), 121 bytes

<style>body { text-align: left !important} #answer-list { padding: 10px; width: 290px; float: left; } #language-list { padding: 10px; width: 290px; float: left; } table thead { font-weight: bold; } table td { padding: 5px; }</style><script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <link rel="stylesheet" type="text/css" href="//cdn.sstatic.net/codegolf/all.css?v=83c949450c8b"> <div id="language-list"> <h2>Shortest Solution by Language</h2> <table class="language-list"> <thead> <tr><td>Language</td><td>User</td><td>Score</td></tr> </thead> <tbody id="languages"> </tbody> </table> </div> <div id="answer-list"> <h2>Leaderboard</h2> <table class="answer-list"> <thead> <tr><td></td><td>Author</td><td>Language</td><td>Size</td></tr> </thead> <tbody id="answers"> </tbody> </table> </div> <table style="display: none"> <tbody id="answer-template"> <tr><td>{{PLACE}}</td><td>{{NAME}}</td><td>{{LANGUAGE}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr> </tbody> </table> <table style="display: none"> <tbody id="language-template"> <tr><td>{{LANGUAGE}}</td><td>{{NAME}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr> </tbody> </table><script>var QUESTION_ID = 57617; var ANSWER_FILTER = "!t)IWYnsLAZle2tQ3KqrVveCRJfxcRLe"; var COMMENT_FILTER = "!)Q2B_A2kjfAiU78X(md6BoYk"; var OVERRIDE_USER = 12012; var answers = [], answers_hash, answer_ids, answer_page = 1, more_answers = true, comment_page; function answersUrl(index) { return "https://api.stackexchange.com/2.2/questions/" + QUESTION_ID + "/answers?page=" + index + "&pagesize=100&order=desc&sort=creation&site=codegolf&filter=" + ANSWER_FILTER; } function commentUrl(index, answers) { return "https://api.stackexchange.com/2.2/answers/" + answers.join(';') + "/comments?page=" + index + "&pagesize=100&order=desc&sort=creation&site=codegolf&filter=" + COMMENT_FILTER; } function getAnswers() { jQuery.ajax({ url: answersUrl(answer_page++), method: "get", dataType: "jsonp", crossDomain: true, success: function (data) { answers.push.apply(answers, data.items); answers_hash = []; answer_ids = []; data.items.forEach(function(a) { a.comments = []; var id = +a.share_link.match(/\d+/); answer_ids.push(id); answers_hash[id] = a; }); if (!data.has_more) more_answers = false; comment_page = 1; getComments(); } }); } function getComments() { jQuery.ajax({ url: commentUrl(comment_page++, answer_ids), method: "get", dataType: "jsonp", crossDomain: true, success: function (data) { data.items.forEach(function(c) { if (c.owner.user_id === OVERRIDE_USER) answers_hash[c.post_id].comments.push(c); }); if (data.has_more) getComments(); else if (more_answers) getAnswers(); else process(); } }); } getAnswers(); var SCORE_REG = /<h\d>\s*([^\n,<]*(?:<(?:[^\n>]*>[^\n<]*<\/[^\n>]*>)[^\n,<]*)*),.*?(\d+)(?=[^\n\d<>]*(?:<(?:s>[^\n<>]*<\/s>|[^\n<>]+>)[^\n\d<>]*)*<\/h\d>)/; var OVERRIDE_REG = /^Override\s*header:\s*/i; function getAuthorName(a) { return a.owner.display_name; } function process() { var valid = []; answers.forEach(function(a) { var body = a.body; a.comments.forEach(function(c) { if(OVERRIDE_REG.test(c.body)) body = '<h1>' + c.body.replace(OVERRIDE_REG, '') + '</h1>'; }); var match = body.match(SCORE_REG); if (match) valid.push({ user: getAuthorName(a), size: +match[2], language: match[1], link: a.share_link, }); else console.log(body); }); valid.sort(function (a, b) { var aB = a.size, bB = b.size; return aB - bB }); var languages = {}; var place = 1; var lastSize = null; var lastPlace = 1; valid.forEach(function (a) { if (a.size != lastSize) lastPlace = place; lastSize = a.size; ++place; var answer = jQuery("#answer-template").html(); answer = answer.replace("{{PLACE}}", lastPlace + ".") .replace("{{NAME}}", a.user) .replace("{{LANGUAGE}}", a.language) .replace("{{SIZE}}", a.size) .replace("{{LINK}}", a.link); answer = jQuery(answer); jQuery("#answers").append(answer); var lang = a.language; lang = jQuery('<a>'+lang+'</a>').text(); languages[lang] = languages[lang] || {lang: a.language, lang_raw: lang.toLowerCase(), user: a.user, size: a.size, link: a.link}; }); var langs = []; for (var lang in languages) if (languages.hasOwnProperty(lang)) langs.push(languages[lang]); langs.sort(function (a, b) { if (a.lang_raw > b.lang_raw) return 1; if (a.lang_raw < b.lang_raw) return -1; return 0; }); for (var i = 0; i < langs.length; ++i) { var language = jQuery("#language-template").html(); var lang = langs[i]; language = language.replace("{{LANGUAGE}}", lang.lang) .replace("{{NAME}}", lang.user) .replace("{{SIZE}}", lang.size) .replace("{{LINK}}", lang.link); language = jQuery(language); jQuery("#languages").append(language); } }</script>

Dennis

Posted 2015-09-11T14:06:29.040

Reputation: 196 637

Can I take inputs as negative numbers, where abs(input) would be the number I am testing? – Stan Strum – 2017-09-06T03:40:28.880

No, the input is a strictly positive integer. – Dennis – 2017-09-06T03:44:53.347

1

Is there a reason for the full program requirement, rather than allowing the full range of default input types? E.g. answering with a function that takes its input as an argument, is currently disallowed? https://codegolf.meta.stackexchange.com/questions/2447/default-for-code-golf-input-output-methods

– Lyndon White – 2017-12-12T06:21:55.663

2

@LyndonWhite This was intended as a catalog (like “Hello, World!”) of primality tests, so a unified submission format seemed preferable. It's one of two decisions about this challenge that I regret, the other being only allowing deterministic primality tests.

– Dennis – 2017-12-12T12:51:06.730

Could a case be made for locking this challenge and posting a new, less restrictive one? – Shaggy – 2018-06-25T12:59:32.587

1@Shaggy Seems like a question for meta. – Dennis – 2018-06-25T13:44:07.693

1Yeah, that's what I was thinking. I'll let you do the honours, seeing as it's your challenge. – Shaggy – 2018-06-25T13:45:55.260

Is it ok if an answer falsely says 1 is prime, or would this disqualify and answer? – The Matt – 2020-02-07T22:35:37.007

Answers

230

hello, world!, 13

hello, world!

histocrat

Posted 2015-09-11T14:06:29.040

Reputation: 20 600

84Did you, like, just create this language, just for this submission? ;) – ETHproductions – 2015-09-11T16:43:16.480

41@ETHproductions Looks like the latest commit was 10 days ago. – Geobits – 2015-09-11T16:46:38.447

1@Geobits Ah, my bad, I saw Sep 11 as the date. Still, that's an interesting language you got there. :) – ETHproductions – 2015-09-11T16:49:27.953

2Methinks the program should be hello, worl!, since 0 appears to be the only falsy value in the language. Also, it's shorter. ;) – DLosc – 2015-09-11T20:35:28.730

Also, I don't know if I'm doing something wrong, but my tweaked version on Ideone hangs when given an input of 1.

– DLosc – 2015-09-11T20:37:19.963

1It's true, if I'd created this language exactly to the spec in the question I'd've done it slightly differently. :p My version suspends judgment on 1 as well. – histocrat – 2015-09-11T20:40:13.253

39I was hoping to have the language in slightly better shape before linking to it anywhere, but this challenge was posted and I couldn't resist. – histocrat – 2015-09-11T20:40:40.123

5@histocrat The good news is, due to the rules of this particular question, you can update the language and use the new version. – DLosc – 2015-09-14T03:09:35.443

31I would almost say that hanging on an input of 1 is correct functionality. – Reinstate Monica -- notmaynard – 2015-09-14T13:45:12.913

5

@histocrat It would be cool if your language would get an entry in the hello world challenge http://codegolf.stackexchange.com/questions/55422/hello-world

– LukStorms – 2015-09-15T11:52:27.047

25The best part about this is that the program isn't just a built-in, each character plays its own part in getting the correct result. – ETHproductions – 2016-09-22T19:59:42.333

2How does this work? – None – 2017-04-06T00:10:20.127

1

@user67719 I guess you could read how on the GitHub repo readme.

– kamoroso94 – 2017-11-19T04:38:12.320

158

Hexagony, 29 bytes

.?'.).@@/'/.!.>+=(<.!)}($>(<%

The readable version of this code is:

   . ? ' .
  ) . @ @ /
 ' / . ! . >
+ = ( < . ! )
 } ( $ > ( <
  % . . . .
   . . . .

Explanation: It test if there is a number from 2 to n-1 who divides n.

Initialization:

Write n in one memory cell and n-1 in an other:

   . ? ' .
  . . . . .
 . . . . . .
+ = ( . . . .
 . . . . . .
  . . . . .
   . . . .

Special Case n=1:

Print a 0 and terminate

   . . . .
  . . . @ .
 . . . ! . .
. . . < . . .
 . . . . . .
  . . . . .
   . . . .

The loop

Calculate n%a and decrease a. Terminate if a=1 or n%a=0.

   . . . .
  ) . . . /
 ' / . . . >
. . . . . . .
 } ( $ > ( <
  % . . . .
   . . . .

Case a=1:

Increase a 0 to an 1, print it and terminate. (The instruction pointer runs in NE direction and loops from the eastern corner to the south western corner. And the $ makes sure it ignores the next command)

   . . . .
  . . . @ .
 . . . ! . .
. . . < . . )
 . . $ . . <
  . . . . .
   . . . .

Case a%n=0:

Print the 0 and terminate (The instruction pointer is running SW and loops to the top to the @

   . . . .
  . . @ . .
 . . . . . >
. . . . . ! .
 . . . . . .
  . . . . .
   . . . .

Etoplay

Posted 2015-09-11T14:06:29.040

Reputation: 3 161

61Holy crap, that's one impressive first post. :) I'll put up the bounty right now (I'll award it in 7 days, to draw some more attention to your answer). Welcome to PPCG! – Martin Ender – 2015-09-20T15:17:52.273

35Great answer! +1 for "The readable version of this code is: <...>" :-) – agtoever – 2015-09-21T07:05:19.973

68

Hexagony, 218 92 58 55 bytes

Notice: This answer has been solidly beaten with a side-length 4 solution by Etoplay.

)}?}.=(..]=}='.}.}~./%*..&.=&{.<......=|>(<..}!=...&@\[

The first ever non-trivial (i.e. non-linear) Hexagony program! It is based on the same squared-factorial approach as Sp3000's Labyrinth answer. After starting out with a hexagon of size 10, I managed to compress it down to size 5. However, I was able to reuse some duplicate code and there are still quite a bunch of no-ops in the code, so size 4 might just be possible.

Explanation

To make sense of the code, we first need to unfold it. Hexagony pads any source code to the next centred hexagonal number with no-ops (.), which is 61. It then rearranges the code into a regular hexagon of the corresponding size:

     ) } ? } .
    = ( . . ] =
   } = ' . } . }
  ~ . / % * . . &
 . = & { . < . . .
  . . . = | > ( <
   . . } ! = . .
    . & @ \ [ .
     . . . . .

This is quite heavily golfed with crossing and overlapping execution paths and multiple instruction pointers (IPs). To explain how it works, let's first look at an ungolfed version where control flow doesn't go through the edges, only one IP is used and the execution paths are as simple as possible:

             . . . . . . . . . . . . .
            . . . . . . . . . . . . . .
           . . . . . . . . . . . . . . .
          . . . . . . . . . . @ . . . . .
         . . . . . . . . . . ! . . . . . .
        . . . . . . . . . . % . . . . . . .
       . . . . . . . . . . ' . . . . . . . .
      . . . . . . . . . . & . . . . . . . . .
     . . . . . . . . . . { . . . . . . . . . .
    . . . . . . . . . . * . . . . . . . . . . .
   . . . . . . . . . . = . . . . . . . . . . . .
  . . . . . . . . . . } . . . . . . . . . . . . .
 ) } ? } = & { < . . & . . . . . . . . . . . . . .
  . . . . . . . > ( < . . . . . . . . . . . . . .
   . . . . . . = . . } . . . . . . . . . . . . .
    . . . . . } . . . = . . . . . . . . . . . .
     . . . . | . . . . | . . . . . . . . . . .
      . . . . * . . . ) . . . . . . . . . . .
       . . . . = . . & . . . . . . . . . . .
        . . . . > } < . . . . . . . . . . .
         . . . . . . . . . . . . . . . . .
          . . . . . . . . . . . . . . . .
           . . . . . . . . . . . . . . .
            . . . . . . . . . . . . . .
             . . . . . . . . . . . . .

Side note: the above code starts with executing the first line, which is full of no-ops. Then, when the IP hits the north east edge, it wraps to the left-most corner (the )), where the actual code begins.

Before we start, a word about Hexagony's memory layout. It's a bit like Brainfuck's tape on steroids. In fact, it's not a tape, but it's a hexagonal grid itself (an infinite one), where each edge has an integer value, which is initially 0 (and as opposed to standard Brainfuck, the values are signed arbitrary-precision integers). For this program, we'll be using four edges:

enter image description here

We'll compute the factorial on edge A, count down our input on edge C and store another copy of the input (for the modulo) on edge D. B is used as a temporary edge for computations.

The memory pointer (MP) starts out on edge A and points north (this is important for moving the MP around). Now here is the first bit of the code:

)}?}=&{

) increments edge A to 1 as the basis of the factorial. } makes the MP take a right-turn, i.e. move to edge C (pointing north-east). Here we read the input as an integer with ?. Then we take another right-turn to edge D with }. = reverses the MP, such that it points at the vertex shared with C. & copies the value from C (the input) into D - the value is copied from the left because the current value is non-positive (zero). Finally, we make the MP take a left-turn back to C with {.

Next, < is technically a branch, but we know that the current value is positive, so the IP will always turn right towards the >. A branch hit from the side acts as a mirror, such that the IP moves horizontally again, towards the (, which decrements the value in C.

The next branch, < is actually a branch now. This is how we loop from n-1 down to 1. While the current value in C is positive, the IP takes a right-turn (to execute the loop). Once we hit zero, it will turn left instead.

Let's look at the loop "body". The | are simple mirrors, the > and < are also used as mirrors again. That means the actual loop body boils down to

}=)&}=*}=

} moves the MP to edge B, = reverses its direction to face the vertex ABC. ) increments the value: this is only relevant for the first iteration, where the value of B is still zero: we want to ensure that it's positive, such that the next instruction & copies the right neighbour, i.e. A, i.e. the current value of the factorial computation, into B.

} then moves the MP to A, = reverses it again to face the common vertex. * multiplies both neighbours, i.e. edges B and C and stores the result in A. Finally, we have another }= to return to C, still facing the vertex ABC.

I hope you can see how this computes the factorial of n-1 in A.

So now we've done that, the loop counter in C is zero. We want to square the factorial and then take the modulo with the input. That's what this code does:

&}=*{&'%!@

Since C is zero, & copies the left neighbour, i.e. the factorial in A. }=* moves to B and stores the product of the two copies of the factorial (i.e. the square) in B. { moves back to C, but doesn't reverse the MP. We know that the current value is now positive, so & copies input from D into C. ' the MP backwards to the right, i.e. onto A. Remember, the square of the factorial is in B and the input is in C. So % computes (n-1)!^2 % n, exactly what we're looking for. ! prints the result as an integer (0 or 1) and @ terminates the program.


Okay, but that was the ungolfed version. What about the golfed version? You need to know two more things about Hexagony:

  1. The edges wrap around. If the IP hits an edge of the hexagon, it jumps to the opposite edge. This is ambiguous when the IP hits a corner straight on, so hitting a corner also acts as a branch: if the current value is positive, the IP jumps to the grid edge to its right, otherwise to the one to its left.
  2. There are actually 6 IPs. Each of them starts in a different corner, moving along the edge in the clockwise direction. Only one of them is active at a time, which means you can just ignore the other 5 IPs if you don't want them. You can switch to the next IP (in clockwise order) with ] and to the previous one with [. (You can also choose a specific one with #, but that's for another time.)

There are also a few new commands in it: \ and / are mirrors like |, and ~ multiplies the current value by -1.

So how does the ungolfed version translate to the golfed one? The linear set up code )}?}=&{ and the basic loop structure can be found here:

        ) } ? } .  ->
       . . . . . .
      . . . . . . .
     . . . . . . . .
->  . = & { . < . . .
     . . . . . > ( <
      . . . . . . .
       . . . . . .
        . . . . .

Now the loop body crosses the edges a few times, but most importantly, the actual computation is handed off to the previous IP (which starts at the left corner, moving north east):

        ) . . . .
       = . . . ] .
      } = . . } . .
     ~ . / . * . . .
    . . . . . . . . .
     . . . = . > ( <
      . . } . = . .
       . & . \ [ .
        . . . . .

After bouncing off the branch towards south east, the IP wraps around the edge to the two = in the top left corner (which, together, are a no-op), then bounces off the /. The ~ inverts the sign of the current value, which is important for subsequent iterations. The IP wraps around the same edge again and finally hits [ where control is handed over to the other IP.

This one now executes ~}=)&}=*} which undoes the negation and then just runs the ungolfed loop body (minus the =). Finally it hits ] which hands control back to the original IP. (Note that next time, we execute it this IP, it will start from where it left off, so it will first hit the corner. We need the current value to be negative in order for the IP to jump back to the north west edge instead of the south east one.)

Once the original IP resumes control, it bounces off the \, executes the remaining = and then hits > to feed into the next loop iteration.

Now the really crazy part: what happens when the loop terminates?

        ) . . . .
       . ( . . ] =
      . . ' . } . }
     . . . % * . . &
    . . . . . . . . .
     . . . = | . . <
      . . } ! . . .
       . & @ . . .
        . . . . .

The IP moves north east form the < and wraps around to the north east diagonal. So it ends up on the same execution path as the loop body (&}=*}]). Which is actually pretty cool, because that is exactly the code we want to execute at this point, at least if we add another =} (because }=} is equivalent to {). But how does this not actually enter the earlier loop again? Because ] changes to the next IP which is now the (so far unused) IP which starts in the top right corner, moving south west. From there, the IP continues along the edge, wraps to the top left corner, moves down the diagonal, bounces off the | and terminates at @ while executing the final bit of linear code:

=}&)('%!@

(The )( is a no-op of course - I had to add the ( because the ) was already there.)

Phew... what a mess...

Martin Ender

Posted 2015-09-11T14:06:29.040

Reputation: 184 808

Nice! How new is this? Also, you may wish to create an esolangs page whenever you get a stable release – mbomb007 – 2015-09-12T21:33:56.417

19@mbomb007 I implemented the language two days ago (and designed it over two or three days before that). And yes, I'll definitely add an esolangs page, but I think the spec isn't 100% stable yet (there are still 3 unassigned commands for instance). Once I feel that it's more stable, I'll add it to both esolangs and our meta post. – Martin Ender – 2015-09-12T21:36:10.340

Under the expanded hex is it wraps to the left-most corner (the 1). What 1 are you talking about there? – mbomb007 – 2015-09-13T04:15:47.650

@mbomb007 fixed. The ) used to be a 1. – Martin Ender – 2015-09-13T06:54:44.180

5+1 just for the level of detail in your explanation of how it works. If more languages came with an example that detailed more people could use them :D – jdarling – 2015-10-22T01:25:44.187

66

Pyth, 4 bytes

}QPQ

Prints True or False.

orlp

Posted 2015-09-11T14:06:29.040

Reputation: 37 067

12I know that this is old but now you can also do that like this: P_Q and save 1 byte. – drobilc – 2016-02-19T20:23:49.423

14It's now possible with P_ – Blue – 2016-07-22T18:27:20.920

3@drobilc You can cut the Q, as an EOF when the function is expecting an argument, it uses the input – Stan Strum – 2017-09-11T03:27:13.633

If we're adding solutions that now work, /P is an alternate 2 byte option. – isaacg – 2020-02-01T00:06:21.653

57

Retina, 16 bytes

^(?!(..+)\1+$)..

Try it online!

Let's start with a classic: detecting primes with a regex. Input should be given in unary, using any repeated printable character. The test suite includes a conversion from decimal to unary for convenience.

A Retina program consisting of a single line treats that line as a regex and prints the number of matches found in the input, which will be 0 for composite numbers and 1 for primes.

The lookahead ensures that the input is not composite: backtracking will try every possible substring (of at least 2 characters) for (..+), the lookahead then attempts to match the rest of the input by repeating what was captured here. If this is possible, that means the input has a divisor greater than 1, but which is less than itself. If that is the case the negative lookahead causes the match to fail. For primes there is no such possibility, and the match continues.

The only issue is that this lookahead also accepts 1, so we rule that out by matching at least two characters with ...

Martin Ender

Posted 2015-09-11T14:06:29.040

Reputation: 184 808

That's actually an irregular expression, since the primes do not form a regular language. – PyRulez – 2017-06-28T07:10:11.487

@PyRulez Most real-world regex flavours are a lot more powerful than the theoretical concept of regular expressions. I improved the wording though. – Martin Ender – 2017-06-28T07:11:24.243

1The most powerful "regex" engines out in the open right now have recognition power equal to that of a linear bounded automata. Standard issue regex, pattern recursion, unlimited lookhead, and unlimited lookbehind are all you need for context-sensitive parsing (though backreferences and such generally help with complicating efficient parsing), and some have them all. Don't even get me started on the engines that let you embed code into the regex. – eaglgenes101 – 2017-11-10T05:24:22.413

51

CJam, 4 bytes

qimp

CJam has a built-in operator for primality testing.

Peter Taylor

Posted 2015-09-11T14:06:29.040

Reputation: 41 901

18Alternatively: limp – Sp3000 – 2015-09-11T15:19:49.763

43pimp my cjam. – flawr – 2015-09-11T18:04:35.980

12pimp is objectively more pimp – MickLH – 2015-09-12T00:32:10.073

1You can also do l~mp – user41805 – 2015-09-13T08:46:11.497

1Wait, if CJam has a built in for primality testing, why is it 4 bytes? – Cyoce – 2016-03-20T05:42:39.287

12@Cyoce, q reads a line of input, i parses it as an integer, and mp is the built-in. CJam has two groups of two-char built-ins: the "extended" ones begin e and the "mathematical"ones begin m – Peter Taylor – 2016-03-20T07:16:28.947

1@flawr I believe (I forgot the source) the Cjam actually means CherryJam, which participated in Google's CodeJam contest. – Chromium – 2018-06-22T03:27:14.513

49

HTML+CSS, 254+nmax*28 bytes

We can check primality using regular expressions. Mozilla has @document, which is defined as:

@document [ <url> | url-prefix(<string>) | domain(<string>) | regexp(<string>) ]# {
  <group-rule-body>
}

To filter elements via CSS based on the current URL. This is a single pass, so we have to do two steps:

  1. Get input from the user. This input must somehow be reflected in the current URL.
  2. Reply to the user in as little code as possible.

1. Getting Input

The shortest way I can figure to get input and transfer that to the URL is a GET form with checkboxes. For the regex, we just need some unique string to count appearances.

So we start with this (61 bytes):

<div id=q><p id=r>1<p id=s>0</div><form method=GET action=#q>

We got two unique <p>s to indicate whether the entered number is a prime (1) or not (0). We also define the form and it's action.

Followed by nmax checkboxes with the same name (nmax*28 bytes):

<input type=checkbox name=i>

Followed by the submit element (34 bytes):

<input name=d value=d type=submit>

2. Display Answer

We need the CSS (159 bytes) to select the <p> to display (1 or 0):

#q,#s,#q:target{display:none}#q:target{display:block}@-moz-document regexp(".*\\?((i=on&)?|(((i=on&)(i=on&)+?)\\4+))d=d#q$"){#s{display:block}#r{display:none}}

» Try it at codepen.io (firefox only)

mınxomaτ

Posted 2015-09-11T14:06:29.040

Reputation: 7 398

13+1: This is my all time favourite abuse of HTML, and the kind of stuff that makes me love codegolf. – cat – 2015-12-29T01:40:40.520

This is certainly interesting, but I'm not sure if it satisfies the rules of this challenge. In particular, I don't think it complies with Your algorithm [...] should, in theory, work for arbitrarily large integers. Couldn't you use a regex on the value of an input field as well? – Dennis – 2015-12-29T02:16:36.140

@Dennis Maybe. Probably even. But that wouldn't solve the problem you mentioned. I leave it here as a non-competing entry, because this is the most on-topic challenge for that. – mınxomaτ – 2015-12-29T02:20:52.360

Why not? If you have a number in unary in an input field, your code would no longer depend on the maximum number. – Dennis – 2015-12-29T02:22:17.977

6Hm, I thought about this a bit more, and there's really no difference between having only x checkboxes and an interpreter that has only y-bit numbers. Disregard my previous comment. – Dennis – 2016-08-06T23:17:34.737

A text input taking a string in unary would solve the nmax issue and allow you to count the number of bytes. It would also allow for theoretically arbitrarily high values (based on maximum URL length, which is a browser implementation detail). – Robert Fraser – 2017-03-09T10:32:43.820

46

Help, WarDoq!, 1 byte

P

Outputs 1 if the input is prime, 0 otherwise.

orlp

Posted 2015-09-11T14:06:29.040

Reputation: 37 067

42

Hexagony, 28 bytes

Since Etoplay absolutely trounced me on this question, I felt that I had to outgolf his only other answer.

?\.">"!*+{&'=<\%(><.*.'(@>'/

Try it online!

I use Wilson's Theorem, like Martin did in his answer: Given n, I output (n-1!)² mod n

Here it the program unfolded:

   ? \ . "
  > " ! * +
 { & ' = < \
% ( > < . * .
 ' ( @ > ' /
  . . . . .
   . . . .

And here is the readable version:

Very readable

Explanation:

The program has three main steps: Initialisation, Factorial and Output.

Hexagony's memory model is an infinite hexagonal grid. I am using 5 memory locations, as shown in this diagram:

Memory

I will be referring to these locations (and the Integers stored in them) by their labels on that diagram.

Initialisation:

Initialisation

The instruction pointer (IP) starts at the top left corner, going East. The memory pointer (MP) starts at IN.

First, ? reads the number from input and stores it in IN. The IP stays on the blue path, reflected by \. The sequence "&( moves the MP back and to the left (to A), copies the value from IN to A and decrements it.

The IP then exits one side of the hexagon and re-enters the other side (onto the green path). It executes '+ which moves the MP to B and copies what was in A. < redirects the IP to West.

Factorial:

I compute the factorial in a specific way, so that squaring it is easy. I store n-1! in both B and C as follows.

Factorial

The instruction pointer starts on the blue path, heading East.

=' reverses the direction of the MP and moves it backwards to C. This is equivalent to {= but having the = where it is was helpful later.

&{ copies the value from A to C, then moves the MP back to A. The IP then follows the green path, doing nothing, before reaching the red path, hitting \ and going onto the orange path.

With (>, we decrement A and redirect the IP East. Here it hits a branch: <. For positive A, we continue along the orange path. Otherwise the IP gets directed North-East.

'* moves the MP to B and stores A * C in B. This is (n-1)*(n-2) where the initial input was n. The IP then enters back into the initial loop and continues decrementing and multiplying until A reaches 0. (computing n-1!)

N.B: On following loops, & stores the value from B in C, as C has a positive value stored in it now. This is crucial to computing factorial.

Output:

Output

When A reaches 0. The branch directs the IP along the blue path instead.

=* reverses the MP and stores the value of B * C in A. Then the IP exits the hexagon and re-enters on the green path; executing "%. This moves the MP to OUT and calculates A mod IN, or (n-1!)² mod n.

The following {" acts as a no-op, as they cancel each-other out. ! prints the final output and *+'( are executed before termination: @.

After execution, (with an input of 5) the memory looks like this:

Memory2

The beautiful images of the control flow were made using Timwi's Hexagony Coloror.

Thank you to Martin Ender for generating all of the images, as I couldn't do it on my PC.

H.PWiz

Posted 2015-09-11T14:06:29.040

Reputation: 10 962

What are you using for these memory diagrams? I've seen Esoteric IDE but I couldn't get it to run... – NieDzejkob – 2017-11-14T15:43:45.383

@NieDzejkob you're better off asking Martin in chat , since he made them for me anyway. – H.PWiz – 2017-11-14T16:08:05.727

@NieDzejkob Yeah, the memory diagram can be exported from the EsoIDE. https://chat.stackexchange.com/rooms/27364/esoteric-programming-languages if you want to chat about this some more.

– Martin Ender – 2017-11-14T18:13:33.430

35

Mornington Crescent, 2448 bytes

We're back in London!

Take Northern Line to Bank
Take Circle Line to Bank
Take District Line to Parsons Green
Take District Line to Bank
Take Circle Line to Hammersmith
Take District Line to Upney
Take District Line to Hammersmith
Take Circle Line to Victoria
Take Victoria Line to Seven Sisters
Take Victoria Line to Victoria
Take Circle Line to Victoria
Take Circle Line to Bank
Take Circle Line to Hammersmith
Take Circle Line to Cannon Street
Take Circle Line to Hammersmith
Take Circle Line to Cannon Street
Take Circle Line to Bank
Take Circle Line to Hammersmith
Take Circle Line to Aldgate
Take Circle Line to Aldgate
Take Metropolitan Line to Chalfont & Latimer
Take Metropolitan Line to Aldgate
Take Circle Line to Hammersmith
Take District Line to Upminster
Take District Line to Hammersmith
Take Circle Line to Notting Hill Gate
Take Circle Line to Hammersmith
Take Circle Line to Notting Hill Gate
Take District Line to Upminster
Take District Line to Bank
Take Circle Line to Victoria
Take Circle Line to Temple
Take Circle Line to Aldgate
Take Circle Line to Aldgate
Take Metropolitan Line to Chalfont & Latimer
Take Metropolitan Line to Pinner
Take Metropolitan Line to Chalfont & Latimer
Take Metropolitan Line to Pinner
Take Metropolitan Line to Chalfont & Latimer
Take Metropolitan Line to Pinner
Take Metropolitan Line to Aldgate
Take Circle Line to Hammersmith
Take District Line to Upminster
Take District Line to Victoria
Take Circle Line to Aldgate
Take Circle Line to Victoria
Take Circle Line to Victoria
Take District Line to Upminster
Take District Line to Embankment
Take Circle Line to Embankment
Take Northern Line to Angel
Take Northern Line to Moorgate
Take Metropolitan Line to Chalfont & Latimer
Take Metropolitan Line to Aldgate
Take Circle Line to Aldgate
Take Circle Line to Cannon Street
Take District Line to Upney
Take District Line to Cannon Street
Take District Line to Acton Town
Take District Line to Acton Town
Take Piccadilly Line to Russell Square
Take Piccadilly Line to Hammersmith
Take Piccadilly Line to Russell Square
Take Piccadilly Line to Ruislip
Take Piccadilly Line to Ruislip
Take Metropolitan Line to Preston Road
Take Metropolitan Line to Aldgate
Take Circle Line to Aldgate
Take Circle Line to Cannon Street
Take Circle Line to Aldgate
Take Circle Line to Aldgate
Take Metropolitan Line to Preston Road
Take Metropolitan Line to Moorgate
Take Circle Line to Moorgate
Take Northern Line to Mornington Crescent

Timwi was so kind to implement the control flow stations Temple and Angel in Esoteric IDE as well as add input and integer parsing to the language specification.

This one is probably better golfed than the "Hello, World!", because this time I wrote a CJam script to help me find the shortest path between any two stations. If you want to use it (although I don't know why anyone would want to...), you can use the online interpreter. Paste this code:

"Mornington Crescent"
"Cannon Street"
]qN/{'[/0=,}$:Q;{Q{1$#!}=\;_oNo'[/1>{']/0="[]"\*}%}%:R;NoQ{R\f{f{\#)}:+}:*},N*

Here the first two lines are the stations you want to check. Also, paste the contents of this pastebin into the input window.

The output will show you which lines are available at the two stations, and then a list of all stations which connect the two, sorted by the length of the station names. It shows all of them, because sometimes it's better to use a longer name, either because it allows a shorter line, or because the station is special (like Bank or Temple) so that you want to avoid it. There are some edge cases where two stations aren't connected by any single other station (notably, the Metropolitan and District lines never cross), in which case you'll have to figure out something else. ;)

As for the actual MC code, it's based on the squared-factorial approach as many other answers because MC has multiplication, division and modulo. Also, I figured that a single loop would be convenient.

One issue is that the loops are do-while loops, and decrementing and incrementing is expensive, so I can't easily compute (n-1)! (for n > 0). Instead, I'm computing n! and then divide by n at the end. I'm sure there is a better solution for this.

When I started writing this, I figured that storing -1 in Hammersmith would be a good idea so I can decrement more cheaply, but in the end this may have cost more than it saved. If I find the patience to redo this, I might try just keeping a -1 around in Upminster instead so I can use Hammersmith for something more useful.

Martin Ender

Posted 2015-09-11T14:06:29.040

Reputation: 184 808

11Is London turing complete? – Rohan Jhunjhunwala – 2016-07-12T01:38:29.037

1

@RohanJhunjhunwala probably

– Martin Ender – 2016-07-12T06:47:45.090

1Wow! I love seeing well thought out questions. I especially love seeing questions where you have to write a program to write a program. – Rohan Jhunjhunwala – 2016-07-12T12:41:18.330

27

Brachylog (V2), 1 byte

Try it online!

Brachylog (V1), 2 bytes

#p

This uses the built-in predicate #p - Prime, which constrains its input to be a prime number.

Brachylog is my attempt at making a Code Golf version of Prolog, that is a declarative code golf language that uses backtracking and unification.

Alternate solution with no built-in: 14 bytes

ybbrb'(e:?r%0)

Here is a breakdown of the code above:

y            The list [0, …, Input]
bbrb         The list [2, …, Input - 1]
'(           True if what's in the parentheses cannot be proven; else false
     e           Take an element from the list [2, …, Input - 1]
     :?r%0       The remainder of the division of the Input but that element is 0
)

Fatalize

Posted 2015-09-11T14:06:29.040

Reputation: 32 976

1You might want to edit the Brachylog 2 version of this into the post, too, now that the syntax is a byte shorter. – None – 2017-05-11T16:10:10.207

1@ais523 True, done. – Fatalize – 2017-05-11T16:15:12.197

Does the Brachylog 2 answer postdate the challenge? – Scott Milner – 2017-05-12T16:30:38.153

1@ScottMilner Yes, but this is explicitely allowed in this challenge: "Unlike our usual rules, feel free to use a language (or language version) even if it's newer than this challenge" – Fatalize – 2017-05-12T16:34:35.127

26

Haskell, 49 bytes

Using xnor's Corollary to Wilson's Theorem:

main=do n<-readLn;print$mod(product[1..n-1]^2)n>0

Lynn

Posted 2015-09-11T14:06:29.040

Reputation: 55 648

Wouldn't it be shorter to do main=interact$\n-> ...? – John Dvorak – 2015-09-20T06:57:16.767

2Counterintuitively, no! Consider that you'd need interact...read in there somewhere, which makes it a lot longer than just readLn. Often do notation can be more concise than you might expect, especially when the alternative is a lambda. – Lynn – 2015-09-20T20:34:12.990

24

Labyrinth, 29 bytes

1
?
:
}  +{%!@
(:'(
 } {
 :**

Reads an integer from STDIN and outputs ((n-1)!)^2 mod n. Wilson's theorem is pretty useful for this challenge.

The program starts at the top-left corner, beginning with 1 which multiplies the top of the stack by 10 and adds 1. This is Labyrinth's way of building large numbers, but since Labyrinth's stacks are filled with zeroes, the end effect is as though we just pushed a 1.

? then reads n from STDIN and : duplicates it. } shifts n to the auxiliary stack, to be used at the end for the modulo. ( then decrements n, and we are ready to begin calculating the squared factorial.

Our second : (duplicate) is at a junction, and here Labyrinth's control flow features come into play. At a junction after an instruction is executed, if the top of the stack is positive we turn right, for negative we turn left and for zero we go straight ahead. If you try to turn but hit a wall, Labyrinth makes you turn in the other direction instead.

For n = 1, since the top of the stack is n decremented, or 0, we go straight ahead. We then hit a no-op ' followed by another decrement ( which puts us at -1. This is negative, so we turn left, executing + plus (-1 + 0 = -1), { to shift n back from the auxiliary stack to the main and % modulo (-1 % 1 = 0). Then we output with ! and terminate with @.

For n > 1, at the second : we turn right. We then shift } our copied loop counter to the auxiliary stack, duplicate : and multiply twice **, before shifting the counter back { and decrementing (. If we're still positive we try to turn right but can't, so Labyrinth makes us turn left instead, continuing the loop. Otherwise, the top of the stack is our loop counter which has been reduced to 0, which we + add to our calculated ((n-1)!)^2. Finally, we shift n back with { then modulo %, output ! and terminate @.

I said that ' is a no-op, but it can also be used for debugging. Run with the -d flag to see the state of the stack every time the ' is passed over!

Sp3000

Posted 2015-09-11T14:06:29.040

Reputation: 58 729

2Squaring the factorial is a really cool trick :) – Lynn – 2015-09-11T18:06:40.610

@Mauris Thanks! I need to give credit where it is due though - I first saw the trick used by xnor here

– Sp3000 – 2015-09-11T18:16:44.553

5Yay, the first Labyrinth answer not written by me! :) – Martin Ender – 2015-09-11T18:48:06.147

24

Bash + GNU utilities, 16

  • 4 bytes saved thanks to @Dennis

  • 2 bytes saved thanks to @Lekensteyn

factor|awk NF==2

Input is one line taken from STDIN. Output is empty string for falsey and non-empty string for truthy. E.g.:

$ ./pr.sh <<< 1
$ ./pr.sh <<< 2
2: 2
$ ./pr.sh <<< 3
3: 3
$ ./pr.sh <<< 4
$

Digital Trauma

Posted 2015-09-11T14:06:29.040

Reputation: 64 644

2Cool, learned about another coreutil. Your can strip two characters by counting the number of fields: factor|awk NF==2 – Lekensteyn – 2015-09-18T11:03:55.843

@Lekensteyn - thanks - for some reason I missed your comment before :) – Digital Trauma – 2016-05-24T19:30:36.223

I was going to post something somewhat similar, only longer and without AWK. Nicely done. – David Conrad – 2017-02-06T19:16:17.120

21

Java, 126 121 bytes

I guess we need a Java answer for the scoreboard... so here's a simple trial division loop:

class P{public static void main(String[]a){int i=2,n=Short.valueOf(a[0]);for(;i<n;)n=n%i++<1?0:n;System.out.print(n>1);}}

As usual for Java, the "full program" requirement makes this much larger than it would be if it were a function, due mostly to the main signature.

In expanded form:

class P{
    public static void main(String[]a){
        int i=2,n=Short.valueOf(a[0]);
        for(;i<n;)
            n=n%i++<1?0:n;
        System.out.print(n>1);
    }
}

Edit: Fixed and regolfed by Peter in comments. Thanks!

Geobits

Posted 2015-09-11T14:06:29.040

Reputation: 19 061

Buggy: it reports that 1 is prime. Otherwise there'd be a 4-char saving by removing p and saying for(;i<n;)n=n%i++<1?0:n;System.out.print(n>0); – Peter Taylor – 2015-09-11T20:32:45.243

2OTOH class P{public static void main(String[]a){int i=2,n=Short.valueOf(a[0]);for(;i<n;)n=n%i++<1?0:n;System.out.print(n>1);}} works – Peter Taylor – 2015-09-11T20:53:19.000

6changing line 3 to 'long i=2,n=Long.valueOf(a[0]);` results in no change in length but a wider range of valid inputs. – President James Moveon Polk – 2015-09-17T21:02:59.990

4Instead of .valueOf you can use new, as in new Short(a[0]), or new Long(a[0]), which is a bit shorter. – ECS – 2016-01-25T08:55:10.740

3You can save 4 bytes by using an interface and dropping the public modifier. – RamenChef – 2017-12-13T19:28:15.440

19

Brain-Flak, 112 108 bytes

({}[()]){((({})())<>){{}<>(({}<(({}[()])()<>)>)<>)<>{({}[()]<({}[()]<({}())>)>{(<()>)}{})}{}{}}}<>{{}}([]{})

Try it online!

How it works

Initially, the first stack will contain a positive integer n, the second stack will be empty.

We start by decrementing n as follows.

(
  {}      Pop n.
  [()]    Yield -1.
)       Push n - 1.

n = 1

If n = 1 is zero, the while loop

{
  ((({})())<>)
  {
    {}<>(({}<(({}[()])()<>)>)<>)<>{({}[()]<({}[()]<({}())>)>{(<()>)}{})}{}{}
  }
}

is skipped entirely. Finally, the remaining code is executed.

<>    Switch to the second stack (empty).
{}    Pop one of the infinite zeroes at the bottom.
{<>}  Switch stacks while the top on the active stack is non-zero. Does nothing.
(
  []    Get the length of the active stack (0).
  {}    Pop another zero.
)     Push 0 + 0 = 0.

n > 1

If n - 1 is non-zero, we enter the loop that n = 1 skips. It isn't a "real" loop; the code is only executed once. It achieves the following.

{                   While the top of the active stack is non-zero:
  (
    (
      ({})                Pop and push n - 1.
      ()                  Yield 1.
    )                   Push n - 1 + 1 = n.
    <>                  Switch to the second stack. Yields 0.
  )                   Push n + 0 = n.
                      We now have n and k = n - 1 on the first stack, and n on
                      the second one. The setup stage is complete and we start
                      employing trial division to determine n's primality.
  {                   While the top of the second stack is non-zero:
    {}                  Pop n (first run) or the last modulus (subsequent runs),
                        leaving the second stack empty.
    <>                  Switch to the first stack.
    (
      (
        {}                  Pop n from the first stack.
        <
          (
            (
              {}              Pop k (initially n - 1) from the first stack.
              [()]            Yield -1.
            )               Push k - 1 to the first stack.
            ()              Yield 1.
            <>              Switch to the second stack.
          )               Push k - 1 + 1 = k on the second stack.
        >               Yield 0.
      )               Push n + 0 = n on the second stack.
      <>              Switch to the first stack.
    )               Push n on the first stack.
    <>              Switch to the second stack, which contains n and k.
                    The first stack contains n and k - 1, so it is ready for
                    the next iteration.
    {({}[()]<({}[()]<({}())>)>{(<()>)}{})}{}{}  Compute and push n % k.
  }               Stop if n % k = 0.
}               Ditto.

n % k is computed using the 42-byte modulus algorithm from my divisibility test answer.

Finally, we interpret the results to determine n's primality.

<>    Switch to the first stack, which contains n and k - 1, where k is the
      largest integer that is smaller than n and divides n evenly.
      If (and only if) n > 1 is prime, k = 1 and (thus) k - 1 = 0.
{     While the top of the first stack is non-zero:
  {}    Pop it.
}     This pops n if n is prime, n and k - 1 if n is composite.
(
  []    Yield the height h of the stack. h = 1 iff n is prime).
  {}    Pop 0.
)     Push h + 0 = h.

Dennis

Posted 2015-09-11T14:06:29.040

Reputation: 196 637

2You don't need to pop the last 0 on the stack, since the truthy 1 on top is enough; you can save two bytes that way by removing the last {}. – Steven H. – 2016-10-05T23:18:33.700

1Hm, I'm torn. On one hand, the question says that output should consist solely of a truthy or falsy value and 1 0 is two values. On the other hand, we'd accept arrays as long as the language considers them truthy or falsy and multiple stack items is the closest thing Brain-Flak has to arrays. Might be worth taking this to meta. – Dennis – 2016-10-06T00:39:53.143

I've verified with the creator of Brain-Flak that 1 0 is truthy. http://chat.stackexchange.com/transcript/message/32746241#32746241

– Steven H. – 2016-10-06T01:01:43.453

3

Relevant meta discussion: Truthiness in Brain-Flak and similar stack-based languages

– Dennis – 2016-10-06T03:59:07.987

18

R, 37 29 bytes

n=scan();cat(sum(!n%%1:n)==2)

Uses trial division. scan() reads an integer from STDIN and cat() writes to STDOUT.

We generate a vector of length n consisting of the integers 1 to n modulo n. We test whether each is 0 by negating (!), which returns a logical value that's true when the number is 0 and false when it's greater than 0. The sum of a logical vector is the number of true elements, and for prime numbers we expect the only nonzero moduli to be 1 and n, thus we expect the sum to be 2.

Saved 8 bytes thanks to flodel!

Alex A.

Posted 2015-09-11T14:06:29.040

Reputation: 23 761

With f=function(x)sum(!x%%1:x)==2 you can do it in 28 bytes. – Mutador – 2015-12-21T16:35:35.730

2@AndréMuta For this challenge, all submissions must be full programs rather than just functions. Thanks for the suggestion though. – Alex A. – 2015-12-21T20:13:08.407

17

TI-BASIC, 12 bytes

2=sum(not(fPart(Ans/randIntNoRep(1,Ans

Pretty straightforward. randIntNoRep( gives a random permutation of all integers from 1 to Ans.

This bends the rules a little; because lists in TI-BASIC are limited to 999 elements I interpreted

assume that the input can be stored in your data type

as meaning that all datatypes can be assumed to accommodate the input. OP agrees with this interpretation.

A 17-byte solution which actually works up to 10^12 or so:

2=Σ(not(fPart(Ans/A)),A,1,Ans

lirtosiast

Posted 2015-09-11T14:06:29.040

Reputation: 20 331

@toothbrush TI-BASIC is a tokenized language, so every token here is one byte, except for randIntNoRep( which is two.

– lirtosiast – 2015-09-13T19:28:52.313

+1 Ah, I've never seen TL-BASIC before. Thanks for letting me know – Toothbrush – 2015-09-13T19:29:48.437

1Although, it is a bit unfair, isn't it...? I should write a golfing language that requires only 1-4 bytes (the question ID), and then the parameters. It will pick the top answer in a language it understands, execute it (passing any parameters), and return the result... I wonder if that's breaking the rules? :-) – Toothbrush – 2015-09-13T19:30:22.093

@toothbrush In TI-BASIC's defense: to the interpreter, it's no more unfair than Pyth and CJam's one-character commands, and TI-BASIC is more readable. – lirtosiast – 2015-09-13T19:34:13.620

1

True. I dislike those kinds of languages, since solutions in almost every other language are longer... although I recently beat CJam with VB6! :-]

– Toothbrush – 2015-09-13T19:35:42.800

@toothbrush that would most definitely break the rule about using resources created after the start of a challenge. --Note that athough they are overall best, many languages have beaten CJam/Pyth.

– lirtosiast – 2015-09-14T02:42:54.460

I should probably upgrade my OS just to get access to new commands like the one you used in the second solution. Well done. +1 – Zenohm – 2015-09-14T15:39:29.407

@Toothbrush, that would be breaking the rules, bigtime. You might be able to get away with it if you give the submissions WITH the language, rather than accessing the internet. – Zacharý – 2017-08-24T00:27:20.413

@Zacharý Yes, it was a joke. The comment is almost 2 years old now, too. – Toothbrush – 2017-08-24T01:23:31.790

@Zacharý And when I posted that comment – to the best of my knowledge – such a thing wasn't forbidden by any rule. – Toothbrush – 2017-08-24T01:28:50.710

16

TI-BASIC, 24 bytes

Note that TI-Basic programs use a token system, so counting characters does not return the actual byte value of the program.

Upvote Thomas Kwa's answer, it is superior.

:Prompt N
:2
:While N≠1 and fPart(N/Ans
:Ans+1
:End
:N=Ans

Sample:

N=?1009
                         1
N=?17
                         1
N=?1008
                         0
N=?16
                         0

Now returns 0 if not a prime, or 1 if it is.

Zenohm

Posted 2015-09-11T14:06:29.040

Reputation: 261

4Isn't the square root just an optimisation that you don't actually need for the program to be correct? – Martin Ender – 2015-09-11T15:24:42.277

Why would you need to divide by two? – Geobits – 2015-09-11T15:29:53.927

I always love TI-BASIC answers. – Grant Miller – 2018-02-27T01:05:30.437

Why choose Prompt over Input? They're the same number of bytes, and when it's a single variable Input would be more idomatic... – Hello Goodbye – 2019-12-28T23:17:15.843

@HelloGoodbye Because the UI is better for free, if I recall my reasoning correctly. – Zenohm – 2020-02-08T01:44:07.083

Ok, seems legit – Hello Goodbye – 2020-02-10T15:42:35.433

16

JavaScript, 39 36 bytes

Saved 3 bytes thanks to ETHproductions:

for(i=n=prompt();n%--i;);alert(1==i)

Displays true for a prime, false otherwise.

The for loop tests every number i from n-1 until i is a divisor. If the first divisor found is 1 then it's a prime number.


Previous solution (39 bytes):

for(i=n=prompt();n%--i&&i;);alert(1==i)

How was left an unneeded test:

for(i=2,n=prompt();n%i>0&&i*i<n;i++);alert(n%i>0) //49: Simple implementation: loop from 2 to sqrt(n) to test the modulo.
for(i=2,n=prompt();n%i>0&&i<n;i++);alert(n==i)    //46: Replace i*i<n by i<n (loop from 2 to n) and replace n%i>0 by n==i
for(i=2,n=prompt();n%i&&i<n;i++);alert(n==i)      //44: Replace n%i>0 by n%i
for(i=2,n=prompt();n%i&&i++<n;);alert(n==i)       //43: Shorten loop increment
for(i=n=prompt();n%--i&&i>0;);alert(1==i)         //41: Loop from n to 1. Better variable initialization.
for(i=n=prompt();n%--i&&i;);alert(1==i)           //39: \o/ Replace i>0 by i

I only posted the 39 bytes solution because the best JavaScript answer was already 40 bytes.

Hedi

Posted 2015-09-11T14:06:29.040

Reputation: 1 857

2Welcome to Programming Puzzles & Code Golf! – Dennis – 2016-08-27T00:48:15.880

2Great answer! The &&i doesn't actually do anything in this program, so you can remove it. – ETHproductions – 2016-09-24T01:48:49.977

should add n>1 to the final condition though, if You don´t want 1 to be prime. – Titus – 2016-10-17T13:08:01.993

1@Titus If the input is 1 the for loop will do n%--i once : 1%0 returns NaN and stops the loop. When alert is called i is already equal to 0 so 1==i returns false. – Hedi – 2016-10-17T20:21:09.057

2i<2 (and some text) – Scheintod – 2018-03-16T08:26:18.813

15

Ruby, 15 + 8 = 23 bytes

p$_.to_i.prime?

Sample run:

bash-4.3$ ruby -rprime -ne 'p$_.to_i.prime?' <<< 2015
false

manatwork

Posted 2015-09-11T14:06:29.040

Reputation: 17 865

Heheh, I knew there would be a builtin in Ruby somewhere, but I couldn't be bothered to look for it, so I answered in C. +1. – Level River St – 2015-09-11T15:01:26.403

@steveverrill, I knew it because was a big help for Project Euler. – manatwork – 2015-09-11T15:03:30.550

15

PARI/GP, 21 bytes

print(isprime(input))

Works for ridiculously big inputs, because this kind of thing is what PARI/GP is made for.

Lynn

Posted 2015-09-11T14:06:29.040

Reputation: 55 648

6isprime does an APR-CL primality proof, so does slow down quite a bit as inputs get very large. ispseudoprime(input) does an AES BPSW probable prime test, which will be much faster for over 100 digits. Still no known counterexamples after 35 years. Version 2.1 and earlier of Pari, from pre-2002, uses a different method that can easily give false results, but nobody should be using that. – DanaJ – 2015-09-11T18:30:43.327

15

Stack Cats, 62 + 4 = 66 bytes

*(>:^]*(*>{<-!<:^>[:((-<)<(<!-)>>-_)_<<]>:]<]]}*<)]*(:)*=<*)>]

Needs to be run with the -ln command-line flags (hence +4 bytes). Prints 0 for composite numbers and 1 for primes.

Try it online!

I think this is the first non-trivial Stack Cats program.

Explanation

A quick Stack Cats introduction:

  • Stack Cats operates on an infinite tape of stacks, with a tape head pointing at a current stack. Every stack is initially filled with an infinite amount of zeros. I will generally ignore these zeros in my wording, so when I say "the bottom of stack" I mean the lowest non-zero value and if I say "the stack is empty" I mean there's only zeros on it.
  • Before the program starts, a -1 is pushed onto the initial stack, and then the entire input is pushed on top of that. In this case, due to the -n flag, the input is read as a decimal integer.
  • At the end of the program, the current stack is used for output. If there's a -1 at the bottom, it will be ignored. Again, due to the -n flag, the values from the stack are simply printed as linefeed-separated decimal integers.
  • Stack Cats is a reversible program language: every piece of code can be undone (without Stack Cats keeping track of an explicit history). More specifically, to reverse any piece of code, you simply mirror it, e.g. <<(\-_) becomes (_-/)>>. This design goal places fairly severe restrictions on what kinds of operators and control flow constructs exist in the language, and what sorts of functions you can compute on the global memory state.
  • To top it all off, every Stack Cats program has to be self-symmetric. You might notice that this is not the case for the above source code. This is what the -l flag is for: it implicitly mirrors the code to the left, using the first character for the centre. Hence the actual program is:

    [<(*>=*(:)*[(>*{[[>[:<[>>_(_-<<(-!>)>(>-)):]<^:>!->}<*)*[^:<)*(>:^]*(*>{<-!<:^>[:((-<)<(<!-)>>-_)_<<]>:]<]]}*<)]*(:)*=<*)>]
    

Programming effectively with the entire code is highly non-trivial and unintuitive and haven't really figured out yet how a human can possibly do it. We've brute forced such program for simpler tasks, but wouldn't have been able to get anywhere near that by hand. Luckily, we've found a basic pattern which allows you to ignore one half of the program. While this is certainly suboptimal, it's currently the only known way to program effectively in Stack Cats.

So in this answer, the template of said pattern is this (there's some variability in how it's executed):

[<(...)*(...)>]

When the program starts, the stack tape looks like this (for input 4, say):

     4    
... -1 ...
     0
     ^

The [ moves the top of the stack to the left (and the tape head along) - we call this "pushing". And the < moves the tape head alone. So after the first two commands, we've got this situation:

...   4 -1 ...
    0 0  0
    ^

Now the (...) is a loop which can be used quite easily as a conditional: the loop is entered and left only when the top of the current stack is positive. Since, it's currently zero, we skip the entire first half of the program. Now the centre command is *. This is simply XOR 1, i.e. it toggles the least significant bit of the top of the stack, and in this case turns the 0 into a 1:

... 1 4 -1 ...
    0 0  0
    ^

Now we encounter the mirror image of the (...). This time the top of the stack is positive and we do enter the code. Before we look into what goes on inside the parentheses, let me explain how we'll wrap up at the end: we want to ensure that at the end of this block, we have the tape head on a positive value again (so that the loop terminates after a single iteration and is used simply as a linear conditional), that the stack to the right holds the output and that the stack right of that holds a -1. If that's the case, we do leave the loop, > moves onto the output value and ] pushes it onto the -1 so we have a clean stack for output.

That's that. Now inside the parentheses we can do whatever we want to check the primality as long as we ensure that we set things up as described in the previous paragraph at the end (which can easily done with some pushing and tape head moving). I first tried solving the problem with Wilson's theorem but ended up well over 100 bytes, because the squared factorial computation is actually quite expensive in Stack Cats (at least I haven't found a short way). So I went with trial division instead and that indeed turned out much simpler. Let's look at the first linear bit:

>:^]

You've already seen two of those commands. In addition, : swaps the top two values of the current stack and ^ XORs the second value into the top value. This makes :^ a common pattern to duplicate a value on an empty stack (we pull a zero on top of the value and then turn the zero into 0 XOR x = x). So after this, section our tape looks like this:

         4    
... 1 4 -1 ...
    0 0  0
         ^

The trial division algorithm I've implemented doesn't work for input 1, so we should skip the code in that case. We can easily map 1 to 0 and everything else to positive values with *, so here's how we do that:

*(*...)

That is we turn 1 into 0, skip a big part of the code if we get indeed 0, but inside we immediately undo the * so that we get our input value back. We just need to make sure again that we end on a positive value at the end of the parentheses so that they don't start looping. Inside the conditional, we move one stack right with the > and then start the main trial division loop:

{<-!<:^>[:((-<)<(<!-)>>-_)_<<]>:]<]]}

Braces (as opposed to parentheses) define a different kind of loop: it's a do-while loop, meaning it always runs for at least one iteration. The other difference is the termination condition: when entering the loop Stack Cat remembers the top value of the current stack (0 in our case). The loop will then run until this same value is seen again at the end of an iteration. This is convenient for us: in each iteration we simply compute the remainder of the next potential divisor and move it onto this stack we're starting the loop on. When we find a divisor, the remainder is 0 and the loop stops. We will try divisors starting at n-1 and then decrement them down to 1. That means a) we know this will terminate when we reach 1 at the latest and b) we can then determine whether the number is prime or not by inspecting the last divisor we tried (if it's 1, it's a prime, otherwise it isn't).

Let's get to it. There's a short linear section at the beginning:

<-!<:^>[:

You know what most of those things do by now. The new commands are - and !. Stack Cats does not have increment or decrement operators. However it has - (negation, i.e. multiply by -1) and ! (bitwise NOT, i.e. multiply by -1 and decrement). These can be combined into either an increment, !-, or decrement -!. So we decrement the copy of n on top of the -1, then make another copy of n on the stack to the left, then fetch the new trial divisor and put it beneath n. So on the first iteration, we get this:

      4       
      3       
... 1 4 -1 ...
    0 0  0
      ^

On further iterations, the 3 will replaced with the next test divisor and so on (whereas the two copies of n will always be the same value at this point).

((-<)<(<!-)>>-_)

This is the modulo computation. Since loops terminate on positive values, the idea is to start from -n and repeatedly add the trial divisor d to it until we get a positive value. Once we do, we subtract the result from d and this gives us the remainder. The tricky bit here is that we can't just have put a -n on top of the stack and start a loop that adds d: if the top of the stack is negative, the loop won't be entered. Such are the limitations of a reversible programming language.

So to circumvent this issue, we do start with n on top of the stack, but negate it only on the first iteration. Again, that sounds simpler than it turns out to be...

(-<)

When the top of the stack is positive (i.e. only on the first iteration), we negate it with -. However, we can't just do (-) because then we wouldn't be leaving the loop until - was applied twice. So we move one cell left with < because we know there's a positive value there (the 1). Okay, so now we've reliably negated n on the first iteration. But we have a new problem: the tape head is now in a different position on the first iteration than in every other one. We need to consolidate this before we move on. The next < moves the tape head left. The situation on the first iteration:

        -4       
         3       
...   1  4 -1 ...
    0 0  0  0
    ^

And on the second iteration (remember we've added d once into -n now):

      -1       
       3       
... 1  4 -1 ...
    0  0  0
    ^

The next conditional merges these paths again:

(<!-)

On the first iteration the tape head points at a zero, so this is skipped entirely. On further iterations, the tape head points at a one though, so we do execute this, move to the left and increment the cell there. Since we know the cell starts from zero, it will now always be positive so we can leave the loop. This ensures we always end up two stack left of the main stack and can now move back with >>. Then at the end of the modulo loop we do -_. You already know -. _ is to subtraction what ^ is to XOR: if the top of the stack is a and the value underneath is b it replaces a with b-a. Since we first negated a though, -_ replaces a with b+a, thereby adding d into our running total.

After the loop ends (we've reached a positive) value, the tape looks like this:

        2       
        3       
... 1 1 4 -1 ...
    0 0 0  0
        ^

The left-most value could be any positive number. In fact, it's the number of iterations minus one. There's another short linear bit now:

_<<]>:]<]]

Like I said earlier we need to subtract the result from d to obtain the actual remainder (3-2 = 1 = 4 % 3), so we just do _ once more. Next, we need to clean up the stack that we've been incrementing on the left: when we try the next divisor, it needs to be zero again, for the first iteration to work. So we move there and push that positive value onto the other helper stack with <<] and then move back onto our operational stack with another >. We pull up d with : and push it back onto the -1 with ] and then we move the remainder onto our conditional stack with <]]. That's the end of the trial division loop: this continues until we get a zero remainder, in which case the stack to the left contains n's greatest divisor (other than n).

After the loop ends, there's just *< before we join paths with the input 1 again. The * simply turns the zero into a 1, which we'll need in a bit, and then we move to the divisor with < (so that we're on the same stack as for input 1).

At this point it helps to compare three different kinds of inputs. First, the special case n = 1 where we haven't done any of that trial division stuff:

         0    
... 1 1 -1 ...
    0 0  0
         ^

Then, our previous example n = 4, a composite number:

    2           
    1    2 1    
... 1 4 -1 1 ...
    0 0  0 0
         ^

And finally, n = 3, a prime number:

    3           
    1    1 1    
... 1 3 -1 1 ...
    0 0  0 0
         ^

So for prime numbers, we have a 1 on this stack, and for composite numbers we either have a 0 or a positive number greater than 2. We turn this situation into the 0 or 1 we need with the following final piece of code:

]*(:)*=<*

] just pushes this value to the right. Then * is used to simplify the conditional situation greatly: by toggling the least significant bit, we turn 1 (prime) into 0, 0 (composite) into the positive value 1, and all other positive values will still remain positive. Now we just need to distinguish between 0 and positive. That's where we use another (:). If the top of the stack is 0 (and the input was a prime), this is simply skipped. But if the top of the stack is positive (and the input was a composite number) this swaps it with the 1, so that we now have 0 for composite and 1 for primes - only two distinct values. Of course, they are the opposite of what we want to output, but that is easily fixed with another *.

Now all that's left is to restore the pattern of stacks expected by our surrounding framework: tape head on a positive value, result on top of the stack to the right, and a single -1 on the stack right of that. This is what =<* is for. = swaps the tops of the two adjacent stacks, thereby moving the -1 to the right of the result, e.g. for input 4 again:

    2     0       
    1     3       
... 1 4   1 -1 ...
    0 0 0 0  0
          ^

Then we just move left with < and turn that zero into a one with *. And that's that.

If you want to dig deeper into how the program works, you can make use of the debug options. Either add the -d flag and insert " wherever you want to see the current memory state, e.g. like this, or use the -D flag to get a complete trace of the entire program. Alternatively, you can use Timwi's EsotericIDE which includes a Stack Cats interpreter with a step-by-step debugger.

Martin Ender

Posted 2015-09-11T14:06:29.040

Reputation: 184 808

3>:^] should be the official Stack Cats logo – Alex A. – 2016-06-24T23:28:23.030

14

Haskell, 54 bytes

import Data.Numbers.Primes
main=readLn>>=print.isPrime

Nothing much to explain.

nimi

Posted 2015-09-11T14:06:29.040

Reputation: 34 639

1The same score can be achieved (although very inefficiently) without external libraries, using Wilson's theorem: main=do n<-readLn;print$n>1&&mod(product[1..n-1]+1)n<1 – Lynn – 2015-09-11T17:31:38.093

9We can even do shorter: main=do n<-readLn;print$mod(product[1..n-1]^2)n>0 is 49 bytes. – Lynn – 2015-09-11T18:06:22.907

4@Mauris: Nice. Please post it as a separate answer. – nimi – 2015-09-11T21:16:57.800

14

C++ template metaprogramming. 166 131 119 bytes.

Code compiles if the constant is a prime, and does not compile if composite or 1.

template<int a,int b=a>struct t{enum{x=t<a,~-b>::x+!(a%b)};};
template<int b>struct t<b,0>{enum{x};};
int _[t<1>::x==2];

(all newlines, except final one, are eliminated in "real" version).

I figure "failure to compile" is a falsey return value for a metaprogramming language. Note that it does not link (so if you feed it a prime, you'll get linking errors) as a full C++ program.

The value to test is the integer on the last "line".

live example.

Yakk

Posted 2015-09-11T14:06:29.040

Reputation: 859

13

Python 3, 59 bytes

Now uses input() instead of command line arguments. Thanks to @Beta Decay

n=int(input())
print([i for i in range(1,n)if n%i==0]==[1])

uno20001

Posted 2015-09-11T14:06:29.040

Reputation: 321

Take input using input() would be much shorter – Beta Decay – 2015-09-11T17:02:20.247

Thanks, I've already written with using of input(), but I forgot to refresh my answer. Thanks again! – uno20001 – 2015-09-11T17:11:15.317

652 bytes: n=m=int(input()), print(all(n%m for m in range(2,n))) – John Lyon – 2015-09-16T05:38:00.453

for i in range(2,math.sqrt(n)+1) – yask – 2015-09-17T16:21:31.417

3Are you serious. Spend 25 extra characters for a lame quadratic speedup? Here we hate bytes. We spend every hour,minute, and second of our lives getting rid of the nineteenth byte. (Just kidding. But we don't do time optimizations that increase program length.) – CalculatorFeline – 2016-03-20T04:15:19.353

@jozzas You solution does not work for n==2. – Leaky Nun – 2016-07-25T12:14:10.453

2Use n%i<1 instead. – Erik the Outgolfer – 2016-10-17T15:52:06.827

Outgolfed! Mine uses a completely different method. Rather than n%i==0, though, you could have n%i<1 because stuff using mod is never negative. That would save a byte. – 0WJYxW9FMN – 2017-09-20T17:17:33.863

If you used Python 2, you could change int(input()) to input() because of Python 2's eval. However, using a lambda would be even shorter – MilkyWay90 – 2019-01-14T22:07:41.123

50 chars, n=int(input());print(all(n%m for m in range(2,n))) – The Matt – 2020-02-07T22:39:20.300

13

C, 67 bytes

i,n;main(p){for(scanf("%d",&i),n=i;--i;p=p*i*i%n);putchar(48+p%n);}

Prints !1 (a falsey value, by Peter Taylor's definition) 0 if (n-1)!^2 == 0 (mod n), and 1 otherwise.

EDIT: After some discussion in chat, puts("!1"+p%n) seems to be considered a bit cheaty, so I've replaced it. The result is one byte longer.

EDIT: Fixed for big inputs.

Shorter solutions

56 bytes: As recommended in the comments by pawel.boczarski, I could take input in unary by reading the number of command line arguments:

p=1,n;main(i){for(n=--i;--i;p=p*i*i%n);putchar(48+p%n);}

invoking the program like

$ ./a.out 1 1 1 1 1
1                        <-- as 5 is prime

51 bytes: If you allow "output" by means of return codes:

p=1,n;main(i){for(n=--i;--i;p=p*i*i%n);return p%n;}

Lynn

Posted 2015-09-11T14:06:29.040

Reputation: 55 648

Your solution could be made shorter using unary representation (number of commandline arguments), as in my solution posted. You could shave off some bytes on scanf call. – pawel.boczarski – 2015-09-12T12:43:31.990

puts("!1"+p%n) How could you ever do a+b for char* values? – Erik the Outgolfer – 2016-07-17T15:11:11.797

If the string "!1" starts at address a, then at a+1 you’ll find the string "1". – Lynn – 2016-07-17T16:40:28.257

@Lynn Oh, I thought it was for concatenation (yeah, better leave that to strcat(const char*,const char*).) – Erik the Outgolfer – 2016-07-17T21:28:11.553

Could you change p=p*i*i%n to p*=i*i%n – Albert Renshaw – 2017-01-24T22:29:11.127

I may be wrong, but can't you use 48+p instead of 48+p%n? The only time you assign to p is in the statement p=p*i*i%n, so p will always be less than n. – Doorknob – 2019-06-24T16:15:49.993

13

Snails, 122

Input should be given in unary. The digits may be any mix of characters except newlines.

^
..~|!(.2+~).!~!{{t.l=.r=.}+!{t.!.!~!{{r!~u~`+(d!~!.r~)+d~,.r.=.(l!~u~)+(d!~l~)+d~,.l.},l=(.!.)(r!~u~)+(d!~!.r~)+d~,.r.!.

In this 2D pattern matching language, the program state consists solely of the current grid location, the set of cells which have been matched, and the position in the pattern code. It's also illegal to travel onto a matched square. It's tricky, but possible to store and retrieve information. The restriction against traveling onto a matched cell can be overcome by backtracking, teleporting (t) and assertions (=, !) which leave the grid unmodified after completing.

Factorization of 25

The factorization for an odd composite number begins by marking out some set of mutually non-adjacent cells (blue in diagram). Then, from each yellow cell, the program verifies that there are an equal number of non-blue cells on either side of the adjacent blue one by shuttling back and forth between the two sides. The diagram shows this pattern for one of the four yellow cells which must be checked.

Annotated code:

^                         Match only at the first character
..~ |                     Special case to return true for n=2
!(.2 + ~)                 Fail for even numbers
. !~                      Match 1st character and fail for n=1
!{                        If the bracketed pattern matches, it's composite.
  (t. l=. r=. =(.,~) )+   Teleport to 1 or more chars and match them (blue in graphic)
                          Only teleport to ones that have an unmatched char on each side.
                          The =(.,~) is removed in the golfed code. It forces the
                          teleports to proceed from left to right, reducing the
                          time from factorial to exponential.
  !{                      If bracketed pattern matches, factorization has failed.
    t . !. !~             Teleport to a square to the left of a blue square (yellow in diagram)
    !{                    Bracketed pattern verifies equal number of spaces to
                          the left or right of a blue square.
      {              
        (r!~ u~)+         Up...
        (d!~!. r~)+       Right...
        d~,               Down...
        . r . =.          Move 1 to the right, and check that we are not on the edge;
                          otherwise d~, can fall off next iteration and create and infinite loop
        (l!~ u~)+         Up...
        (d!~ l~)+         Left...
        d ~,              Down...
        . l .             Left 1
      } ,                 Repeat 0 or more times
      l  =(. !.)          Check for exactly 1 unused char to the left
      (r!~ u~)+           Up...
      (d!~!. r~)+         Right...
      d ~,                Down...
      . r . !.
    }
  }
}

feersum

Posted 2015-09-11T14:06:29.040

Reputation: 29 566

13

Python 2, 44

P=n=1
exec"P*=n*n;n+=1;"*~-input()
print P%n

Like Sp3000's Python answer, but avoids storing the input by counting the variable n up from 1 to the input value.

xnor

Posted 2015-09-11T14:06:29.040

Reputation: 115 687

12

APL, 40 13 bytes

2=+/0=x|⍨⍳x←⎕

Trial division with the same algorithm as my R answer. We assign x to the input from STDIN () and get the remainder for x divided by each integer from 1 to x. Each remainder is compared against 0, which gives us a vector of ones and zeros indicating which integers divide x. This is summed using +/ to get the number of divisors. If this number is exactly 2, this means the only divisors are 1 and x, and thus x is prime.

Alex A.

Posted 2015-09-11T14:06:29.040

Reputation: 23 761

10

Mouse, 65 47 bytes

?N:0S:1I:(I.N.=0=^N.I.\0=[S.1+S:]I.1+I:)S.1=!$

This uses trial division.

Ungolfed:

? N:              ~ Read an integer from STDIN and store it in N
0 S:              ~ Start a summation variable at 0
1 I:              ~ Start an interator variable at 1
( I. N. = 0 = ^   ~ While I != N
  N. I. \ 0 = [   ~ Check whether I divides N
    S. 1 + S:     ~ If so, increment the sum
  ]
  I. 1 + I:       ~ Increment the iterator
)
S. 1 = !          ~ If the sum is 1, the only divisor encountered
                  ~ is 1 (we didn't go all the way to N in the
                  ~ loop) and thus N is prime
$

Alex A.

Posted 2015-09-11T14:06:29.040

Reputation: 23 761

3May I ask what trial division is? – Beta Decay – 2015-09-13T14:03:06.350

1@BetaDecay You check the remainder when dividing by each number up to n. It's the brute force prime checking algorithm. – Alex A. – 2015-09-13T16:07:23.060

9

Cheddar, 37 bytes

Looks like a full program is required which unfortunately means a lot of boilerplate:

print Math.prime(Number::IO.prompt())

This might not work on TIO so you'd have to put it into a file and call it that way

Function, 10 bytes

Math.prime

This returns a function which checks if input is prime using Math.prime. Example:

$ cheddar primechecker.cheddar -x "[1, 3, 4, 10, 13] => (print) + f"

TF2Goat

Posted 2015-09-11T14:06:29.040

Reputation: 91

2A full program is required. – Dennis – 2017-01-06T20:18:21.273

@Dennis >_> sorry for being so late, didn't see your comment until now but fixed – Downgoat – 2017-04-13T13:28:39.927

9

Dodos, 154 143 136 133 126 122 121 bytes

	N , , + > > D f f
D
	D ,
	N F > > M
M
	M m
	+ B m f
m
	F
	+ B
,
	dip F
	>
F
	+ B f
f
	+ >
	+
B
	N
N
	B dip
+
	dot
>
	dab

Try it online!

Builtins and aliases

+
	dot

This creates an alias + for the builtin function dot, which maps the vector (v1, ..., vn) to the vector (v1 + ... + vn), i.e.,

>
	dab

This creates an alias > for the builtin function dab, which maps the vector (v1, ..., vn) to the vector (v2, ..., vn).

dip

The remaining builtin, dip, maps the vector (v1, ..., vn) to the vector (|v1 - 1|, ..., |vn - 1|).

B and N

B
	N
N
	B dip

This defines a pair of mutually recursive functions. Recall that Dodos only divide or surrender.

B, if called from outside this function group, is meant to take a pair (x, y) as argument. B(x, y) simply calls N(x, y). This will always succeed, because all calls to N go through B.

N(x, y) attempts to call B(|x - 1|, |y - 1|). The tuple inequality (|x - 1|, |y - 1|) < (x, y) holds if an only if x > 0, so N(0, y) will return (0, y), since B(1, |y - 1|) surrenders.

Whenever x ≤ y, calling B(x, y) simply decrements both coordinates x times before surrendering, returning (0, y - x). If x > y, both coordinates will still be dipped x times. However, once the second coordinate reaches 0, it will cycle between 0 and 1, resulting in (0, (x - y) % 2).

N, if called from outside this function group, is meant to take a singleton (x) as argument. N(x) calls B(|x - 1|), which calls simply N(|x - 1|).

N(0) calls B(1), which attempts to call N(1). This surrenders, so B(1) returns 1, and so does N(0).

Whenever x > 0, N(x) will decrement x until reaching N(1) → B(0) → N(0) → B(1). Since B(1) surrenders, N(0)'s argument is the return value of N(x).

Thus, N(0) = 1, while N(x) = 0 whenever x > 0.

F and f

F
	+ B f
f
	+ >
	+

f returns the results of + > and + as a vector; + > takes the sum of the vector without its first coordinate, while + takes the sum f the whole vectors.

Thus, f(v1, ..., vn) = (v2 + ... + vn, v1 + ... + vn).

On occasions, we'll call f outside of F. Notably, f(x) = (0, x), f(0, x) = (x, x), and
f(x, y) = (y, x + y)

F simply calls three functions we've seen before. Since v2 + ... + vn ≤ v1 + ... + vn, B maps the result returned by f to (0, v1). + takes the sum, returning (v1).

Thus, calling F on a vector returns its first coordinate.

,

,
	dip F
	>

dip F dips the first coordinate of the argument vector, while > returns the remaining coordinates. Thus, , maps (v1, ..., vn) to (|v1 - 1|, ..., vn).

In particular, , maps (x) to (|x - 1|), so we can use it instead of dip for singleton vectors.

M and m

M
	M m
	+ B m f
m
	F
	+ B

m is always meant to be called on a pair. It simply combines a few functions we've seen before. Recall that B behaves differently if x &leq; y and if not.

We have m(x, y) = (x, y - x) if x &leq; y but m(x, y) = (x, (x - y) % 2) otherwise.

M is always meant to be called on a pair, whose first coordinate will be non-zero. M attempts to recursively call itself on the result of m. Since m doesn't change the first coordinate of its argument, we only need to examine the second one.

On the second line, B(m(f(x, y))) = B(m(y, x + y)) = B(y, x). After + takes the sum, we get x - y if x ≥ y, but (y - x) % 2 otherwise.

If y = qx, M(x, y) = M(x, qx) will successively call M(x, (q - 1)x), M(x, (q - 2)x), ..., M(x, x), M(x, 0). At the end, M(x, 0) attempts to recursively call itself, which surrenders. The return value is (x, 0), concatenated with all singletons returned by the second line. Since the second line maps (x, x) to 0, the result of M(x, y) will match the pattern (x, 0, 0, ...).

If y = qx + r, with 0 < r < x, we'll proceed in similar fashion, eventually reaching M(x, x + r), then M(x, r).

  • If r = 1 and x is even, m(x, r) = (x, (x - r) % 2) = (x, 1), and the recursive call to M surrenders. In the previous call, the second line mapped (x, x + r) to (x + r - x) % 2 = 1, so the result of M(x, y) will match the pattern (x, 1, 1, ...).

  • If r = 1 and x is odd, m(x, r) = (x, (x - r) % 2) = (x, 0), and the recursive call to M succeeds.

    Likewise, if r > 1, then m(x, r) = (x, (x - r) % 2) &leq; (x, 1) < (x, r), and the recursive call to M succeeds.

    In both cases, the second line will be evaluated for the last time with argument (x, t). Since we have t < r < x, the outcome is the non-zero vector (x - t), so the result of M(x, y) will match the pattern (x, ?, x - t, ...).

Thus, the third element of the vector returned by M(x, y) will be 0 if and only if x is a divisor of y.

D

D
	D ,
	N F > > M

D expects (n, n) as its initial argument.

The first line will recursively call D on the result of ,, so we'll call D(n, n) → D(n - 1, n) → ... → D(1, n) → D(0, n) → D(1, n), surrendering and returning (0, n).

The second line will be evaluated for every (k, n), with 0 < k ≤ n. F > > M calls M, discards the first two elements, then extracts the first remaining one. As we've seen before, M returns (k, ?, 1, ...) if k is a divisor of n, (k, ?, 0, ...) otherwise, so the final vector returned by D(n, n) is (0, n, 1 | n, 2 | n, ..., n | n).

main

	N , , + > > D f f

Our entry point expects a singleton (n), with n > 0.

Since f(f(n)) = f(0, n) = (n, n), D will return (0, n, 1 | n, 2 | n, ..., n | n).

After discarding (0, n) with > >, a call to + takes the sum of the Booleans, counting the number of divisors of n.

, , dips the divisor count twice, resulting in a 0 only for zero or two divisors. Since n > 0, there is at least one divisor, so the result is 0 if and only if n has exactly two divisors.

Finally, N takes the logical NOT, returning 1 for primes and 0 for non-primes.

Dennis

Posted 2015-09-11T14:06:29.040

Reputation: 196 637

I've edited my answer. – Dennis – 2018-03-22T01:56:29.183

Not really related, but ... no chatroom for Dodos yet? – Weijun Zhou – 2018-03-22T02:40:05.313

1

@WeijunZhou Fixed.

– Dennis – 2018-03-22T02:47:18.090

That's a tricky language! Some typos near the end: 0 < k < n should be 0 < k ≤ n, k | n should be n | n (two places), and "M returns 1" should be something like "M returns third element 1". – Ørjan Johansen – 2018-03-22T03:42:33.053

@ØrjanJohansen All fixed, I think. Thanks! – Dennis – 2018-03-22T03:49:49.780

This is insane. Brain-Flak is a breeze compared to Dodos! +1 – DLosc – 2018-03-22T08:02:25.390

9

Swift 2.0, 70 100 98 82 77 75 bytes

A simple trial division loop, divided by 2 instead of sqrt() as it takes less bytes! Will default to using 64-bit ints.

let n=Int(Process.arguments[1])!;print(n<4||(2..<n).filter{n%$0<1}.count<1)

GoatInTheMachine

Posted 2015-09-11T14:06:29.040

Reputation: 463

1Is there a need to divide by two? I think you can use the 2..<n range instead to save another. – Geobits – 2015-09-11T14:56:43.243

1This prints true for 0 and 1. You can change the condition from n<4|| to n>1&& to make it work. Also: You can change Process.arguments[1] to readLine()! – Kametrixom – 2015-09-12T15:58:47.990

Instead of ".count<1" you can use: "==[]" to crop some extra characters. – Simon – 2015-12-04T20:43:45.563

9

C, trial division, 72 bytes

i=1;main(n){for(scanf("%d",&n);n%++i&&i*i<n;);printf("%d",n<3?n-1:n%i);}

Note special handling for n=1,2

We need to stop the loop before i gets to n-1, so i<n-1 would do, but i*i<n is more efficient.

Level River St

Posted 2015-09-11T14:06:29.040

Reputation: 22 049

I used this to post a Javascript answer with the same functionality :) – Sam – 2015-09-11T15:05:33.580

You get the exact same behaviour in one byte less by just putting everything on a single line, no separator needed. – hvd – 2015-09-13T21:45:08.253

@hvd OMG! how did I miss that ?! thanks. – Level River St – 2015-09-13T22:31:14.713

Replace i and n to save the initialization - when run with no arguments, the first parameter is 1. – ugoren – 2015-09-17T07:15:31.003

9

gs2, 2 bytes

Vk

Basically read-num is-prime.

Lynn

Posted 2015-09-11T14:06:29.040

Reputation: 55 648

8

Prolog (SWI), 42 40 bytes

+X:-X>1,2+X.
Y+X:-X=:=Y;0<X mod Y,1+Y+X.

Try it online!

Tests primality by checking whether any of the numbers less than the number and greater than one divide the number.


Explanation

+X:-X>1,2+X

defines a new predicate +/1 that is true if X > 1 and 2+X is satisfied. Despite what it appears, this is not an arithmetical expression. It refers to the predicate defined on the next line.

Y+X:-X=:=Y;0<X mod Y,1+Y+X.

This defines a new predicate +/2 that is true if X =:= Y (=:= represents equality for arithmetic expressions), or both X mod Y is not zero (since X mod Y can't be negative, checking that it is greater than zero is sufficient) and 1+Y+X is true. The trick with 1+Y+X is that the two pluses do not end up being used the same way. The + operator is left associative so 1+Y+X is equivalent to (1+Y)+X. Since the expression must be a call to a predicate and +/2 is a predicate that I defined, the interpreter then calls +/2 with arguments 1+Y and X. Thus recursively the program will check whether any Y from 2 to X-1 divides X (it stops at X since X=:=Y would be true and so the truth value of the others becomes irrelevant).

Operators in SWI-Prolog

This program heavily abuses the way operators are handled in SWI-Prolog. SWI-Prolog does not define predicates for arithmetic operators such as +. This makes sense since the arithmetical + is not a predicate since the output of a predicate must be true or false. What SWI-Prolog does instead is it builds structures out of the operators and then the predicates that evaluate arithmetic expressions (such as =:= and <) know how to evaluate those structures. For instance after the +/2 predicate in my program recurses three times with the initial Y=2 then Y=1+(1+(1+2)) not Y=5. The fact that + is not defined as a predicate means that I can define it as a predicate myself and since it is an operator I can save the bytes that I would otherwise need to spend on parentheses and commas.

0 '

Posted 2015-09-11T14:06:29.040

Reputation: 3 439

8

Matlab/Octave 24

It is just using a builtin function, using a sieve.

disp(isprime(input('')))

You could also use this:

isprime(input(''))

Which would print the output as the last result in the console, but I am not sure whether this is allowed.

flawr

Posted 2015-09-11T14:06:29.040

Reputation: 40 560

I am curious, where did you get the information about the other method that you mentioned? (Perhaps it is different in a newer vesion?) – flawr – 2015-09-15T17:52:44.647

This link I guess the MuPAD notice at the top is relevant. – Martin Ender – 2015-09-15T17:56:01.927

Yes that is what makes the difference, thanks for the link=) – flawr – 2015-09-15T19:46:29.567

8

><>, 25 + 3 = 28 bytes

:1-:v
v!?:<-1$**:@:
>r%n;

Inputting as a byte with i is shorter, but ><> can handle numbers larger than 255, hence the need for command line input in order to follow the rules. The +3 is for the v flag, i.e. run like

py -3 fish.py primes.fish -v 101

Outputs (n-1)*((n-1)!)^2 mod n (the initial (n-1)* is unnecessary, but it makes the code shorter).

Sp3000

Posted 2015-09-11T14:06:29.040

Reputation: 58 729

I don't quite understand what you mean by your first sentence and I'm curious to know. Care to explain? The rules seem to say that it only needs to work for the integers 1 to 255. – cole – 2015-09-11T23:21:37.137

3@Cole The rules say you can read a single byte "if this is your languages largest data type" – Sp3000 – 2015-09-12T01:08:32.360

8

Python 2, 46 bytes

m=n=input()
a=1
while~-m:m-=1;a*=m*m
print a%n

Sp3000

Posted 2015-09-11T14:06:29.040

Reputation: 58 729

Just 2 bytes shorter than the functional version: n=input();print all(n%m for m in range(2,n))*~-n. If all([]) returned False it would be the other way around. – DLosc – 2015-09-11T19:34:14.210

8

ShapeScript, 53 25 23 bytes

_11?1-"@1?*@1-"1?*!?*@%

The program uses Wilson's theorem; it prints 1 for primes and 0 for non-primes. Input is in unary.

I created ShapeScript for this competition. The interpreter on GitHub has a slightly modified syntax and better I/O (none of which are required in this answer).

Try it online!

How it works

_        Take the length of the input to convert from unary to integer (N).
1        Push 1 (accumulator).
1?1-     Push a copy of N and subtract 1. Let's call the result I.
"        Push a string that, when evaluated, does the following:
  @        Swap I with the accumulator.
  1?       Push a copy of I.
  *        Multiply it with the accumulator.
  @        Swap the updated accumulator with I.
  1-       Decrement I.
"
1?       Push a copy of N-1.
*!       Repeat the string N-1 times and evaluate the result.
         This calculates (N-1)! and leaves I = 0 on the stack.
?        Use I to copy the factorial.
*        Multiply to calculate the factorial's square.
@%       Calculate N%((N-1)!*(N-1)!).

Dennis

Posted 2015-09-11T14:06:29.040

Reputation: 196 637

8

Sesos, 50 49 bytes

Algorithm #3...

0000000: 16def7 f5991b 7441bf 3f0ebb eecfd8 b86b33 b7eb33  ......tA.?......k3..3
0000015: 37ecda bccdd8 b86b33 3ffcfe 8c7de8 797cfc f599c3  7......k3?...}.y|....
000002a: f973f5 8479c5 03                                  .s..y..

Try it online!

This is the very first working code I got, so I'm sure it's possible to shave off a few more bytes here. Here's the BF-code I wrote (with some rather sparse comments that are mostly meant for myself):

,
[>+>+>+<<<-]                ; triplicate input
>>[-                        ; i from n_1 down to 0
    <+[-<<<+>>>             ; j from n down to 0 copying n to the left
        [                   ; k from j down to 1
            >[<<+<+>>>-]    ; add i to the values on the left
            <<[>>+<<-]      ; move one copy of i back
            >>>>+<<<-       ; decrement k while copying j to the right
        ]
        >>[<<+<<->>>>-]     ; subtract n from i*j while copying it
        <<[>>+<<-]          ; move n back
        >>>[<<<+>>>-]       ; move j back
        <<<<<[              ; if not equal:
            ,               ; reset to zero
            >               ; move to zero left of j
        ]
        >
    ]
    <<<[>>>+<<<-]           ; move n back to j
    >[<]>>>                 ; if we didn't exit move back to i
                            ; otherwise remain on the zero left of j
]
>,+>-[<->,]<.

I then used this Retina script to convert that to Sesos ASM:

set numin
set numout

get
jmp
   fwd 1
   add 1
   fwd 1
   add 1
   fwd 1
   add 1
   rwd 3
   sub 1
jnz
fwd 2
jmp
   sub 1
   rwd 1
   add 1
   jmp
      sub 1
      rwd 3
      add 1
      fwd 3
      jmp
         fwd 1
         jmp
            rwd 2
            add 1
            rwd 1
            add 1
            fwd 3
            sub 1
         jnz
         rwd 2
         jmp
            fwd 2
            add 1
            rwd 2
            sub 1
         jnz
         fwd 4
         add 1
         rwd 3
         sub 1
      jnz
      fwd 2
      jmp
         rwd 2
         add 1
         rwd 2
         sub 1
         fwd 4
         sub 1
      jnz
      rwd 2
      jmp
         fwd 2
         add 1
         rwd 2
         sub 1
      jnz
      fwd 3
      jmp
         rwd 3
         add 1
         fwd 3
         sub 1
      jnz
      rwd 5
      jmp
         get
         fwd 1
      jnz
      fwd 1
   jnz
   rwd 3
   jmp
      fwd 3
      add 1
      rwd 3
      sub 1
   jnz
   fwd 1
   jmp
      rwd 1
   jnz
   fwd 3
jnz
fwd 1
get
add 1
fwd 1
sub 1
jmp
   rwd 1
   sub 1
   fwd 1
   get
jnz
rwd 1
put

And of course the final conversion to binary is done by Sesos itself.

I scrapped three earlier attempts for trial division, and ultimately really got tired of the modulo computation. So I started thinking about how I could avoid that altogether. I ended up coming up with a very simple primality test, that for some reason never occurred to me before and might be handy for a lot of other esolangs where doing a multiplication is fine but computing a modulo is a royal pain:

In essence, I just compute the full multiplication between [1, ..., n-1] and [1, ..., n] starting from the largest value. After each multiplication, I subtract n from the result. If that gives 0, I terminate. This is bound to terminate, because at the beginning of the final iteration of the outer loop, I'm computing 1 * n. If I get there, it's a prime. Otherwise, some earlier multiplication will have given n and the loop stops there instead. That means I can simply check after terminating whether the first iterator is equal to 1 or not in order to decide primality.

I'll probably post the Brainfuck-version of this as well, once I'm happy with the golfing.

Martin Ender

Posted 2015-09-11T14:06:29.040

Reputation: 184 808

7

Reflections, 194 181 bytes

  _v@\
|* / (0    /\
   /;*      <
/0):\(1/# + /#+\
:  ; >~<   \ _ /
#|v\/ 1)
(0*    \#:(1 \
\\#  \(0__0) /
 _  / (0\
/^^: 0):/
\#+ _#_
/0):^\
:  / /
#
(0 >#* _#_
\ _<
   \        /

Test it!

Outputs 1 for prime, else 0.

Explanation

First we parse the number:

  _v@\
|* / (0
     *
     (1/ -> IP leaves here
     >~<
      1)
       \#:(1 \
     \(0__0) /
  • _ reads a line from input
  • v reflects the IP down
  • / reflects the IP left
  • * at (1|1) pushes 1×1=1
  • | reflects the IP right
  • * pushes another 1
  • / reflects the IP up
  • v pops a value off the stack and reflects the IP right as it's true
  • @ at (4|0) converts all input to numbers
  • \ reflects the IP down
  • (0 moves the first digit to stack 0
  • * at (5|2) pushes 5×2=10
  • (1 moves the 10 to stack 1
  • > reflects the IP right
  • ~ pushes the number of left digits
  • < pops that number and reflects the IP down if it's not 0:
    • 1) moves the 10 from stack 1 to the main stack
    • \ reflects the IP right
    • # redefines (0|0)
    • : doubles the 10
    • (1 moves the top 10 to stack 1 again
    • \ reflects the IP down
    • / reflects the IP left
    • 0) pulls the previous result from stack 0
    • _ at (1|1) multiplies the previous result and 10
    • _ at (0|1) adds the next digit
    • (0 pushes the result to stack 0
    • \ reflects the IP up
    • > enters the loop again
  • if it's zero, reflect the IP up
    • / reflects the IP right

Now, we have the test number on stack 0.

Then, we initialise the loop:

            <
       /# + /
  • / reflects the IP right
  • # redefines (0|0)
  • + at (2|0) pushes 2+0=2
  • / reflects the IP up
  • < reflects the IP left

Now, we have a 2 (the counter) on the main stack and the input number on stack 0.

Then we have the real loop:

           /\
   /;*      <
/0):\       /#+\
:  ;       \ _ /
#|v\/
(0*
\\#
 _  / (0\
/^^: 0):/
\#+ _#_
/0):^\
:  / /
#
(0 >#* _#_
\ _<
   \        /
  • * pushes x×y
  • ; pops that again
  • / reflects the IP down
  • : duplicates the counter
  • ; discards the duplicate
  • \ reflects the IP right
  • / reflects the IP up
  • \ reflects the IP left
  • : duplicates the counter again
  • 0) pulls the input number from stack 0
  • / reflects the IP down
  • : duplicates the input
  • # redefines (0|0)
  • (0 pushes the duplicated input to stack 0
  • \ reflects the IP right
  • \ reflects the IP down
  • _ at (1|3) pops the counter and the input and pushes whether they're equal
  • ^ pops the test and reflects the IP left if true (i.e. if we have tested all numbers less than the input and haven't found a factor → the number is prime):
    • / reflects the IP down
    • \ reflects the IP right
    • # redefines (0|0)
    • + at (1|0) pushes 1+0=1
    • _ at (3|0) converts to string
    • # redefines (0|0)
    • _ at (1|0) prints
    • then the IP leaves the grid and the program ends
  • else the IP is reflected right:
  • ^ reflects the IP up
  • # redefines (0|0)
  • * pushes 0×-1=0
  • v pops the 0 and reflects the IP left
  • | reflects the IP right
  • v reflects the IP down
  • * pushes 0×-1=0
  • # redefines (0|0)
  • ^ pops the zero and reflects the IP right
  • : duplicates the counter
  • 0) pulls the input from stack 0
  • : duplicates it
  • / reflects the IP up
  • \ reflects the IP left
  • (0 pushes the duplicated input to stack 0
  • / reflects the IP down
  • _ at (2|3) checks if the input is greater than the counter. Note that this is only false if the input is 1 as else the previous check applies before.
  • ^ reflects the IP right if the check was false (i.e. input is < 2):
    • \ reflects the IP down
    • / reflects the IP left
    • / reflects the IP down
    • > enters the 'output zero' part, see below
  • else the IP is reflected left:
  • : duplicates the counter (once again)
  • 0) pulls the input from stack 0 (once again)
  • : duplicates the input (once again)
  • # redefines (0|0)
  • (0 pushes the input to stack 0 (once again)
  • \ reflects the IP right
  • _ at (2|2) pops input and counter and pushes input modulo counter
  • < pops the result and reflects the IP up if 0 (it's a factor):
    • > reflects the IP right into the 'output zero' part, see below
  • else (it's no factor) the IP is reflected down:
  • \ reflects the IP right
  • / reflects the IP up
  • / reflects the IP right
  • # redefines (0|0)
  • + at (1|0) pushes 1+0=1
  • \ reflects the IP down
  • / reflects the IP left
  • _ at (0|1) adds the 1 to the counter (counter++)
  • \ reflects the IP up
  • / reflects the IP right
  • \ reflects the IP down
  • < enters the loop again

Now for the 'output zero' part:

   >#* _#_
  • > reflects the IP right
  • # redefines (0|0)
  • * at (1|0) pushes 1×0=0
  • _ at (3|0) converts to string
  • # redefines (0|0)
  • _ at (1|0) prints '0'
  • then the IP leaves the grid and the program ends

wastl

Posted 2015-09-11T14:06:29.040

Reputation: 3 089

Okay, I need to check out this language. – Esolanging Fruit – 2018-03-07T20:05:10.470

7

Foo, 40 bytes

&1@@@>>&>&1<(2-1@<<@>&%+1@>>%<)&2/@>+%$i

Probably not the best approach, but I wanted to give it a try. Thanks to the "wonders" of Foo's do-while loops, I had to special case 1 and 2, both of which output errors to STDERR (but STDOUT output is correct).

The input is hardcoded as the number after the first &.

Sp3000

Posted 2015-09-11T14:06:29.040

Reputation: 58 729

7

PowerShell, 35 Bytes

param($a)$a-match'^(?!(..+)\1+$)..'

Uses the same regex from Martin's Retina answer, as that's way shorter than anything that will wind up using the [math]:: libraries one would normally use. Expects input as command-line argument in unary format.

Corrected from initial version (which was apparently specific to the particular PowerShell implementation I coded it on) thanks to Jonathan Leech-Pepin. Grr undocumented version differences.

Examples:

PS C:\Tools\Scripts\golfing> .\is-this-number-a-prime.ps1 111111
False

PS C:\Tools\Scripts\golfing> .\is-this-number-a-prime.ps1 1111111
True

Bonus - PowerShell pipeline input, 29 Bytes

%{$_-match'^(?!(..+)\1+$)..'}

Same as the above, just called differently, which shaves bytes. For example,

PS C:\Tools\Scripts\golfing> 111111 | %{$_-match'^(?!(..+)\1+$)..'}
False

AdmBorkBork

Posted 2015-09-11T14:06:29.040

Reputation: 41 581

I actually had to wrap the $args[...]..' in () to be able to get it to resolve. Otherwise it was always True. – Jonathan Leech-Pepin – 2015-11-04T21:11:15.050

@JonathanLeech-Pepin Interesting - likely a versioning difference in how operations are ordered. I primarily code in PowerShell v4 (using the ISE); what were you using? – AdmBorkBork – 2015-11-04T21:27:57.777

Happened on Win7 with the latest 5.0 rtm (August), also happens on Win10 with built-in. It is not however an issue with the pipeline version. On the other hand: param($a)$a-match'^(?!(..+)\1+$)..' works and is exactly 35 bytes as well. – Jonathan Leech-Pepin – 2015-11-04T21:40:36.593

@JonathanLeech-Pepin Indeed -- I just tested and confirmed on a Windows 7 machine. How strange. Yay for undocumented features :-/ ... I've corrected the code with the param($a) iteration, which should work on any version. Thanks for the assist! – AdmBorkBork – 2015-11-04T21:56:39.543

7

Fourier, 44 bytes

Guess who? :D

1~h2~xI~g<3{1}{5+g~g}g(g%x{0}{~hgv~x}x^~x)ho

Yes, it's my very own Fourier again, in a situation where it is actually in the running for a golfing competition.

The most bytes are spent on handling the input cases for 1 and 2.

Prints "1" for True and "0" for False.

Explanation

1~h                                            # Set h to 1
   2~x                                         # Set x to 2
      I~g                                      # Set g to user input      
         <3{1}{     }                          # If accumulator is less than three then
               5+g~g                           # Add 5 and g and set g to that value  
                     g(                  )     # Loop until the accumulator equals g
                       g%x{0}{      }          # If g%x equals 0, then
                              ~hgv~x           # Set h to 0 and set x to g-1
                                     x^~x      # Increment x and set x to that value
                                           ho  # Output the value of h

Beta Decay

Posted 2015-09-11T14:06:29.040

Reputation: 21 478

7

Bubblegum, 98 bytes

from math import factorial as F#
try:n=int(i)-1;o=n*(F(n)%-~n==n)
except:o=sum(map(int,i.split()))

This prints p - 1 if p is prime and 0 otherwise.

It may not look like it, but this is the shortest known Bubblegum program that achieves this task.

There are probably shorter programs, but their discovery would require a cryptographic break of the SHA-256 hash.

Dennis

Posted 2015-09-11T14:06:29.040

Reputation: 196 637

Did you choose the hash value based on this program or did you find it starting from a random hash? – Sp3000 – 2015-09-11T17:03:16.757

Option 1. The program works for addition and primality testing. – Dennis – 2015-09-11T17:05:17.067

Wait what according to the esolangs page this is identical to the code for calculating a sum... – ev3commander – 2015-11-18T20:56:59.770

@BlockCoder1392 If you give this program one number it will test primality. If you give it two or more it will add. – pppery – 2016-02-25T02:13:47.323

7

Perl, 23 20 bytes

say/^(?!(..+)\1+$)/

using -n option.

say<>=~/^(?!(..+)\1+$)/

Using the regular expression+unary input approach, prints 11 (or whatever number you entered) or a blank line.

Bonus: decimal version, 31 bytes

(1x<>)=~/^1$|^(11+)\1+$/||say 1

ThaddeusB

Posted 2015-09-11T14:06:29.040

Reputation: 401

Does this require input in unary? – Lynn – 2015-09-11T18:45:33.500

@Mauris, clarified as you commented :) – ThaddeusB – 2015-09-11T18:46:11.877

The 20 byte version reports 1 as a prime, doesn't it? – Martin Ender – 2015-09-14T10:14:03.600

The decimal version says 0 is prime. – DanaJ – 2015-09-14T12:12:00.127

@DanaJ The specs say it has to work on 1-255 :) For a cost of 1 byte, changing the first part to ^1?$ would fix 0. – ThaddeusB – 2015-09-14T14:05:39.783

@MartinBüttner No, the lookahead forces a match to be at least 2 characters (= 2 or greater) – ThaddeusB – 2015-09-14T14:09:12.457

@ThaddeusB Exactly, but it's a negative lookahead, so if there's only one character, the contents of the lookahead fail, which means the lookahead itself passes and you get a match. See here

– Martin Ender – 2015-09-14T14:12:22.060

@MartinBüttner When you print the result of a regular expression in Perl, it prints the captured match ($1) instead of true/false. So the regular expression evaluates as true for 1, but it doesn't print anything because there was no match to the (..+) – ThaddeusB – 2015-09-14T14:30:25.530

@ThaddeusB A negative lookahead shouldn't be able to capture anything (because it has to fail for the regex to match). How does this print something for primes then? – Martin Ender – 2015-09-14T14:33:01.197

@MartinBüttner The lookahead itself doesn't capture anything, but the (..+) inside the look ahead does capture. When the negative lookahead fails (e.g. "11" matches in "1111") $1 is cleared, when the negative lookahead goes through all the possible values without failing, the last try is left is $1 and that prints. Since there were no ''(..+)'' match attempts for 1, nothing is ever put in $1... There doesn't seem to be much reason as to why Perl would do this, but that is what it does. So a prime will print 11 and a non-prime will print nothing. – ThaddeusB – 2015-09-14T14:53:04.010

@ThaddeusB Oh, that's an interesting oddity of Perl's regex flavour then. Even PCRE doesn't behave like this. I'll try to remember that. ;) – Martin Ender – 2015-09-14T14:54:17.527

@ThaddeusB Am I correct that you read the specs as saying the program only applies to the input 1-255, and results outside that range do not need to meet the requirements? That would impact the "probable prime" arguments for a number of other languages (since then only numbers up to 255 would have to be deterministic). I find the relevant paragraph in the specs rather hard to interpret. – DanaJ – 2015-09-14T16:21:04.687

2

@DanaJ Good question... Per Dennis' clarification 0 failing is not an issue.

– ThaddeusB – 2015-09-14T16:37:08.747

Please use the ## Perl, 19 + 1 (-n flag) = 20 bytes format as specified in the question, as I was initially confused as to why it was not counted as 19 bytes. Please also include an explanation as to why using say instead of print is allowed for free, such as is done in this answer.

– Deadcode – 2018-12-08T18:01:28.690

say 1-(1x<>)=~/^1$|^(11+)\1+$/ is shorter than the given decimal version by 1 byte (it's 30 bytes), and has the added bonus of printing 0 instead of nothing for a "not-prime" result. But I would suggest using say 1-(1x<>)=~/^1?$|^(11+)\1+$/ which is identical in length at 31 bytes and identifies 0 as not-prime. – Deadcode – 2018-12-08T18:26:45.293

@ThaddeusB The link in "Per Dennis' clarification 0 failing is not an issue." is dead. I don't know what comment it originally linked to, but this comment does clarify that the input is strictly positive integers. I still think it is an interesting exercise to analyze what minimum penalty in bytes is necessary to correctly identify 0 as not-prime.

– Deadcode – 2018-12-08T19:16:34.693

7

JavaScript (ES6), 47 bytes

alert(!/^(11+)\1+$/.test('1'.repeat(prompt())))

Toothbrush

Posted 2015-09-11T14:06:29.040

Reputation: 3 197

@ETHproductions OK. I'm working on the case of the input being 1. – Toothbrush – 2015-09-13T17:54:47.617

2This seems to be missing output. It assumes that the code is run in a console, i.e. a REPL environment. The challenge explicitly asks for a full program though. – Martin Ender – 2015-09-14T09:43:08.557

Virtually identical to one already posted, which also covers the '1' case: http://codegolf.stackexchange.com/a/57692/30793

– Mwr247 – 2015-09-14T14:51:21.593

1

Also, needs an alert or other print function to be considered valid for code golf: http://meta.codegolf.stackexchange.com/questions/803/javascript-standards-for-io

– Mwr247 – 2015-09-14T16:48:12.410

Outputs true for 0 and 1. – RK. – 2015-09-17T19:24:34.180

7

FRACTRAN, 144 bytes

29/14 222/377 59/26 247/59 329/57 19/47 2/19 11/29 403/407 217/33 11/31 2/11 1/37 1/2 23/9 43/69 23/43 1/23 425/41 4823/85 17/53 41/25 2/119 1/5

This took me way longer than expected - special casing 1 was pretty annoying. Takes 5^n as input and outputs 3^0 = 1 (falsy) if composite, or 3^1 = 3 (truthy) if prime.

The approach is similar to Conway's prime generator, performing a divmod on descending divisors. This isn't as compact though, so there's still much to golf.

Sp3000

Posted 2015-09-11T14:06:29.040

Reputation: 58 729

How does 3^1 = 7? – feersum – 2015-09-13T01:06:51.393

@feersum Updated one number, forgot to update the other. Thanks for pointing out – Sp3000 – 2015-09-13T01:50:37.670

7

Sesos, 67 66 65 bytes

Edit: Saved a byte by using another get instead of a loop.

Edit: Saved a byte because I don't need this rwd 6 after I changed from sub 1 to add 1 before it.

Try it online

The hexdump:

0000000: 16f8be 76ca83 e653e3 b472f0 750ef0 af9f1f fcebbb  ...v...S..r.u........
0000015: 7f7ec6 77e13b bf41f7 2961f0 af9f1f fcebbb 7f6ec7  .~.w.;.A.)a........n.
000002a: 3fc013 ef9da3 a0fbbc 77ecc7 776e1b bf73b8 576a9c  ?........w..wn..s.Wj.
000003f: 663e                                              f>

This is the Sesos assembly code that I wrote, which is assembled into the above binary to be executed:

set numin
set numout
get

jmp     ; n += (n==2)
sub 1
fwd 1
add 1
fwd 1
add 1
rwd 2
jnz
add 2
fwd 1
jmp
sub 1
rwd 1
sub 1
fwd 1
jnz
add 1
rwd 1
jmp
fwd 1
sub 1
rwd 1
get
jnz
fwd 1
jmp
sub 1
fwd 1
add 1
rwd 1
jnz
fwd 1

jmp     ; list from n to 1
jmp
sub 1
fwd 3
add 1
rwd 3
jnz
fwd 3
jmp
sub 1
fwd 3
add 1
rwd 6
add 1
fwd 3
jnz
fwd 3
sub 1
jnz

rwd 6   ; List [n, n-1, ..., 2, 2]. We don't want n%1.
add 1
jmp
rwd 6
jnz
fwd 6

jmp     ; move n one cell to the left
sub 1
rwd 1
add 1
fwd 1
jnz

add 2   ; copy the n's
jmp
rwd 1
jmp
sub 1
fwd 3
add 1
rwd 3
jnz
fwd 3
jmp
sub 1
fwd 3
add 1
rwd 6
add 1
fwd 3
jnz
fwd 4
jnz
rwd 7

jmp     ; compute each divmod, only the n%d results will be used
jmp
sub 1
fwd 1
sub 1
jmp
fwd 1
add 1
fwd 2
jnz
fwd 1
jmp
add 1
jmp
sub 1
rwd 1
add 1
fwd 1
jnz
fwd 1
add 1
fwd 2
jnz
rwd 5
jnz
rwd 6
jnz
fwd 8

jmp     ; go to first modulus of zero, or past end of list
fwd 6
jnz

fwd 1   ; negate cell to the right
jmp
rwd 1
add 1
fwd 1
jmp
sub 1
jnz
jnz
add 1
rwd 1
jmp
fwd 1
sub 1
rwd 1
sub 1
jnz

fwd 1   ; output
put

Explanation (In BF, since I actually wrote it in BF first)

Sesos and BF are closely related, so I will write the explanation in BF to take less space (it won't be on as many lines):

>   fwd 1
<   rwd 1
+   add 1
-   sub 1
,   get
.   put
[   jmp
]   jnz

First, Sesos is basically BF, but there is some I/O help, using the assembler directives set numin and set numout. These allow me to take an unbounded integer as input, into a single cell, or output that cell as an integer. I decided this was the easiest way to write the program for all positive integers.

My explanation is of each section from the above code, with sub-explanations showing the manipulations of the tape, in an attempt to help you understand the process and algorithm. I put the tape in curly braces, and use > to denote the pointer's location on the tape.

Section 1, the bug-fix / edge case:

It should be noted that before I fixed this, my code was only 54 bytes. Because of how I determine if a number is prime later, I had to add one to n if n==2, so I do that first. I use a , here (get) to zero a cell instead of looping with [-].

[->+>+<<]++>[-<->]+<[>-<,]>[->+<]>

    n += (n==2):
    goal 1: { 2 n n }
    goal 2: { 0 n==2 n }
    goal 3: { 0 0 n* }

    { n 0 0 }
    [->+>+<<]++>
    { 2 >n n }
    [-<->]+<[>-<,]>
    { 0 >n==2 n }
    [->+<]>
    { 0 0 >n* }

Section 2, the list and my ultimate goal:

The way I check if n is prime is to check n modulo every number from n-1 to 2, which I figured would be simplest. My main goal was to reach the following data structure:

0 >{n n-1 0 0 0 0, n n-2 0 0 0 0, ..., n 2 0 0 0 0}

This facilitates the DivMod algorithm I planned to use, which requires n d 0 0 0 0 on the tape.

So I create a list from n-1 to 0, with the necessary spacing. I copy the first marked cell to the second, then copy that temp cell back into the original and into the next. Then subtract one. This repeats until I hit zero.

0 { 0 >n }
[[->>>+<<<]>>>[->>>+<<<<<<+>>>]>>>-]

0 {0 n 0 0 0 0, 0 n-1 0 0 0 0, ..., 0 2 0 0 0 0, 0 1 0 0 0 0, 0 >0 0 0 0 0}
     ^     ^      ^

Then, make the last section find n%2, since n%1 would cause a result of 0 for every n. Changing it to a zero instead of a two produces the wrong answer for n=1. After that, move back to n.

<<<<<<+
[<<<<<<]>>>>>>

0 {0 >n 0 0 0 0, 0 n-1 0 0 0 0, ..., 0 2 0 0 0 0}

Move n left one cell, preparing to copy it across the list:

[-<+>]

0 {n >0 0 0 0 0, 0 n-1 0 0 0 0, ..., 0 2 0 0 0 0}

Section 3, copy the n's

I copy n to the correct position for each entry in the list, so that I'll be ready to use the DivMod algorithm for each entry. I first add two here, so that we find another n%2, rather than n%0. This is nearly the same code as in section 2, except that I compare to the cell on the right each time, in order to stop upon completing the length of the list.

++[<[->>>+<<<]>>>[->>>+<<<<<<+>>>]>>>>]

0 {n 2 0 0 0 0, n n-1 0 0 0 0, ..., n 2 0 0 0 0, n >0 ...}
   ^     ^      ^

Section 4, compute each DivMod

I go through the list, doing the algorithm for each, after which only the n%d results are used. Though the algorithm only lists 4 cells on the site, it relies on the 5th and 6th cells being zero for its magic to work. I used the version which does not preserve n, since I won't need it anymore.

The algorithm:

# >n d 0 0 0 0
[->-[>+>>]>[+[-<+>]>+>>]<<<<<]
# >0 d-n%d n%d n/d 0 0

As applied across the list (x marks stuff I don't really need, but do make use of later):

[
    [->-[>+>>]>[+[-<+>]>+>>]<<<<<]
    <<<<<<
]>>>>>>>>

0 {0 x >n%(n-1) x 0 0, 0 x n%(n-2) x 0 0, ..., 0 x n%2 x 0 0}

Section 5, if any n%d == 0

I check the list from left to right.

{0 x >n%(n-1) x 0 0, ...}

[>]

What? You expected more? Well it really is that simple. This stops at the first occurrence of 0, which is either in this list, meaning the number is not prime, since it has a divisor, or we went past the list, and the number is therefore prime.

Section 6, negate the cell to the right and output

Uses this algorithm:

temp0[-]
x[temp0+x[-]]+
temp0[x-temp0-]

I don't need the first line, since my temp is already zero. I also use get to zero a cell instead of a loop. The last line prints the resulting number, a one if prime, or a zero if not.

>[<+>,]+
<[>-<-]
>.

Concluding remarks

Overall, this was a fun challenge. I found the mapping to BF pretty quickly with trail and error using the interpreter and the documentation. I completed it with something like 8 hours of effort. Much of the writing occurred in Notepad++ in BF that I then converted to Sesos with a Python program, tested, and debugged.

Convert BF to Sesos

mbomb007

Posted 2015-09-11T14:06:29.040

Reputation: 21 944

6

Mini-Flak, 202 bytes

Mini-Flak is a turing complete subset of the Brain-Flak language. (It is currently the smallest know turing complete subset of Brain-Flak) It works exactly like Brain-Flak except the <> and [] nilads and the <...> monad are forbidden.

(({})){(({}[()])(((({}({}))[({}[{}])]([()]()))({({}[()((({}()[({})])){{}((({}({})))[{}])}{})]{})}({}{}({})[{}]))[{}]))[{}])}{}{}({}({})[{}()()()]){({}[()((({})){(({}{}(({})))[{}])}{}({}({})[{}]))]{})}{}

Try it online

Explanation

The reason <...> is banned in mini-flak is that it is equivalent to (...)[{}]. So to start this explanation I am going to use this translation in reverse to create an equivalent Brain-Flak program for increased readability for anyone who is already familiar with Brain-Flak.

(({}))
{
 (({}[()])<
  ((({}({}))[({}[{}])]([()]()))<{({}[()((({}()[({})])){{}(<({}({}))>)}{})]{})}({}{}<{}>)>)
 >)
}{}{}({}<{}>[()()()])
{({}[()((({})){(<{}{}(({}))>)}{}({}<{}>))]{})}{}

This program has two main parts, performs the modulus on the input for every number smaller than than the input and leaves them in a stack.

(({}))
{
 (({}[()])<
  ((({}({}))[({}[{}])]([()]()))<{({}[()((({}()[({})])){{}(<({}({}))>)}{})]{})}({}{}<{}>)>)
 >)
}{}{}

This uses a old version of modulo I wrote ({}(<()>)){({}[()((({}()[({})])){{}(<({}({}))>)}{})]{})}({}{}<{}>)

The second part ands together all of the results of the last part

({}<{}>[()()()])
{({}[()((({})){(<{}{}(({}))>)}{}({}<{}>))]{})}{}

If any one of the results is zero the result of all the ands will be zero otherwise it will be truthy and the number will be prime.

Post Rock Garf Hunter

Posted 2015-09-11T14:06:29.040

Reputation: 55 382

So you accepted my challenge, right?

– Erik the Outgolfer – 2016-11-29T12:00:36.253

6

Julia, 46 bytes

n=int(ARGS[1]);show(sum([n%i==0for i=1:n])==2)

An integer is read as the first command line argument using int(ARGS[1]) and the result is printed to STDOUT using show. Primality is checked using trial division with the same formulation as my R answer.

Note that the builtin function isprime uses the Miller-Rabin algorithm, which is probabilistic and is thus unsuitable for this challenge. (Thanks to Martin Büttner for pointing that out.)

Saved 4 bytes thanks to kvill.

Alex A.

Posted 2015-09-11T14:06:29.040

Reputation: 23 761

1Isn't the use of ARGS[1] possible, as an alternative to readline()? – kvill – 2015-09-13T20:38:39.860

@kvill Yeah, I suppose so. Thanks. – Alex A. – 2015-09-13T20:44:15.743

1...and even show() is possible for another byte. – kvill – 2015-09-13T20:52:17.097

@kvill Good call. – Alex A. – 2015-09-13T21:13:47.050

6

GNU sed -r, 19 18

/^(1|(11+)\2+)$/c↲

(that last isn't a literal character; it stands for a final newline).

Takes unary input on stdin. Anything that can be represented by two or more 1s, two or more times is a composite number and replaced by an empty line. Prime numbers are all truthy and left as-is.

There's a slight catch in that this would classify 1 as a prime (it's actually neither prime nor composite), so there are an extra 4 bytes (1|) to handle that case.

If you're willing to accept empty string as a false "output", then we could use d instead of c↲ for a further 1 byte saving.

Toby Speight

Posted 2015-09-11T14:06:29.040

Reputation: 5 058

1I should have looked through the other answers before posting this - I thought I was being original and clever! – Toby Speight – 2015-09-15T14:40:18.150

6

Excel, 41 bytes

=2=SUM(N(0=MOD(A1,ROW(OFFSET(A1,,,A1)))))

Takes input from A1.

4 bytes saved thanks to @Joffan!

Mama Fun Roll

Posted 2015-09-11T14:06:29.040

Reputation: 7 234

This didn't work for me, even entered as an array formula (as I think you intended). However this array formula works: =OR(A1=2,AND(MOD(A1,ROW(INDIRECT("2:"&A1-1))))) at 47 bytes. – Joffan – 2016-06-23T15:21:49.883

You sure? This works fine for me. Put the input number in A1, put the formula in another cell, and it should work. – Mama Fun Roll – 2016-06-23T22:46:15.317

What numbers did you try it with? 9, for example? [BTW I learned an interesting dodge from this, so thanks] – Joffan – 2016-06-23T22:48:57.490

Hmm... interesting. This doesn't work for you? – Mama Fun Roll – 2016-06-23T23:00:48.583

No, it gives TRUE for 9 - maybe Excel version? I'm using 2010 – Joffan – 2016-06-23T23:05:13.420

Weird... idk. I'll look into it when I have time. – Mama Fun Roll – 2016-06-23T23:11:49.317

Shorter version: =2=SUM(N(0=MOD(A1,ROW(OFFSET(A1,,,A1))))) 41 bytes. (again an array formula, reporting on the value at A1) – Joffan – 2016-07-02T00:23:16.050

6

JavaScript (ES6), 43 bytes

This is the shortest solution so far that accepts decimal input. Also, it doesn't use regex like the other short solutions.

p=n=>--d-1?n%d&&p(n):1;alert(p(d=prompt()))

What it uses instead is a recursive function, something that wasn't very useful when you needed to write function and return , but is now very useful because of the => notation.

Ungolfed:

p=n=>                 // p=function(n){ return
--d-1?                // if --d is not 1 (decrement d)
    n%d&&p(n)         // if n divdes d, false, else rerun the function
                      // (d has already been decremented)
:1;                   // else (if d is 1) then true
alert(p(d=prompt()))  // Use the function on the input
                      // and assign this value to d

Jens Renders

Posted 2015-09-11T14:06:29.040

Reputation: 1 476

One byte shorter: n%d?p(n):0 -> n%d?p(n):0 – user81655 – 2016-04-09T11:55:43.297

@user81655 I dont get what you mean. Did you make a typo? it's twice the same thing – Jens Renders – 2016-04-10T16:27:03.400

Haha, I did sorry. I meant to write n%d&&p(n). – user81655 – 2016-04-10T16:29:32.500

@user81655 Nice! I was looking for things like this but did'nt see it :) – Jens Renders – 2016-04-10T16:33:48.703

6

Cubix, 21 bytes

%@\?I:u;>O/)((./0\)?/

Cubix is a 2-dimensional, stack-based esolang. Cubix is different from other 2D langs in that the source code is wrapped around the outside of a cube.

Test it online! Note: there's a 50 ms delay between iterations; see the browser console for current progress.

Explanation

(Note: This is somewhat confusing; I'll add a diagram with colored paths when I get a chance.)

The first thing the interpreter does is figure out the smallest cube that the code will fit onto. In this case, the edge-length is 2. Then the code is padded with no-ops . until all six sides are filled. Whitespace is removed before processing, so this code is identical to the above:

    % @
    \ ?
I : u ; > O / )
( ( . / 0 \ ) ?
    / .
    . .

Now the code is run. The IP (instruction pointer) starts out on the top left char of the far left face, pointing east. Here's an overview of the basic commands:

  • \|/_ are mirrors, and reflect the IP depending on the direction it's traveling.
  • >v<^ set the direction of the IP unconditionally.
  • ? turns the IP right if the top item is positive, or left if it's negative.
  • I inputs an integer (signed or unsigned).
  • O outputs an integer.
  • : duplicates the top item.
  • ; pops an item.
  • @ ends the program.

The first char we encounter is I, which inputs an integer from STDIN. : duplicates this integer. u makes the IP turn right twice, so it ends up on the no-op below u, facing west. Now it enters the main loop.

First, we need to check if this integer is less than 2, in which case it's not prime. So we decrement it twice with ((, then check its sign with the ?. If it's less than 0, the IP is turned left, in which case it wraps around to the bottom-left of the bottom panel, facing north. Removing the direction changes from the next bit, we get 0O@, which pushes a 0, outputs as an integer, and terminates the program.

If the input is more than 2, the IP is turned right at the ?. Next, the top item is incremented once with ). The IP wraps around to the % at the top-left of the top face, which pushes the modulo of the top two numbers. If the input M modulo any number 1 < N < M is 0, the number is not prime. So we check the sign of the top item with ?. If the top item is now 0, it gets output with O, then @ terminates the program.

Otherwise, the IP gets sent down to the ;, which pops the result of % since we have no further use for it. Now it's back where it started, and the loop continues until it takes a different turn at either of the ?s.

There is one more case I didn't mention before: if the sign of the top item is 0 at the first ?, that means we've run through every number 1 < N < M, which in turn means the input is prime. Since the top item must be 0, we increment it with ), then output with O and terminate the program with @.

I think this program is optimal, but I'm not certain. I'll keep looking to find a better solution.

ETHproductions

Posted 2015-09-11T14:06:29.040

Reputation: 47 880

5

J, 19 17 Bytes

echo(p:[:".1!:1)1

Explanation:

echo(p:[:".1!:1)1  | Full program.
echo(          )   | Hook: (f g) y is evaluated as y f (g y)
       [:".1!:1    | Right verb in the hook
           1!:1    | With argument 1, reads a line of input
         ".        | Evaluate, converts valid string to int
       [:          | Cap, treat these two verbs as one
     p:            | With a left argument of 1, as supplied by the hook, tests primality of its right argument
echo               | Print

Bolce Bussiere

Posted 2015-09-11T14:06:29.040

Reputation: 970

I might be missing something, but 1 p: is shorter than 1=#q: (built-in primality testing isn't disallowed as far as I can tell). – cole – 2018-01-16T22:25:35.567

5

Python 3, 52 bytes

p=n=1
exec("p*=n*n;n+=1;"*~-int(input()))
print(p%n)

Saved a byte thanks to xnor in chat.

Lynn

Posted 2015-09-11T14:06:29.040

Reputation: 55 648

It gives an error because there lacks a semicolon in the string. And you could save a character by removing the *n and adding a minus sign in the output. – Labo – 2015-11-09T13:33:14.600

2@Labo That won't work. It would print 2 for input 4. – Dennis – 2015-11-10T17:12:12.483

Yes, sorry, I forgot this case… – Labo – 2015-11-10T20:55:43.867

1Is that a tadpole operator I see? – Yakk – 2015-11-13T15:17:47.867

1Yes! ~-x is shorter than x-1 here, because I can avoid a pair of parentheses. – Lynn – 2015-11-13T16:49:04.850

5

Mathematica, 33 bytes

Print[Mod[(#-1)!^2,#]>0&@Input[]]

or

Print[Length@Divisors@Input[]==2]

Mathematica has a built-in PrimeQ as well, but I believe it uses a probabilistic test.

Martin Ender

Posted 2015-09-11T14:06:29.040

Reputation: 184 808

What is the measurable difference between having and not having "Print@" at the front of that expression? – Eric Towers – 2015-09-13T23:56:44.127

@EricTowers without it, it will only print when run inside a notebook, i.e. in a REPL environment. I wouldn't consider that a full program. If you invoke the code from the command line, you need the Print@ to actually get any output. – Martin Ender – 2015-09-14T06:46:44.537

According to the documentation, PrimeQ is a deterministic algorithm. I suspect the challenge text meant the other meaning (100% correct result). – DanaJ – 2015-09-14T12:00:55.363

You could save a byte with Echo. – Pavel – 2016-11-28T19:23:26.250

@Pavel unfortunately, that also prints >> to STDOUT. – Martin Ender – 2016-11-28T19:33:18.043

5

Pip, 10 8 bytes

1=0Na%,a

Try it online! (testing numbers 0 through 29)

Takes the number as a command-line argument (a) and uses the definition of prime number (has only 1 and itself for factors):

      ,a  Range of numbers from 0 through a-1
    a%    Take a mod each number in range; this is 0 if the number is a divisor,
            nil if the number is 0, and nonzero otherwise
  0N      Count the number of zeros in that list
1=        True (1) if the count is exactly 1, false (0) otherwise
          Print (implicit)

DLosc

Posted 2015-09-11T14:06:29.040

Reputation: 21 213

5

dc, 27 bytes

?dd[d1-d1<f*]dsfxr/r2r|p

How it works (example stack for input 7):

?                             7               push input
 dd                           7 7 7           dup
   [d1-d1<f*]dsf              7 7 7 {fact}    f = factorial macro
                x             7 7 5040        execute
                 r            7 5040 7        swap
                  /           7 720           divide
                   r2r        720 2 7         swap, 2, swap
                      |       1               modular exp: 720^2 mod 7
                       p                      print output

The factorial macro breaks for input 1, or something, but it turns out not to matter, and the output is correct.

Lynn

Posted 2015-09-11T14:06:29.040

Reputation: 55 648

5

Turing Machine Code, 2043 bytes

As usual, I'm using the rule table syntax defined here. Requires unary input.

0 * * r 0
0 _ _ l c
a _ 1 r b
a 0 1 r b
a 1 0 l a
b * * r b
b _ _ r 0
c 1 _ l d
c _ _ * y
d * * l d
d _ _ l a
y _ _ l y
y * * l z
z * * l z
z _ _ r X
X 1 1 r Y
Y _ _ * f
Y * * l Z
X * * l 1
Z * * l 1
1 * a r 2
2 _ b l 3
2 * * r 2
3 a a r 4
3 x x r 4
3 y y r 4
3 * * l 3
4 0 x r 5
4 1 y r 5y
4 b b l 9
9 x 0 l 9
9 y 1 l 9
9 a a r A
5 b b r 6
5 * * r 5
5y b b r 6y
5y * * r 5y
6 _ 0 l 3
6 * * r 6
6y _ 1 l 3
6y * * r 6y
A _ c l 11
A * * r A
11 b b r 12
11 x x r 12
11 y y r 12
11 * * l 11
12 0 x r 13x
12 1 y r 13y
12 c c l B
13x _ 0 l 11
13x * * r 13x
13y _ 1 l 11
13y * * r 13y
B x 0 l B
B y 1 l B
B b b r 21
21 _ d l 22
21 * * r 21
22 1 0 r 23
22 0 1 l 22
23 d d r E
23 * * r 23
E c c r 51
E x x r 51
E y y r 51
E * * l E
51 0 x r Kx
51 1 y r Ky
51 d d l 53
Kx _ 0 l E
Kx * * r Kx
Ky _ 1 l E
Ky * * r Ky
53 x 0 l 53
53 y 1 l 53
53 c c r F
F _ _ l 61
F * * r F
61 1 0 l 62
61 0 1 l 61
61 d d r 70
62 c c l 63
62 * * l 62
63 1 0 r F
63 0 1 l 63
63 b b r 80
70 _ _ l 71
70 * * r 70
71 d d l 72
71 * _ l 71
72 c c * E
72 * * l 72
80 _ _ l Za
80 * * r 80
Za 0 1 r Zb
Za 1 0 l Za
Za d d r Zk
Zb _ _ l Zc
Zb * * r Zb
Zc x x l Zc
Zc y y l Zc
Zc d d r A0
Zc 0 x l $
Zc 1 y l $y
$ d d l Ze
$ * * l $
$y d d l @
$y * * l $y
Ze 0 x r Zf
Ze 1 1 r Zk
Ze x x l Ze
Ze y y l Ze
@ 1 y r Zf
@ 0 0 r Zk
@ x x l @
@ y y l @
Zf _ _ l Zc
Zf * * r Zf
Zk _ _ l Zm
Zk * * r Zk
Zm d d l Zn
Zm * _ l Zm
Zn x 0 l Zn
Zn y 1 l Zn
Zn 0 0 r Zo
Zn 1 1 r Zo
Zo d d l 82
Zo * * r Zo
82 1 0 l 83
82 0 1 l 82
83 c c l 84
83 * * l 83
84 b b l 85
84 * _ l 84
85 a a r 86
85 x x r 86
85 y y r 86
85 * * l 85
86 0 x r &x
86 1 y r &y
86 b b l 90
&x b b r Lx
&x * * r &x
&y b b r Ly
&y * * r &y
Lx _ 0 l 85
Lx * * r Lx
Ly _ 1 l 85
Ly * * r Ly
90 x 0 l 90
90 y 1 l 90
90 a a r 91
91 c c r 51
91 * * r 91
A0 _ _ l A1
A0 * * r A0
A1 d d l A2
A1 * _ l A1
A2 x 0 l A2
A2 y 1 l A2
A2 c c r A3
A3 d d l B0
A3 * * r A3
B0 1 0 r B1
B0 0 1 l B0
B1 d d l B2
B1 * * r B1
B2 1 0 r f
B2 0 1 l B2
B2 c c r t
t * * r t
t _ _ l t2
t2 * _ l t2
t2 _ 1 * halt
f * * r f
f _ _ l f2
f2 * _ l f2
f2 _ 0 * halt

SuperJedi224

Posted 2015-09-11T14:06:29.040

Reputation: 11 342

Nice! I imagine the state names could be golfed a bit more, though... – DLosc – 2015-10-27T23:23:52.963

I'm still surprised this isn't actually the longest answer. (The Mornington Crescent answer is longer, which I guess makes some sense.) – SuperJedi224 – 2015-10-29T01:29:56.567

5

Japt, 1 byte

j

Built-ins are useful for long challenges, but aren't fun in mini-challenges like this. So here's an alternate version without the built-in; still pretty short:

Japt, 8 bytes

o2 e@U%X

Try it online!

How it works

      // Implicit: U = input number. Implicitly place a U at the beginning of the program
o2    // Create an array of all integers from 2 to U. (2o6 = [2,3,4,5])
      // For numbers below 2, this returns n to 2. (2o-3 = [-3,-2,-1,0,1])
e@    // Check if every number X in this range returns truthily to:
U%X   //   the remainder of U divided by X.
      //   In other words, if any of these remainders are 0, return false.
      //   For numbers less than 2, the range contains 1,
      //   so this always returns false for these cases.
      // Implicit: output last expression

ETHproductions

Posted 2015-09-11T14:06:29.040

Reputation: 47 880

5

Seriously 0.1, 2 bytes

,p

, reads a value from STDIN. p pops from the stack and pushes 1 if it is prime, else 0. At EOF, all values on the stack are popped and printed.

Currently, Seriously uses trial division to test primality, so for large inputs it may take a while. In a future version, I'll probably use ECPP or Miller-Rabin.

Try it online

Mego

Posted 2015-09-11T14:06:29.040

Reputation: 32 998

8Seriously. Seriously? – Dennis – 2015-11-09T03:03:21.187

5

Sesos, 63 62 58 bytes

0000000: 1651bc afcddc c4fbbe 3e739e c0f1be 3673c3 eecdf8  .Q.......>s....6s....
0000015: cc75b8 677ce2 b57bc6 78cddc 796de3 7eed33 b7c113  .u.g|..{.x..ym.~.3...
000002a: ef9da3 a0fbbc 77ece7 3adc2b 354e33 f9             ......w..:.+5N3.

The ASM code that I wrote, along with some comments can be run on Try it online:

set numin
set numout
get
sub 1
jmp ; if
    add 1
    jmp ; copy
        fwd 1
        add 1
        fwd 1
        add 1
        rwd 2
        sub 1
    jnz ; end copy
    fwd 2
    sub 1
    jmp ; triplicate loop
        fwd 1
        add 1
        fwd 1
        add 1
        fwd 1
        add 1
        rwd 3
        sub 1
    jnz ; end triplicate
    fwd 1
    sub 1
    fwd 1
    sub 1
    jmp ; factorial loop
        jmp ; multiply loop
            fwd 1
            jmp ; forward add step of multiply loop
                fwd 1
                add 1
                fwd 1
                add 1
                rwd 2
                sub 1
            jnz ; end forward add step of multiply loop
            fwd 2
            jmp ; after add step, reset tape
                rwd 2
                add 1
                fwd 2
                sub 1
            jnz ; end reset tape
            rwd 3
            sub 1
        jnz ; end multiply
        fwd 1
        get ; sets to 0
        fwd 1
        jmp ; transfer intermediate value
            rwd 1
            add 1
            fwd 1
            sub 1
        jnz ; end transfer
        rwd 3
        sub 1
        jmp ; reset loop counter
            fwd 1
            add 1
            rwd 2
            add 1
            fwd 1
            sub 1
        jnz ; end reset
        rwd 1
        jmp ; fix memory location of header
            fwd 1
            add 1
            rwd 1
            sub 1
        jnz ; end fix
        fwd 2
    jnz ; end factorial
    fwd 1
    add 1
    rwd 4
    jmp ; transfer input for mod
        fwd 5
        add 1
        rwd 5
        sub 1
    jnz ; end transfer
    fwd 4
    jmp ; mod
        sub 1
        fwd 1
        sub 1
        jmp ; a
        fwd 1
        add 1
        fwd 2
        jnz ; a
        fwd 1
        jmp ; b
        add 1
        jmp ; c
        sub 1
        rwd 1
        add 1
        fwd 1
        jnz ; b
        fwd 1
        add 1
        fwd 2
        jnz ; c
        rwd 5
    jnz ; end mod
    fwd 1
    get ; sets to zero
    fwd 1
    jmp ; invert a
        rwd 1
        add 1
        fwd 1
        get ; blanks
    jnz ; end invert a
    add 1
    rwd 1
    jmp ; invert b
        fwd 1
        sub 1
        rwd 1
        sub 1
    jnz ; end invert b
jnz ; end if
fwd 1
put

I've never written any BF variant code before, outside of very simple tasks, so I'm sure some of this is not optimal. The divmod algorithm and the logical negation algorithm used were taken from the esolangs algorithms page.

This implements Wilson's theorem. We compute (n-1)! + 1 and then logically negate that value mod n. The factorial code hangs on input 1, so the code is wrapped in an if loop that totally skips running in that case. At the very end, the tape head is manipulated to be over where we would have left the mod value if we ran the code, or over some random zero if the input was 1. I'll add a more thorough explanation when I am done golfing.

FryAmTheEggman

Posted 2015-09-11T14:06:29.040

Reputation: 16 206

4

Symbolic Python, 48 bytes

___=_
__("__=_/_"+";_=-~_;__*=_*_"*~-_)
_=__%___

Try it online!

Uses Wilson's theorem, i.e. that \$(n-1)!^{2} \% n = 1\$ if \$n\$ is a prime, otherwise it equals \$0\$.

Explanation:

___=_                              # Save the value of input to ___
__(                             )  # Evaluate string
   "__=_/_"                        # Initialise __ as 1
           +";             "*~-_   # Then repeat n-1 times
              _=-~_;               # Decrement n
                    __*=_*_        # Multiply __ by n squared
_=__%___                           # Output the value of __ modulo ___

Jo King

Posted 2015-09-11T14:06:29.040

Reputation: 38 234

4

Alchemist, 126 bytes

_->In_a
a->b+c
0e+b+c->g+h
f+0c+b->e+b
e+0h+c->f+c
f+0b+c->c
0e+0f+g->b
0f+h+0s->c
0f+0a+0g+0h+c->f
f+0b+0c+h->s
s+0h->Out_"1"

Try it online!

Outputs 1 for primes, and nothing for composite numbers. Here's a script that tests the program against numbers below 100.

This is rather inefficient, as I removed a restriction that prevented one rule undoing another rule for a sweet one byte save (worth it!). To be much more efficient, we can replace the 0e in the third rule with f and add f to the other side too. Try it online!

Explanation:

_->In_a           # Create the input number of a atoms
a->b+c            # Convert all them to b and c atoms
                  # b will serve as a permanent save of the input
                  # c will be a counter starting at the input

0f+0a+0g+0h+c->f  # Decrement c and start the main loop by setting the f flag
# Note that f and 0e are mostly interchangeable, and the same with 0f and e     
0e+b+c->g+h       # Subtract the counter from the input, keeping a copy of both
f+0c+b->e+b       # When the counter runs out, set the e flag
0f+h+0s->c        # Move the temporary counter atoms back to the main counter
e+0h+c->f+c       # Once done, set the f flag again
f+0b+c->c         # If the counter isn't 0 when the input reaches 0
                  # Then the input is not divisible by the counter

0e+0f+g->b        # Convert the temporary input atoms back to the main input
                  # Reuse the temp counter to main counter rule again

# Once both are done, start the loop over again, decrementing the counter
# This continues until:

f+0b+0c+h->s      # If both the counter and the input reach 0 at the same time
                  # Then the input is currently divisible by the counter
                  # Decrement the temporary counter atom
s+0h->Out_"1"     # If the temp counter is 0 after that (i.e. the highest factor is 1)
                  # Print 1

Jo King

Posted 2015-09-11T14:06:29.040

Reputation: 38 234

4

MATLAB, 24 31 36 bytes

  1. Changed to 31 bytes in order to add in the case of n=1
  2. Changed to 36 bytes to make a full program and to incorporate the 1 byte saving from a suggestion by pawel.boczarski (thanks!)

Since isprime is already taken, an alternative is to take a look at the greatest common divisor of the number in question n with every number from 2 up to n-1. If the GCD of every number in the output is 1, then the number is prime. However, we need to check for the case of n=1, and so by definition, this shouldn't be prime:

n=input('');n-1&all(gcd(n,2:n-1)==1)

This creates an anonymous function that checks if the number is prime and assigns it to the variable f. To call the function, simply do f(n) once it's created with n being a positive integer.

Sample Runs

>> n=input('');n-1&all(gcd(n,2:n-1)==1)
1

ans =

     0

>> n=input('');n-1&all(gcd(n,2:n-1)==1)
2

ans =

     1

>> n=input('');n-1&all(gcd(n,2:n-1)==1)
3

ans =

     1

>> n=input('');n-1&all(gcd(n,2:n-1)==1)
4

ans =

     0

>> n=input('');n-1&all(gcd(n,2:n-1)==1)
5

ans =

     1

>> n=input('');n-1&all(gcd(n,2:n-1)==1)
7

ans =

     1

>> n=input('');n-1&all(gcd(n,2:n-1)==1)
10

ans =

     0

>> n=input('');n-1&all(gcd(n,2:n-1)==1)
13

ans =

     1

>> n=input('');n-1&all(gcd(n,2:n-1)==1)
15

ans =

     0

>> n=input('');n-1&all(gcd(n,2:n-1)==1)
20

ans =

     0

>> n=input('');n-1&all(gcd(n,2:n-1)==1)
23

ans =

     1

>> n=input('');n-1&all(gcd(n,2:n-1)==1)
33

ans =

     0

>> n=input('');n-1&all(gcd(n,2:n-1)==1)
37

ans =

     1

rayryeng - Reinstate Monica

Posted 2015-09-11T14:06:29.040

Reputation: 1 521

Two tips: you can omit f= - the anonymous handle is a perfect solution (2 bytes saved), moreover in case of positive integer n~=1 can be replaced by n-1 (1 byte saved) - I tried in Octave and it worked, should work in Matlab as well. – pawel.boczarski – 2015-09-12T08:59:01.053

@pawel.boczarski thanks! I had the anonymous handle but thought it conflicted with the definition of a "full program". Could you perhaps clarify on what this means? – rayryeng - Reinstate Monica – 2015-09-12T09:02:32.617

Uhm, you're right. This is clarified in the task spec that a full program is demanded, I didn't notice that. But the tip with n-1 instead of n~=1 will still work. – pawel.boczarski – 2015-09-12T09:10:42.040

@pawel.boczarski that'll shave one byte off. Thanks! Also thanks for the clarification – rayryeng - Reinstate Monica – 2015-09-12T09:19:15.617

4

Lua, 56 bytes

n=io.read"*n"p=1
for i=1,n-1 do
p=p*i*i%n
end
print(p%n)

Lynn

Posted 2015-09-11T14:06:29.040

Reputation: 55 648

p=1 for i=1,...-1 do p=p*i*i%...end print(p%...>0) does the job in 50 bytes, by switching the input to command-line argument, and printing true and false instead of 0 and 1. Also, Be aware that 0 evaluates to true and is therefore a truthy value in lua, so your current answer technically always input true. – Katenkyo – 2016-04-11T12:31:28.383

4

Prelude, 125 80 75 59 55 52 bytes

1-^ (1-v#0)#)#1+1-))(0
?(1-
 ^  ^1-( 0#v+^-( 0##10)!

For convenience, I'd suggest using my modified interpreter which reads input and prints output as decimal integers. (Otherwise, I/O would be via a single character's ASCII value.)

It turns out that simple trial division is shorter in Prelude than the squared-factorial approach, because multiplication is fairly expensive. I also found new shortest solutions to compute a modulo and a logical NOT while working on this.

Explanation

The basic algorithm is trial division: check how many numbers in [0,n-1] divide the input n via modulo (where my modulo implementation happens to return i for i%0, such that 0 is never a divisor, but doesn't crash the program either). If there was exactly one divisor (1) we have a have a prime, otherwise we don't. So the print the logical NOT of the number of divisors minus 1.

Let's go through the voices:

  1. This voice counts the number of divisors, and is also used for the outer loop of the modulo.
  2. This voice merely reads the input and then performs the main loop from n-1 down to 0.
  3. This voice does the main work of the modulo computation and computes and prints the logical NOT of the number of divisors.

Finally, we can look at some parts of the code in detail:

  ^ (1-v#0)#)#

    ^1-( 0#v+^-

This is the modulo. The top voice starts by copying n from the bottom voice and the bottom voice copies the current (potential) divisor from the middle voice. The idea is to decrement both values until n is zero, but whenever the divisor hits zero we restore it.

The restoring works like this: we always copy the divisor into the top voice with v. But while the bottom voice is non-zero, we immediately discard it and push a zero instead. This value is then shifted to the bottom voice and added to the current value there.

Finally, the modulo is how far we are into the current cycle, so we subtract the divisor from the result with ^- (which is actually minus the modulo, but we only want to know if it's zero or not, so this doesn't matter).

1-     mod    1+1-))
?(1-
 ^     mod     ( 0##

This is then the main loop. The top voice is initialised to -1 (to account for the fact that 1 is always a divisor), the middle voice reads the input and the bottom voice receives a copy. Before computing the modulo, the middle voice decrements the current divisor.

After the modulo is computed we increment the counter, but decrement it again if the modulo was non-zero.

Finally, we compute the logical not, by pushing a 1 onto the bottom voice but putting a 0 on top if the divisor counter was non-zero, before printing it:

                    (0

                    10)!

Martin Ender

Posted 2015-09-11T14:06:29.040

Reputation: 184 808

4

APL, 21

{∧/0≠⍵|⍨(⍵≠1)+⍳|⍵-2}⎕

Trial division. It would incorrectly state that ¯1 (minus one) is a prime but it's outside the domain so it's all right.

user44932

Posted 2015-09-11T14:06:29.040

Reputation: 61

1Welcome to Programming Puzzles & Code Golf! Because APL has its own legacy code page, we usually count APL as one byte per character here. – Dennis – 2015-09-13T05:28:01.893

4

Fortran 2003, 51 bytes

Here is a solution using Fortran's array capabilities. Fortran 2003 is really only required for the shorter array constructor [ ] instead of the old (/ /).

read*,n
print*,count([(mod(n,i),i=1,n)]==0)==2
end

sigma

Posted 2015-09-11T14:06:29.040

Reputation: 201

4

HPR, 1217 bytes

393 bytes with macros

HPR is an unusable language I created for a challenge here. It has only five commands, and it's not Turing complete; I was actually surprised that a primality test could be programmed in HPR. This answer stretches the conditions of input formats a bit, since the input has to be provided as a descending list of integers from n to 1, encoded in unary and separated by 0s. There has to be a trailing 0, so the number 4 would be encoded as 11110111011010. The program prints 1 if the input is a prime, and an empty line if not. Here's the source code:

$!($)(!(!(-)(#(-)())#(!(-)(#(-)()))())(!(!(-)(#(-)())#(!(-)(#(-)()))())(!(-)(#(-)())#(!(-)(#(-)())*#(!(-)(#(-)()))()!(-)(--))(*#(!(-)(#(-)()))())))!(-)(#(-)())!(-#(!(*)(#(*)())#(!(-)(#(-)()))())(!(-)(#(-)())))(#(-#(!(*)(#(*)())#(!(-)(#(-)()))())(!(-)(#(-)())))())#(-)(#(!(-)(#(-)()))())!(#(*#(!(-)(#(-)()))())(!(-)(#(-)()))-!(-)(!(!(-)(#(-)())#(!(-)(#(-)()))())(!(#(#(!($)(#(*)()#(!(-)(#(-)()))())!(-)(#(-)())*)()#(!(-)(#(-)()))())(!(-)(#(-)())))(-#(!(-)(#(-)()))())#(!(-)(#(-)()))())))(!(!(-)(#(-)())#(!(-)(#(-)()))())(!(!(-)(#(-)())#(!(-)(#(-)()))())($!($)(!(!(-)(#(-)())#(!(-)(#(-)()))())(!(!(-)(#(-)())#(!(-)(#(-)()))())(#(*)()#(!(-)(#(-)()))()))!(-)(#(-)())#(*)(!(-)(#(-)())!(-#(!(*)(#(*)())#(!(-)(#(-)()))())(!(-)(#(-)())))(#(-#(!(*)(#(*)())#(!(-)(#(-)()))())(!(-)(#(-)())))())#(-)(#(!(-)(#(-)()))()))#(!(-)(#(-)()))())!(!(-)(#(-)())#(!(-)(#(-)()))())(!(-)(#(-)())#(*)(!(-)(#(-)())!(-#(!(*)(#(*)())#(!(-)(#(-)()))())(!(-)(#(-)())))(#(-#(!(*)(#(*)())#(!(-)(#(-)()))())(!(-)(#(-)())))())#(-)(#(!(-)(#(-)()))()))#(!(-)(#(-)()))())))-#(!(-)(#(-)()))())-#(!(-)(#(-)()))())!(!(-)(#(-)())#(!(-)(#(-)()))())(!(-)(#(-)())#(!(-)(#(-)())*#(!(-)(#(-)()))()!(-)(--))(*#(!(-)(#(-)()))()))!(-)(#(-)())*#(!(-)(#(-)()))()!(-)(-)

This was not written by hand, but generated from this 393-byte source by TheNumberOne's macro system:

def d 0 !(-)(#(-)())
def l 0 #(<d>)()
def f 1 !({0})(#({0})())
def n 1 !(<d><l>)({0})
def o 2 <n>(<n>({0})){1}
def m 0 <d><f>(-#(!(*)(#(*)())<l>)(<d>))#(-)(<l>)
def x 0 <d>#(*)(<m>)<l>
def z 1 <d>*<l>!(-)(-{0})
def i 0 <d>#(<z>(-))(*<l>)
$!($)(<o>(<i>,<m>!(#(*<l>)(<d>)-!(-)(<n>(!(#(#(!($)(#(*)()<l>)<d>*)()<l>)(<d>))(-<l>)<l>)))(<o>($!($)(<o>(#(*)()<l>,<x>))<n>(<x>),-<l>))-<l>))<n>(<i>)<z>()

Explanation

An HPR program is run in an environment, which is a set containing nonnegative integers and lists. Initially, it contains the input list, and the five commands modify the environment in different ways. However, it's not possible to obtain larger numbers than the maximum of the list, and it's not possible to add new elements to lists. Thus, trial division is the way to go. I'm not going to completely explain the code in this post, but an expanded version with comments can be found here.

Zgarb

Posted 2015-09-11T14:06:29.040

Reputation: 39 083

4

Tcl, 78 75 bytes

Thanks to sergiol

if $argv<2 {exit 1}
incr d
while {[incr d]<$argv} {if $argv%$d==0 {exit 1}}

Original version

if {$argv<2} {exit 1}
incr d
while {[incr d]<$argv} {if {$argv%$d==0} {exit 1}}

Works for all integer values (both negative and arbitrarily large). However, as this aims to be short and not efficient, it is written with a simple divisor={2,3,4,...} loop, so you'll begin getting noticeable lag around eight digit numbers (n ≥ 108).

The input is taken on the command-line; the output is an exit code: 0 for prime and 1 for not prime.

On Windows you can use the following batch file to test it:

@echo off
tclsh a.tcl %1
if ERRORLEVEL 1 (
  echo not prime
) else (
  echo prime
)

Use it as:

C:\foo> run.bat 2017
prime

On *nixen you can use the following bash script to test it:

#! /bin/sh
tclsh a.tcl $1
if [ $? -eq 0 ]
then
  echo prime
else
  echo not prime
fi

Use it as:

% ./run.sh 2017
prime

Enjoy!

Dúthomhas

Posted 2015-09-11T14:06:29.040

Reputation: 541

Please be careful to test stuff before suggesting edits. ! has higher precedence than %, so the ==0 remains... Other edits made from your suggestion. Thanks! – Dúthomhas – 2017-10-23T01:49:35.463

I tested it on my local machine and it seemed to work – sergiol – 2017-10-23T09:18:49.560

4

05AB1E, 1 byte

Code:

p

Explantation:

p     # Implicit input, check whether it is prime or not.

Try it online!

Adnan

Posted 2015-09-11T14:06:29.040

Reputation: 41 965

@Okx Actually, that isn't necessary to state in this challenge, because this challenge explicitly states that newer languages are welcome here. – Adnan – 2017-01-26T17:34:23.003

4

Sesos, 28 24 bytes

0000000: 1651b8 77cd1c 345e33 c67bbe c673b8 676c38 67fe98  .Q.w..4^3.{..s.gl8g..
0000015: 70e201

Uses trial division. Try it online! Check Debug to see the generated binary code.

How it works

The binary file above has been generated by assembling the following SASM code.

set numin     ; Switch to numeric input.
set numout    ; Switch to numeric output.

get, sub 1    ; Read an integer n from STDIN and decrement it.
              ; If n is 1, this will leave a 0 in cell 0.
jmp           ; While loop entry point; if the cell is 0, skip the loop body.
    add 1     ; Add 1 to restore n.

    ; The following is the actual primality test. To see how it works, consult the
    ; corresponding section of this answer.

    jmp, rwd 1, add 1, fwd 2, add 1, rwd 1, sub 1, jnz
    fwd 1
    jmp
        sub 1, rwd 1
        jmp, fwd 1, add 1, rwd 1, sub 1, jnz
        rwd 1
        jmp
            fwd 1, add 1, fwd 1, sub 1, fwd 1, add 1, rwd 1
            jmp, fwd 1, jnz
            fwd 1
            jmp, rwd 1, add 1, fwd 1, sub 1, jnz
            rwd 2
            jmp, rwd 1, jnz
            fwd 1, sub 1
        jnz
        fwd 3
    jnz
    rwd 1, sub 1
    jmp, rwd 1, jnz

jnz           ; While loop exit marker; since the previous instruction also sets
              ; an exit marker, the current cell will be 0 and we exit the loop.
rwd 1, put    ; Retrocede to the previous cell and print its content to STDOUT.
              ; If n > 1, this will print the result from the primality test.
              ; If n = 1, it will simply print 0.

Modulus computation

The core of this answer is its modulus algorithm, which is based on the divmod algorithm from Esolangs. Unlike the latter, it doesn't compute a quotient and works if the divisor is 1.

Suppose the tape in in the following state, where n and d are positive integers.

  v
  n     0     d     0     0     0

To compute n % d, we will decrement the third cell n times. Each time we decrement it, we also increment the fourth cell (so their sum is d at all times), and swap their contents every time the third cell reaches 0.

We have to decrement the first cell once for each iteration, and stop once it reaches 0. Since this will destroy the content of the first cell, we'll also increment the second cell once for each iteration, effectively copying n to the second cell.

Once the first cell reaches 0, the tape will be in the following state.

  v
  0     n   d-n%d  n%d    0     0  

We achieve this as follows.

jmp           ; While the first cell is non-zero:
    fwd 1     ;     Advance to the second cell.
    add 1     ;     Increment it.
    fwd 1     ;     Advance to the third cell.
    sub 1     ;     Decrement it.
    fwd 1     ;     Advance to the fourth cell.
    add 1     ;     Increment it.
    rwd 1     ;     Retrocede to the third cell.
    jmp       ;     While the current cell in non-zero.
        fwd 1 ;         Advance to the next cell.
    jnz       ;     This will advance to the fifth cell if the third is non-zero
              ;     and stay on the third cell otherwise.
    fwd 1     ;     Advance either to the fourth or sixth cell.
    jmp       ;     While the fourth/sixth cell is non-zero:
        rwd 1 ;         Retrocede to the third/fifth cell.
        add 1 ;         Increment it.
        fwd 1 ;         Advance to the fourth/sixth cell.
        sub 1 ;         Decrement it.
    jnz       ;     This will add the fourth/sixth cell to the third/fifth cell,
              ;     zeroing the former in the process. Since the sixth cell has a
              ;     value of 0, this loop is a no-op if the third cell in 0; other-
              ;     wise, it sets the third cell to d and the fourth cell to 0.
    rwd 2     ;     Retrocede to the second/fourth cell.
    jmp       ;     While the current cell is non-zero:
        rwd 1 ;         Retrocede to the previos cell.
    jnz       ;     This places the head to the left of the first cell.
    fwd 1     ;     Advance to the first cell.
    sub 1     ;     Decrement it.
jnz           ; 

Primality test (WIP)

For input n > 1, we can use the modulus algorithm from the previous section to implement a primality test by trial division.

If n is already on the tape, we procede as follows.

  1. Create a copy of n and decrement it to leave the tape as

                      v
          n     0     d     0     0     0
    

    where d = n - 1.

  2. Retrocede to n and use the modulus algorithm to compute n % d, leaving the tape as follows.

          v
          0     n   d-n%d  n%d    0     0  
    
  3. Advance to the cell containing n % d.

    1. If n % d > 0, add d - n % d to n % d to restore d, decrement it, and go back to step 2.

    2. If n % d = 0, we found the first divisor of n (1 for primes); go to step 4.

  4. Since n % d = 0, d - n % d = d.

    We retrocede to that cell, decrement it to leave d - 1 in it (0 if and only if n is prime), and retrocede until finding the first 0 cell. This leaves the tape as

                 v            V                 
          0      0     n     d-1   0    0     0  
    

    where v marks the location of the tape head if d - 1 > 0, and V the location if **d - 1 = 0.

    Note that the value of the cell under the tape head will be 0 in either case. The cell to the left will contain n if n is prime, and 0 if n is composite.

Dennis

Posted 2015-09-11T14:06:29.040

Reputation: 196 637

You should add the BF version of your modulus algorithm to the Esolangs page. – mbomb007 – 2016-07-25T15:35:10.550

Done. – Dennis – 2016-07-25T17:09:50.737

4

brainfuck, 62 bytes

,-[+[<+>>+<-]>[-<[>+<-]<[>+>->+<[>]>[<+>-]<<[<]>-]>>>]<-[<]]<.

This is a port of my Sesos answer. Input and output are character-based. For a character whose code point is prime, the program will print the character itself; otherwise, it will print NUL.

Try it online!

Dennis

Posted 2015-09-11T14:06:29.040

Reputation: 196 637

4

Jellyfish, 13 bytes

p|&*!<
 E   i

Try it online!

Uses the squared-factorial approach based on Wilson's theorem.

Explanation

Jellyfish is Zgarb's language based on his 2D syntax challenge. The semantics are largely inspired by J, but the syntax is where it gets interesting. All functions are single characters and laid out on a grid. Functions take their arguments from the next token south and east of them and return the result north and west. This let's you create an interesting web of function calls where you reuse values by passing them into several functions from multiple directions. There are also higher-order functions called operators, which let you construct more complex functions from the built-in ones.

The only non-functions in the above code are E and &. The former redirects |'s search for its southern argument to the east, so that | actually takes i as that input.

& applied to a single unary function (in this case *) uses the binary definition by supplying the single argument twice. Since the binary definition of * is multiplication, this yields a squaring function.

Taking these things into account, we can write up a more conventional expression:

p(i | (&* ! < i))

Here, i is just the input we're testing for primality. < decrements it, ! computes its factorial, &* squares it. | is modulo although it divides its right-hand operator by its left to compute the remainder. Hence the stuff inside the p(...) computes (n-1)!^2 % n. The p just prints the result.

Martin Ender

Posted 2015-09-11T14:06:29.040

Reputation: 184 808

I'm a little surprised how much you can do with my nonsensical collection of built-ins. :D But thanks for the praise! – Zgarb – 2016-08-03T20:19:11.267

4

WolframAlpha, 10 bytes

isprime(n)

Anastasiya-Romanova 秀

Posted 2015-09-11T14:06:29.040

Reputation: 1 673

haha, this is interesting... – ABcDexter – 2016-08-31T09:56:15.910

Save some bytes with 'n prime'. – Pavel – 2016-12-29T22:40:17.287

4

Logicode, 601 511 467 bytes

circ o(n)->cond n<->0+n/o(n>)
circ p(n)->[
cond n->var c=~((~(o(n)))>)/var c=0
cond (~n)<->var d=p(c)+0/var d=c+1
d
]
circ q(n)->[
cond n->var e=~((~(o(n)))>)/var e=0
cond (~n)<->var f=e+0/var f=q(e)+1
f
]
circ r(a,b)->cond *a&*b->r(q(a),q(b))/a
circ s(a,b)->!(*(r(b,a)))
circ t(a,b)->cond b->t(q(a),q(b))/a
circ u(a,b)->cond s(a,b)->u(t(a,b),b)/!(*a)
circ v(a)->[
var j=p(j)
cond s(a,j)->[
var k=k+u(a,j)
var l=v(a)
]/[
var k=k
]
*((~k)>)
]
var j=1
var k
out v(binp)

Oh my, that is a lot of code.

Here's a rundown of what each circuit does:

  • o is a trimmer, and it strips the extra 0's at the start (this is used for p and q.
  • p is a successor, and q is a decrement.
  • r is a preliminary bit to s (lessthan).
  • s is the lessthan (which uses the remainder checker).
  • t is a subtractor, which calculates a - b for any two positive integers a and b (in binary).
  • u is a mod checker, which returns 1 if a%b is not 0, and 0 if it is 0.
  • v is the actual prime checker, which returns 1 if the number is not prime, and 0 if the number is.
    • The j and k at the bottom are the divisor (b in the mod checker) and the un-bool'd output respectively.

The final line is the "input" bit, which asks for user input in binary (any other character that is not 0 or 1 in the input will be ignored), and returns 1 if the result is not prime, and 0 if the result is.

Edit 1: Saved a whopping 90 bytes (implemented circ/cond one-liners).

Edit 2: Saved another 44 bytes (implemented boolean operator, multi-line conds, and null variables).

clismique

Posted 2015-09-11T14:06:29.040

Reputation: 6 600

3

Haskell, 47 52 bytes

main=do n<-readLn;print$n>1&&all((>0).mod n)[2..n-1]

EDIT: I had failed to take 1 into account. Fixed!

Nicuvëo

Posted 2015-09-11T14:06:29.040

Reputation: 31

1Welcome to ppcg! Nice first post! Congrats on out-golfing the other existing haskell answers! – Rɪᴋᴇʀ – 2017-02-06T15:40:05.327

This code returns True for 1 which is not valid according to the specification. – Laikoni – 2017-02-19T11:42:28.700

3

Alice, 12 11 bytes

/o|\ntdc
@i

Try it online!

Prints 1 for primes and 0 for composite numbers and 1. Here is an alternative solution that prints p-1 for primes instead:

/o|\tzt.
@i

Try it online!

Explanation

/   Send the IP southeast, switching to Ordinal mode.
i   Read all input as a string.
|   Reflect the IP back where it came from.
i   Try reading input again, but this just pushes "".
/   Send the IP west, switching back to Cardinal mode.
    The IP wraps around to the end of the line.
c   Convert the input to an integer and push its prime factors. Pushes nothing
    at all for input 1.
d   Get the stack depth. This is 1 iff the input is a prime.
t   Decrement to give 0 for primes.
n   Logical NOT. Now we have 1 for primes, 0 otherwise.
\   Send the IP southwest, switching to Ordinal mode.
o   Print the result as a string.
@   Terminate the program.

Martin Ender

Posted 2015-09-11T14:06:29.040

Reputation: 184 808

3

q, 35 34 bytes

Haven't golfed much yet, but we can drop 2 bytes easily if we're allowed to return nothing for 1 and 2. Really, the language truly shines in code golf where the seemingly golfed code is just idiomatic q. Using q instead of k lets us use min, mod and til operators to save some bytes.

Having to read STDIN isn't much idiomatic q, and costed us 11 bytes.

-> I was able to save a byte due to on-the-fly assignments which I forgot about

0>min 1,x mod/:2_til x:"I"$read0 0

Explanation

code is executed right-to-left

"I"$read0 0 reads STDIN, and then casts it to an integer

{..} denotes an anonymous function with x as an implicit variable, applied to the integer

til x returns a range 0..(x-1)

2_ drops 0 and 1, which are not needed for prime checking

1,x mod/: then takes mod of x with every element of the list and prepends a 1 to the list due to 1 and 2 returning empty list

0<min takes the minimum of the mods, and checks if it is bigger than zero. If it is, then it must be a prime!

ulucs

Posted 2015-09-11T14:06:29.040

Reputation: 91

3Welcome to PPCG! – Martin Ender – 2018-03-07T22:24:07.600

3

Whitespace, 145 137 134 bytes

[S S S N
_Push_0][S N
S _Duplicate][T N
T   T   _Read_STDIN_as_integer_in_heap_0][T T   T   _Retrieve][S S S T  S N
_Push_2][T  S S T   _Subtract][N
T   T   T   T   N
_Jump_to_Label_FALSE_if_negative][S S S T   N
_Push_1][S N
S _Duplicate_1][N
S S N
_Create_Label_LOOP][S N
N
_Discard_top_stack][S S S T N
_Push_1][T  S S S _Add][S N
S _Duplicate][S N
S _Duplicate][S S S N
_Push_0][T  T   T   _Retrieve_heap_at_0][T  S S T   _subtract][N
T   S T N
_Jump_to_Label_TRUE_if_0][S S S N
_Push_0][T  T   T   _Retrieve_heap_0][S N
T   _Swap_top_two][T    S T T   _Modulo][S N
S _Duplicate][N
T   S T T   N
_Jump_to_Label_FALSE_if_0][N
S N
N
_Jump_to_Label_LOOP][N
S S T   N
_Create_Label_TRUE][S S S T N
_Push_1][T  N
S T _Output_1][N
N
N
_Exit][N
S S T   T   N
_Create_Label_FALSE][S S S N
_Push_0][T  N
S T _Output_0]

Letters S (space), T (tab), and N (new-line) added as highlighting only.
[..._some_action] added as explanation only.

Outputs 1 if a prime, 0 otherwise.

Try it online (with raw spaces, tabs, and new-lines only).

General explanation:

  1. Read input
  2. Is it less than 2: Return 0
  3. Integer i=1
  4. Start loop
    • 5) Increase i by 1
    • 6) If the input and i are equal: Return 1 (last iteration of the loop)
    • 7a) Is the input modulo i 0: Return 0
    • 7b) Else: Go back to step 5

Example run (with 5 as input):

Command   Explanation                   Stack       Heap    STDIN   STDOUT

SSSN      Push 0                        [0]         {}
SNS       Duplicate (0)                 [0,0]       {}
TNTT      Read STDIN as number          [0]         {0:5}   5
TTT       Retrieve heap at 0            [5]         {0:5}
SSSTSN    Push 2                        [5,2]       {0:5}
TSST      Subtract (5-2)                [3]         {0:5}
NTTTTN    Jump to Label_FALSE if neg.   []          {0:5}
SSSTN     Push 1                        [1]         {0:5}
SNS       Duplicate (1)                 [1,1]       {0:5}
NSSN      Create Label_LOOP             [1,1]       {0:5}
 SNN      Discard top                   [1]         {0:5}
 SSSTN    Push 1                        [1,1]       {0:5}
 TSSS     Add (1+1)                     [2]         {0:5}
 SNS      Duplicate (2)                 [2,2]       {0:5}
 SNS      Duplicate (2)                 [2,2,2]     {0:5}
 SSSN     Push 0                        [2,2,2,0]   {0:5}
 TTT      Retrieve heap at 0            [2,2,2,5]   {0:5}
 TSST     Subtract (2-5)                [2,2,-3]    {0:5}
 NTSTN    Jump to LABEL_TRUE if 0       [2,2]       {0:5}
 SSSN     Push 0                        [2,2,0]     {0:5}
 TTT      Retrieve heap at 0            [2,2,5]     {0:5}
 SNT      Swap top two (2,5] -> 5,2])   [2,5,2]     {0:5}
 TSTT     Modulo (5%2)                  [2,1]       {0:5}
 SNS      Duplicate (1)                 [2,1,1]     {0:5}
 NTSTTN   Jump to Label_FALSE if 0      [2,1]       {0:5}
 NSNN     Jump to Label_LOOP            [2,1]       {0:5}

 SNN      Discard top                   [2]         {0:5}
 SSSTN    Push 1                        [2,1]       {0:5}
 TSSS     Add (2+1)                     [3]         {0:5}
 SNS      Duplicate (3)                 [3,3]       {0:5}
 SNS      Duplicate (3)                 [3,3,3]     {0:5}
 SSSN     Push 0                        [3,3,3,0]   {0:5}
 TTT      Retrieve heap at 0            [3,3,3,5]   {0:5}
 TSST     Subtract (3-5)                [3,3,-2]    {0:5}
 NTSTN    Jump to LABEL_TRUE if 0       [3,3]       {0:5}
 SSSN     Push 0                        [3,3,0]     {0:5}
 TTT      Retrieve heap at 0            [3,3,5]     {0:5}
 SNT      Swap top two (3,5] -> 5,3])   [3,5,3]     {0:5}
 TSTT     Modulo (5%3)                  [3,2]       {0:5}
 SNS      Duplicate (2)                 [3,2,2]     {0:5}
 NTSTTN   Jump to Label_FALSE if 0      [3,2]       {0:5}
 NSNN     Jump to Label_LOOP            [3,2]       {0:5}

 SNN      Discard top                   [3]         {0:5}
 SSSTN    Push 1                        [3,1]       {0:5}
 TSSS     Add (3+1)                     [4]         {0:5}
 SNS      Duplicate (4)                 [4,4]       {0:5}
 SNS      Duplicate (4)                 [4,4,4]     {0:5}
 SSSN     Push 0                        [4,4,4,0]   {0:5}
 TTT      Retrieve heap at 0            [4,4,4,5]   {0:5}
 TSST     Subtract (4-5)                [4,4,-1]    {0:5}
 NTSTN    Jump to LABEL_TRUE if 0       [4,4]       {0:5}
 SSSN     Push 0                        [4,4,0]     {0:5}
 TTT      Retrieve heap at 0            [4,4,5]     {0:5}
 SNT      Swap top two (4,5] -> 5,4])   [4,5,4]     {0:5}
 TSTT     Modulo (5%4)                  [4,1]       {0:5}
 SNS      Duplicate (1)                 [4,1,1]     {0:5}
 NTSTTN   Jump to Label_FALSE if 0      [4,1]       {0:5}
 NSNN     Jump to Label_LOOP            [4,1]       {0:5}

 SNN      Discard top                   [4]         {0:5}
 SSSTN    Push 1                        [4,1]       {0:5}
 TSSS     Add (4+1)                     [5]         {0:5}
 SNS      Duplicate (5)                 [5,5]       {0:5}
 SNS      Duplicate (5)                 [5,5,5]     {0:5}
 SSSN     Push 0                        [5,5,5,0]   {0:5}
 TTT      Retrieve heap at 0            [5,5,5,5]   {0:5}
 TSST     Subtract (5-5)                [5,5,0]     {0:5}
 NTSTN    Jump to LABEL_TRUE if 0       [5,5]       {0:5}

NSSTN     Create Label_TRUE             [5,5]       {0:5}
SSSTN     Push 1                        [5,5,1]     {0:5}
TNST      Output top as number          [5,5]       {0:5}          1
NNN       Stop program                  [5,5]       {0:5}

Kevin Cruijssen

Posted 2015-09-11T14:06:29.040

Reputation: 67 575

3

APL, 9 chars/bytes

2≡≢∪∨∘⍳⍨⎕

get the input

compute least common divisor between n and all integers from 1 to n

∨∘⍳⍨

count of unique values

≢∪

if it's exactly 2, then it's prime

2≡

Popov

Posted 2015-09-11T14:06:29.040

Reputation: 101

3

Ruby, 16 + 6 = 22 bytes

[*$<];p$..prime?

or equivalently

p$<.count.prime?

Rules abuse! Kind of, anyway. This answer requires that the input be in unary, and that the character used for input be a newline. Invoke like

ruby -rprime prime_test.rb input

Where input is a file containing n newlines.

I calculate this at 22 bytes: 6 for "rprime" and 16 for the code. However, I also calculate manatwork's answer at 22 bytes if you golf the command line invocation (7 for 'nrprime' and 15 for the code).

histocrat

Posted 2015-09-11T14:06:29.040

Reputation: 20 600

Are you sure that Ruby's prime? doesn't use a probabilistic test? – Martin Ender – 2015-09-14T10:23:16.863

1Yes, it loops through a pseudoprime generator (which returns a superset of the primes) and checks for divisibility of each number less than it. – histocrat – 2015-09-14T12:07:23.773

3

JavaScript, 53 52 40 bytes

alert(/^(?!(..+)\1+$)../.test(prompt()))

Just realized the question allows unary input, which cuts the size down to 40 bytes. Previous answer of 52 bytes is below (ES6):

alert(/^(?!(..+)\1+$)../.test('1'.repeat(prompt())))

Uses that cool unary regex to determine primality.

I actually originally wrote this without seeing the Retina answer, and using a different regex, as seen here: !/^1?$|^(11+?)\1+$/. I moved to the Retina one since saves a character by avoiding the need for negation.

Mwr247

Posted 2015-09-11T14:06:29.040

Reputation: 3 494

I think this (and the other JavaScript answers) should use alert or console.log. Otherwise it's assuming a REPL environment (like a browser console) whereas the challenge explicitly asks for a full program. That said, your own regex wouldn't have been longer than mine, because you only need ^1$ (zero is not a valid input). – Martin Ender – 2015-09-14T10:11:06.920

@MartinBüttner I agree on it needing a print statement, but when doing so puts an otherwise shorter answer behind longer ones that don't follow that rule... I chose instead to mention why I didn't. If the other answers conform, I'll gladly adjust mine as well. Thanks! =)

Good point, hadn't noticed the 0 part. It would be the same length in that case. – Mwr247 – 2015-09-14T15:05:02.203

Well if the others think the same no one will change it. ;) You could just leave a comment (if I haven't done so already) and hope for Dennis to invalidate the answer (to remove it from the leaderboard) if the author doesn't fix it within a couple of days. – Martin Ender – 2015-09-14T15:07:35.120

@MartinBüttner I left a comment on one of them a few days back, but fair enough haha. I suppose I could just edit them all for it as well and update the counts. – Mwr247 – 2015-09-14T15:21:33.567

You can actually shorten the original regex by one character by making it !/^.?$|^(..+)\1+$/ – Aplet123 – 2016-03-27T13:05:44.440

3

O, 23 bytes

j.1>\J2/{Jn%0={0}{}?}dp

Take 2! This one uses trial by division.

kirbyfan64sos

Posted 2015-09-11T14:06:29.040

Reputation: 8 730

3

awk, 30 25 24 bytes

"factor "$0|getline~--NF

Prints the number followed by a colon if it's is prime, prints nothing if it's not.

I check for NF, because the output line from factor has exactly two fields if the input number is prime. The pipe command changes $0, and returns 1 on successful execution. So if NF-1=1 I know we had a prime.

You can use the ~ operator to compare two numbers, if you know that the second number would always be equal or greater.

Example usage:

echo 12347 | awk '"factor "$0|getline~--NF'

My previous idea didn't involve using system commands and is 29 bytes long:

{for(++d;$0%++d&&$0>1;);}d~$0

Prints the number if it's prime and nothing if it's not.

Example usage:

echo 12347 | awk '{for(++d;$0%++d&&$0>1;);}d~$0'

Cabbie407

Posted 2015-09-11T14:06:29.040

Reputation: 1 158

And not only because I touched the ZX81 too: if you should have only 2 fields, it has not to be the 3rd. awk '"factor "$0|getline~!$3', one character less, but it answers prime number for 1 too (that it should be excluded), so I don't know if useful. – Hastur – 2016-07-19T17:53:19.247

3

C, 61 bytes

r;main(i,j){r=(--i>1);for(j=i-1;j>1;)r*=!!(i%j--);return r;}

Uses unary representation - the number is encoded in the number of commandline arguments. The return value is initialized to 1 and then, it's multiplied by the sign of remainder of division of the tested number and numbers 2..i-1 in a loop. So it will be zeroed when any non-trivial divisor found.

The result is returned to the system as the exit code.

Test:

echo 'r;main(i,j){r=(--i>1);for(j=i-1;j>1;)r*=!!(i%j--);return r;}' > main.c
gcc -o main main.c
./main 1 ; echo $?
./main 1 1 ; echo $?
./main 1 1 1 ; echo $?
./main 1 1 1 1 ; echo $?
./main 1 1 1 1 1 ; echo $?
./main 1 1 1 1 1 1 ; echo $?
./main 1 1 1 1 1 1 1 ; echo $?
./main 1 1 1 1 1 1 1 1 ; echo $?
./main 1 1 1 1 1 1 1 1 1 ; echo $?
./main 1 1 1 1 1 1 1 1 1 1 ; echo $?
./main 1 1 1 1 1 1 1 1 1 1 1 ; echo $?

pawel.boczarski

Posted 2015-09-11T14:06:29.040

Reputation: 1 243

3

Fortran 90, 66 55 bytes

Using trial division.

read*,i
do1 j=2,i
1 if(mod(i,j)==0)exit
print*,i==j
end

Saved 11 bytes thanks to sigma.

A do loop with labelled statement (not an enddo or continue) is still valid in Fortran 90, but obsolete.

Alternative using Wilson's theorem (56 bytes):

read*,j
k=1
do1 i=2,j-1
1 k=mod(k*i,j)
print*,k==j-1
end

Ruth Franklin

Posted 2015-09-11T14:06:29.040

Reputation: 760

Aww you beat me, now I'll have to come up with a different Fortran solution! Some tips though: the program statement is not necessary, which will save you 9 bytes, and you can also get away with writing do1 instead of do 1. – sigma – 2015-09-13T20:43:32.937

3

XSLT 3.0, 178 bytes

<transform xmlns="http://www.w3.org/1999/XSL/Transform" xmlns:x="x" version="3.0"><template match="d" expand-text="yes">{.>1 and empty((2 to .-1)[current() mod .=0])}</template></transform>

Posted as an alternative, because it uses a significantly different approach than my previous post. In expanded form, with the usual prefixes:

<xsl:stylesheet
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="3.0">

    <xsl:template match="d" expand-text="yes">{
        . > 1 and empty((2 to . - 1)[current() mod . = 0])
    }</xsl:template>

</xsl:stylesheet>

This method also uses a variant of my XPath solution for primality testing, which is explained there.

Since the default in XSLT is to process all nodes in the input document, this is what happens here as well. For input, you need to hand it a proper, validated XML document with a root element <d>:

<d>101</d>

The result will be output as true or false. It utilizes an XSLT 3.0 feature that allows inline XPath expressions in text nodes. It uses the XSLT 2.0 feature of schema-aware processing. If you don't have such a processor, add the XSD namespace and replace the XPath expression with the following:

. > 1 and empty((2 to . cast as xs:integer - 1)[current() mod . = 0])

For a non schema-aware processor, it would make the code at least 62 bytes larger.

Abel

Posted 2015-09-11T14:06:29.040

Reputation: 153

3

C#, 156 133 130 bytes

(second attempt after the first didn't exactly work)

using System;class C{static void Main(string[]a){int i=2,n=int.Parse(a[0]),b=n-1;for(;i<=n/2;)b*=n%i++>0?1:0;Console.Write(b>0);}}

(and thanks to Dennis for helping me out with my first code golf :) )

helencrump

Posted 2015-09-11T14:06:29.040

Reputation: 161

1Welcome to Programming Puzzles & Code Golf! In its current form, your answer simply prints the result of the last divisibility test. You can fix that by initializing b=1 and multiplying b by the results (b*=...). – Dennis – 2015-09-15T16:05:56.430

I'm not a C# expert, but since C# doesn't have implicit int-to-bool conversion, I don't think 1/0 satisfy the definition of truthy/falsy. using System;class C{static void Main(string[] a){int i=2,n=Int32.Parse(a[0]),b=n-1;for(;i<=n/2;)b*=n%i++>0?1:0;Console.Write(b>0);}} produces the proper output (and is 23 bytes shorter). – Dennis – 2015-09-15T16:14:09.157

Ah, I see. I would have said that 1/0 does satisfy the condition, but I'll stick with your definition. :) – helencrump – 2015-09-15T16:16:48.677

2

For most languages, it does. However, our definition counts 1 as truthy if if(1){...} executes the block, which doesn't seem to wok in C# (again, not an expert).

– Dennis – 2015-09-15T16:20:40.063

1You can remove the space between string[] and a. Also, you can change Int32 to just int. – LegionMammal978 – 2015-09-19T15:53:47.680

Like the Java answer, you can save a few bytes by changing the loop condition to i<n. Although in practice the code i<=n/2 runs faster, the code for this question only has to work, not be fast. Besides, either way, the algorithm isn't very efficient.

– Xynariz – 2016-07-26T17:44:53.363

3

Smalltalk, 47 characters

Obviously no competition for isPrime if the Smalltalk dialect has it, but not all do. For example, the one used in Coding Ground (GNU Smalltalk v3.2.5) does not have it.

I'm relying on the observation that GCD((n - 1)!, n) = 1 for prime n which I haven't seen used very often. Ridiculously bad algorithm, but Smalltalk has no problem working with large integers. Replace the 2 with whatever you want to test:

|n|n:=2.((n>1)&((n-1)factorial gcd:n)=1)inspect

It does not consider 1 as prime as required by the OP. However, one widely accepted definition of a prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. So by this definition, 1 shouldn't be a candidate for primality testing, any more than 0, ½, i, e or π should be.

Note that in some situations, -1 is considered prime, because -1 = 1 × -1, and is used as such in some factorization algorithms.

user15259

Posted 2015-09-11T14:06:29.040

Reputation:

3

J, 4 bytes

1&p:

This is essentially a built-in function for J as p: provides several different prime-related functions, depending on the left argument (attached here with 1&).

As with J in general, it is incredibly array-friendly:

   1&p: 0 1 2 3 4 5 6 7 8 9 10
0 0 1 1 0 1 0 1 0 0 0

Dane

Posted 2015-09-11T14:06:29.040

Reputation: 291

3

Perl, 25 bytes

#!perl -p
$_=2==grep$'%$_<1,//..$_

Counting the shebang as one. Input is taken, in decimal, from stdin.

Sample Usage:

$ echo 101101 | perl isprime.pl

$ echo 101107 | perl isprime.pl
1

Perl, 24 bytes

#!perl -p
$_=3>grep$'%$_<1,//..$_

One byte can be shaved by replacing 2== with 3>, however, it will incorrectly identify both 0 and 1 as prime.

primo

Posted 2015-09-11T14:06:29.040

Reputation: 30 891

3

Scratch, 17 blocks (currently glitchy)

demo
flagstdini2reptilieqaoransmodiiszeroincisayieqans
Self-explanatory...
Edit: I fixed 2 bugs. But, if you enter 1, it gets in an infinite loop...

ev3commander

Posted 2015-09-11T14:06:29.040

Reputation: 1 187

I tried the demo, and entering 7 returns False? – mathmandan – 2015-10-21T21:19:11.900

@mathmandan: Yeah, there were 2 bugs. I'll fix them, but the block count will be the same. – ev3commander – 2015-10-21T22:54:31.560

This seems to loop forever for input 1. – Dennis – 2015-10-22T01:46:18.257

@Dennis: Noted. Will fix with 5 more blocks – ev3commander – 2015-10-22T18:55:58.360

As before: Scratch is typically scored by the number of bytes in the corresponding scratchblocks code here. – SuperJedi224 – 2015-10-29T01:09:12.547

3

Ouroboros, 39 bytes

Sr0s1(
)S1+.@@.@@%!Ms+S.@@.@>6*(6s2=n1(

Each line of code in an Ouroboros program represents a snake eating its tail.

Snake 1

S switches to the shared stack; r0 reads a number from input and pushes a 0. Then s1( switches back to snake 1's stack, pushes a 1, and eats that many characters of the tail. The instruction pointer is on a character that gets eaten, so the snake dies.

Snake 2

Here the magic happens. We check every number from 1 up through n, adding 1 to a tally if it divides our input number. At the end we check whether the number of factors equals 2 and print 1 or 0 accordingly.

) is a no-op the first time through. S switches to the shared stack. We then push a 1 (just after the first snake pushes its 0) and add. The stack now contains the input number and the factor we're testing for divisibility.

.@@.@@%! makes copies of both numbers, takes the modulus, and negates (1 if it is a factor, 0 if not). M moves that result to snake 2's stack, where we're storing the tally of factors; then s+S switches to that stack, adds the top two numbers, and switches back to the shared stack.

Next, .@@.@>6* makes copies of both numbers and tests whether the input number is greater than the test factor, pushing 6 if so and 0 if not. ( then eats that many characters from the end of the snake.

  • If the number is still greater than the factor, the uneaten code now ends after (6. This pushes a 6 and wraps execution back to the beginning. There ) regurgitates the 6 characters we just ate. S does nothing because we're already on the shared stack. 1+ then increments the test factor, and we go through the loop again.
  • When the number is no longer greater than the factor, nothing gets eaten and execution continues. We push a 6 but then switch to snake 2's stack, where the number of factors is sitting. 2=n tests whether it's 2 and outputs the result (1 or 0) as a number. Finally, 1( eats the last character and dies.

Try it out

// Define Stack class
function Stack() {
  this.stack = [];
  this.length = 0;
}
Stack.prototype.push = function(item) {
  this.stack.push(item);
  this.length++;
}
Stack.prototype.pop = function() {
  var result = 0;
  if (this.length > 0) {
    result = this.stack.pop();
    this.length--;
  }
  return result;
}
Stack.prototype.top = function() {
  var result = 0;
  if (this.length > 0) {
    result = this.stack[this.length - 1];
  }
  return result;
}
Stack.prototype.toString = function() {
  return "" + this.stack;
}

// Define Snake class
function Snake(code) {
  this.code = code;
  this.length = this.code.length;
  this.ip = 0;
  this.ownStack = new Stack();
  this.currStack = this.ownStack;
  this.alive = true;
  this.wait = 0;
  this.partialString = this.partialNumber = null;
}
Snake.prototype.step = function() {
  if (!this.alive) {
    return null;
  }
  if (this.wait > 0) {
    this.wait--;
    return null;
  }
  var instruction = this.code.charAt(this.ip);
  var output = null;
  console.log("Executing instruction " + instruction);
  if (this.partialString !== null) {
    // We're in the middle of a double-quoted string
    if (instruction == '"') {
      // Close the string and push its character codes in reverse order
      for (var i = this.partialString.length - 1; i >= 0; i--) {
        this.currStack.push(this.partialString.charCodeAt(i));
      }
      this.partialString = null;
    } else {
      this.partialString += instruction;
    }
  } else if (instruction == '"') {
    this.partialString = "";
  } else if ("0" <= instruction && instruction <= "9") {
    if (this.partialNumber !== null) {
      this.partialNumber = this.partialNumber + instruction;  // NB: concatenation!
    } else {
      this.partialNumber = instruction;
    }
    next = this.code.charAt((this.ip + 1) % this.length);
    if (next < "0" || "9" < next) {
      // Next instruction is non-numeric, so end number and push it
      this.currStack.push(+this.partialNumber);
      this.partialNumber = null;
    }
  } else if ("a" <= instruction && instruction <= "f") {
    // a-f push numbers 10 through 15
    var value = instruction.charCodeAt(0) - 87;
    this.currStack.push(value);
  } else if (instruction == "$") {
    // Toggle the current stack
    if (this.currStack === this.ownStack) {
      this.currStack = this.program.sharedStack;
    } else {
      this.currStack = this.ownStack;
    }
  } else if (instruction == "s") {
    this.currStack = this.ownStack;
  } else if (instruction == "S") {
    this.currStack = this.program.sharedStack;
  } else if (instruction == "l") {
    this.currStack.push(this.ownStack.length);
  } else if (instruction == "L") {
    this.currStack.push(this.program.sharedStack.length);
  } else if (instruction == ".") {
    var item = this.currStack.pop();
    this.currStack.push(item);
    this.currStack.push(item);
  } else if (instruction == "m") {
    var item = this.ownStack.pop();
    this.program.sharedStack.push(item);
  } else if (instruction == "M") {
    var item = this.program.sharedStack.pop();
    this.ownStack.push(item);
  } else if (instruction == "y") {
    var item = this.ownStack.top();
    this.program.sharedStack.push(item);
  } else if (instruction == "Y") {
    var item = this.program.sharedStack.top();
    this.ownStack.push(item);
  } else if (instruction == "\\") {
    var top = this.currStack.pop();
    var next = this.currStack.pop()
    this.currStack.push(top);
    this.currStack.push(next);
  } else if (instruction == "@") {
    var c = this.currStack.pop();
    var b = this.currStack.pop();
    var a = this.currStack.pop();
    this.currStack.push(c);
    this.currStack.push(a);
    this.currStack.push(b);
  } else if (instruction == ";") {
    this.currStack.pop();
  } else if (instruction == "+") {
    var b = this.currStack.pop();
    var a = this.currStack.pop();
    this.currStack.push(a + b);
  } else if (instruction == "-") {
    var b = this.currStack.pop();
    var a = this.currStack.pop();
    this.currStack.push(a - b);
  } else if (instruction == "*") {
    var b = this.currStack.pop();
    var a = this.currStack.pop();
    this.currStack.push(a * b);
  } else if (instruction == "/") {
    var b = this.currStack.pop();
    var a = this.currStack.pop();
    this.currStack.push(a / b);
  } else if (instruction == "%") {
    var b = this.currStack.pop();
    var a = this.currStack.pop();
    this.currStack.push(a % b);
  } else if (instruction == "_") {
    this.currStack.push(-this.currStack.pop());
  } else if (instruction == "I") {
    var value = this.currStack.pop();
    if (value < 0) {
      this.currStack.push(Math.ceil(value));
    } else {
      this.currStack.push(Math.floor(value));
    }
  } else if (instruction == ">") {
    var b = this.currStack.pop();
    var a = this.currStack.pop();
    this.currStack.push(+(a > b));
  } else if (instruction == "<") {
    var b = this.currStack.pop();
    var a = this.currStack.pop();
    this.currStack.push(+(a < b));
  } else if (instruction == "=") {
    var b = this.currStack.pop();
    var a = this.currStack.pop();
    this.currStack.push(+(a == b));
  } else if (instruction == "!") {
    this.currStack.push(+ !this.currStack.pop());
  } else if (instruction == "?") {
    this.currStack.push(Math.random());
  } else if (instruction == "n") {
    output = "" + this.currStack.pop();
  } else if (instruction == "o") {
    output = String.fromCharCode(this.currStack.pop());
  } else if (instruction == "r") {
    var input = this.program.io.getNumber();
    this.currStack.push(input);
  } else if (instruction == "i") {
    var input = this.program.io.getChar();
    this.currStack.push(input);
  } else if (instruction == "(") {
    this.length -= Math.floor(this.currStack.pop());
    this.length = Math.max(this.length, 0);
  } else if (instruction == ")") {
    this.length += Math.floor(this.currStack.pop());
    this.length = Math.min(this.length, this.code.length);
  } else if (instruction == "w") {
    this.wait = this.currStack.pop();
  }
  // Any unrecognized character is a no-op
  if (this.ip >= this.length) {
    // We've swallowed the IP, so this snake dies
    this.alive = false;
    this.program.snakesLiving--;
  } else {
    // Increment IP and loop if appropriate
    this.ip = (this.ip + 1) % this.length;
  }
  return output;
}
Snake.prototype.getHighlightedCode = function() {
  var result = "";
  for (var i = 0; i < this.code.length; i++) {
    if (i == this.length) {
      result += '<span class="swallowedCode">';
    }
    if (i == this.ip) {
      if (this.wait > 0) {
        result += '<span class="nextActiveToken">';
      } else {
        result += '<span class="activeToken">';
      }
      result += escapeEntities(this.code.charAt(i)) + '</span>';
    } else {
      result += escapeEntities(this.code.charAt(i));
    }
  }
  if (this.length < this.code.length) {
    result += '</span>';
  }
  return result;
}

// Define Program class
function Program(source, speed, io) {
  this.sharedStack = new Stack();
  this.snakes = source.split(/\r?\n/).map(function(snakeCode) {
    var snake = new Snake(snakeCode);
    snake.program = this;
    snake.sharedStack = this.sharedStack;
    return snake;
  }.bind(this));
  this.snakesLiving = this.snakes.length;
  this.io = io;
  this.speed = speed || 10;
  this.halting = false;
}
Program.prototype.run = function() {
  this.step();
  if (this.snakesLiving) {
    this.timeout = window.setTimeout(this.run.bind(this), 1000 / this.speed);
  }
}
Program.prototype.step = function() {
   for (var s = 0; s < this.snakes.length; s++) {
    var output = this.snakes[s].step();
    if (output) {
      this.io.print(output);
    }
  }
  this.io.displaySource(this.snakes.map(function (snake) {
      return snake.getHighlightedCode();
    }).join("<br>"));
 }
Program.prototype.halt = function() {
  window.clearTimeout(this.timeout);
}

var ioFunctions = {
  print: function (item) {
    var stdout = document.getElementById('stdout');
    stdout.value += "" + item;
  },
  getChar: function () {
    if (inputData) {
      var inputChar = inputData[0];
      inputData = inputData.slice(1);
      result = inputChar.charCodeAt(0);
    } else {
      result = -1;
    }
    var stdinDisplay = document.getElementById('stdin-display');
    stdinDisplay.innerHTML = escapeEntities(inputData);
    return result;
  },
  getNumber: function () {
    while (inputData && (inputData[0] < "0" || "9" < inputData[0])) {
      inputData = inputData.slice(1);
    }
    if (inputData) {
      var inputNumber = inputData.match(/\d+/)[0];
      inputData = inputData.slice(inputNumber.length);
      result = +inputNumber;
    } else {
      result = -1;
    }
    var stdinDisplay = document.getElementById('stdin-display');
    stdinDisplay.innerHTML = escapeEntities(inputData);
    return result;
  },
  displaySource: function (formattedCode) {
    var sourceDisplay = document.getElementById('source-display');
    sourceDisplay.innerHTML = formattedCode;
  }
};
var program = null;
var inputData = null;
function showEditor() {
  var source = document.getElementById('source'),
    sourceDisplayWrapper = document.getElementById('source-display-wrapper'),
    stdin = document.getElementById('stdin'),
    stdinDisplayWrapper = document.getElementById('stdin-display-wrapper');
  
  source.style.display = "block";
  stdin.style.display = "block";
  sourceDisplayWrapper.style.display = "none";
  stdinDisplayWrapper.style.display = "none";
  
  source.focus();
}
function hideEditor() {
  var source = document.getElementById('source'),
    sourceDisplay = document.getElementById('source-display'),
    sourceDisplayWrapper = document.getElementById('source-display-wrapper'),
    stdin = document.getElementById('stdin'),
    stdinDisplay = document.getElementById('stdin-display'),
    stdinDisplayWrapper = document.getElementById('stdin-display-wrapper');
  
  source.style.display = "none";
  stdin.style.display = "none";
  sourceDisplayWrapper.style.display = "block";
  stdinDisplayWrapper.style.display = "block";
  
  var sourceHeight = getComputedStyle(source).height,
    stdinHeight = getComputedStyle(stdin).height;
  sourceDisplayWrapper.style.minHeight = sourceHeight;
  sourceDisplayWrapper.style.maxHeight = sourceHeight;
  stdinDisplayWrapper.style.minHeight = stdinHeight;
  stdinDisplayWrapper.style.maxHeight = stdinHeight;
  sourceDisplay.textContent = source.value;
  stdinDisplay.textContent = stdin.value;
}
function escapeEntities(input) {
  return input.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
}
function resetProgram() {
  var stdout = document.getElementById('stdout');
  stdout.value = null;
  if (program !== null) {
    program.halt();
  }
  program = null;
  inputData = null;
  showEditor();
}
function initProgram() {
  var source = document.getElementById('source'),
    stepsPerSecond = document.getElementById('steps-per-second'),
    stdin = document.getElementById('stdin');
  program = new Program(source.value, +stepsPerSecond.innerHTML, ioFunctions);
  hideEditor();
  inputData = stdin.value;
}
function runBtnClick() {
  if (program === null || program.snakesLiving == 0) {
    resetProgram();
    initProgram();
  } else {
    program.halt();
    var stepsPerSecond = document.getElementById('steps-per-second');
    program.speed = +stepsPerSecond.innerHTML;
  }
  program.run();
}
function stepBtnClick() {
  if (program === null) {
    initProgram();
  } else {
    program.halt();
  }
  program.step();
}
function sourceDisplayClick() {
  resetProgram();
}
.container {
    width: 100%;
}
.so-box {
    font-family:'Helvetica Neue', Arial, sans-serif;
    font-weight: bold;
    color: #fff;
    text-align: center;
    padding: .3em .7em;
    font-size: 1em;
    line-height: 1.1;
    border: 1px solid #c47b07;
    -webkit-box-shadow: 0 2px 2px rgba(0, 0, 0, 0.3), 0 2px 0 rgba(255, 255, 255, 0.15) inset;
    text-shadow: 0 0 2px rgba(0, 0, 0, 0.5);
    background: #f88912;
    box-shadow: 0 2px 2px rgba(0, 0, 0, 0.3), 0 2px 0 rgba(255, 255, 255, 0.15) inset;
}
.control {
    display: inline-block;
    border-radius: 6px;
    float: left;
    margin-right: 25px;
    cursor: pointer;
}
.option {
    padding: 10px 20px;
    margin-right: 25px;
    float: left;
}
h1 {
    text-align: center;
    font-family: Georgia, 'Times New Roman', serif;
}
a {
    text-decoration: none;
}
input, textarea {
    box-sizing: border-box;
}
textarea {
    display: block;
    white-space: pre;
    overflow: auto;
    height: 50px;
    width: 100%;
    max-width: 100%;
    min-height: 25px;
}
span[contenteditable] {
    padding: 2px 6px;
    background: #cc7801;
    color: #fff;
}
#stdout-container, #stdin-container {
    height: auto;
    padding: 6px 0;
}
#reset {
    float: right;
}
#source-display-wrapper , #stdin-display-wrapper{
    display: none;
    width: 100%;
    height: 100%;
    overflow: auto;
    border: 1px solid black;
    box-sizing: border-box;
}
#source-display , #stdin-display{
    font-family: monospace;
    white-space: pre;
    padding: 2px;
}
.activeToken {
    background: #f93;
}
.nextActiveToken {
    background: #bbb;
}
.swallowedCode{
    color: #999;
}
.clearfix:after {
    content:".";
    display: block;
    height: 0;
    clear: both;
    visibility: hidden;
}
.clearfix {
    display: inline-block;
}
* html .clearfix {
    height: 1%;
}
.clearfix {
    display: block;
}
<!--
Designed and written 2015 by D. Loscutoff
Much of the HTML and CSS was taken from this Befunge interpreter by Ingo Bürk: http://codegolf.stackexchange.com/a/40331/16766
-->
<div class="container">
<textarea id="source" placeholder="Enter your program here" wrap="off">Sr0s1(
)S1+.@@.@@%!Ms+S.@@.@>6*(6s2=n1(</textarea>
<div id="source-display-wrapper" onclick="sourceDisplayClick()"><div id="source-display"></div></div></div><div id="stdin-container" class="container">
<textarea id="stdin" placeholder="Input" wrap="off">5</textarea>
<div id="stdin-display-wrapper" onclick="stdinDisplayClick()"><div id="stdin-display"></div></div></div><div id="controls-container" class="container clearfix"><input type="button" id="run" class="control so-box" value="Run" onclick="runBtnClick()" /><input type="button" id="pause" class="control so-box" value="Pause" onclick="program.halt()" /><input type="button" id="step" class="control so-box" value="Step" onclick="stepBtnClick()" /><input type="button" id="reset" class="control so-box" value="Reset" onclick="resetProgram()" /></div><div id="stdout-container" class="container"><textarea id="stdout" placeholder="Output" wrap="off" readonly></textarea></div><div id="options-container" class="container"><div class="option so-box">Steps per Second:
<span id="steps-per-second" contenteditable>20</span></div></div>

DLosc

Posted 2015-09-11T14:06:29.040

Reputation: 21 213

3

Vitsy, 2 bytes

Yes, it's that Simple...x.

pN
   Implicit grab of STDIN as number, if possible.
p  If it's prime, push 1 to the stack. Else, push 0.
 N Output as number.

Interestingly, adding an i to this will also find prime characters.

ipN

For input % (ASCII character 37) will output 1.

Addison Crump

Posted 2015-09-11T14:06:29.040

Reputation: 10 763

Haha, so punny! +1 from me. – Conor O'Brien – 2015-11-07T19:00:37.530

3

Arcyóu, 42 7 bytes

Note: I added a builtin for primality testing after I submitted this answer. In the interest of completeness, I have left the old answer below, but this is the new official answer:

(p?(#(l

Primality check on a line of input casted to int.


Old answer:

((F(n)(?([ n)(&(f x(_ 2 n)(% n x)))f))(#(l

Arcyóu is a LISP-like golfing language of my own devising.

Explanation:

((F(n)               ; Anonymous function taking one argument n
  (? ([ n)           ; If-statement with condition n-1 (handling the special case)
    (&               ; & is both bitwise AND and an 'all' function
      (f x (_ 2 n)   ; For loop iterating over a range from 2 to n
        (% n x)))    ; n mod x
    f))              ; If n did equal 1, return false
(# (l                ; Now call the function on a line of input casted to int                 

The interpreter allows you to leave off final close-parens, since adding them back is trivial.

jqblz

Posted 2015-09-11T14:06:29.040

Reputation: 2 062

3

, 3 chars / 6 bytes

МȜï

Try it here (Firefox only).

Not sure why I didn't post this earlier...

Bonus solution, 5 chars / 10 bytes

!МȝïꝈ

Try it here (Firefox only).

Checks if the input's array of prime factorization is greater than 0.

Mama Fun Roll

Posted 2015-09-11T14:06:29.040

Reputation: 7 234

3

Jelly, 4 bytes

’!²%

Try it online!

Jelly has a built-in for primality testing (ÆP, 2 bytes), but it uses a probabilistic method.

This answer uses Wilson's theorem instead. For input x, it calculates (x - 1)!² % x, which yields 1 if x is a prime number and 0 if not.

        Input: x

’       Decrement; compute x - 1.
 !      Apply factorial atop the previous result. Yields (x - 1)!.
  ²     Apply square atop the previous result. Yields (x - 1)!².
   %    Do a modulus hook; compute (x - 1)!² % x.

Dennis

Posted 2015-09-11T14:06:29.040

Reputation: 196 637

3

Par, 9 bytes

Counted using its own, non-UTF-8 encoding.

✶↓″↑p~1=*

Explanation:

✶          Parse the input (which is
             implicitly on the stack).    n
 ↓         Subtract one.                  (n-1)
  ″        Duplicate.                     (n-1)  (n-1)
   ↑       Add one.                       (n-1)  n
    p      Prime divisors. For 1, this
             strangely returns (1).       (n-1)  np
     ~     Length.                        (n-1)  np~
      1=   Is the length one? This is
             iff n isn't composite.       (n-1)  noncomposite(n)
        *  Multiply the top of stack.     (n-1)*noncomposite(n)

Lynn

Posted 2015-09-11T14:06:29.040

Reputation: 55 648

3

Binary-Encoded Golfical, 13+1 (-x flag) = 14 bytes

This can converted to the standard graphical version using the included Encoder utility, or run directly by adding the -x flag.

Hex dump:

00 40 02 15 14 49 1b 00 00 00 01 17 17

The original image:

enter image description here

Zoomed in 100x with color value lables:

enter image description here

Explanation:

10,0,0->Input number
14,3,0->Turn right if prime
11,0,1->Go east
0,0,0->Set to 0
0,0,1->Set to 1
10,1,0->Print number

SuperJedi224

Posted 2015-09-11T14:06:29.040

Reputation: 11 342

3

MediaWiki templates with ParserFunctions, 101 + 1 = 102 bytes (for title)

{{#ifexpr:{{{n}}} mod {{{f|(n-1)}}}==0|false|{{#ifexpr:{{{f}}}==1|true|{{:p|n=n|f={{#expr:f-1}}}}}}}}

Ungolfed:

{{#ifexpr:{{{n}}} mod {{{f|(n-1)}}}==0|false|
    {{#ifexpr:{{{f}}}==1|true|
        {{p|n=n|f={{#expr:f-1}}}}
    }}
}}

This recursive trial division method theoretically works, but to determine the primality of a positive integer n, $wgMaxTemplateDepth (in the MediaWiki config) must exceed n - 2.

DuhHello

Posted 2015-09-11T14:06:29.040

Reputation: 499

So, in practice, this works for no single input? According to the rules, it must work at least for integers 1 to 255... – Dennis – 2015-12-21T20:25:13.540

This solution is valid for wikis that allow at least a recursive depth of 253. – DuhHello – 2015-12-21T20:29:29.450

3

Samau, 2 bytes

▌τ

Hex dump:

dd ab

Yes, it's 2 bytes. Samau uses CP737 as its default character encoding.

▌       read a number
 τ      test if it is a prime

τ uses the isCertifiedPrime function in the haskell package arithmoi. It's not a probabilistic algorithm.


There's also a 7-bytes answer if built-ins are not allowed:

▌;\│Σ2=

Hex dump:

dd 3b 5c b3 91 32 3d

Most mathematical functions in Samau automatically thread over lists.

▌           read a number, let's call it n
 ;          duplicate
  \         range from 1 to n
   |        return 1 for divisors of n, and 0 for the other numbers
    Σ       take the sum
     =2     if the sum is 2, then it's a prime

alephalpha

Posted 2015-09-11T14:06:29.040

Reputation: 23 988

3

JavaScript (ES6), 54 bytes

for((x=prompt(a=i=1))>1||a--;++i<x;x%i?0:a=0);alert(a)

Outputs 1 for prime, 0 for non-prime. All four JS solutions so far were based on the regex, so I thought I'd be brave and try one without.

ETHproductions

Posted 2015-09-11T14:06:29.040

Reputation: 47 880

3

Befunge 93, 44 bytes

&:v>0.@       @.-1<
03<_v#%p03+1:g03::_^#`g

This works by trial division.

There's a hidden unprintable character between the v> on the first line; it's the character whose value is 2. The base64 of the file is as follows:

Jjp2Aj4wLkAgICAgICAgQC4tMTwKMDM8X3YjJXAwMysxOmcwMzo6X14jYGc=

Opening it as hex in Sublime Text looks like this (newline confusion, though):

263a 7602 3e30 2e40 2020 2020 2020 2040
2e2d 313c 0d0a 3033 3c5f 7623 2570 3033
2b31 3a67 3033 3a3a 5f5e 2360 670d 0a

Try this out here.

Justin

Posted 2015-09-11T14:06:29.040

Reputation: 19 757

3

Lambda Calculus, 615 bytes

(\p.(\g.(\x.g(x x))(\x.g(x x)))(\f n d.((\n.n(\x.(\x y.y))(\x y.x))((\n f x.n(\g h.h(g f))(\u.x)(\u.u))d))(\x y.x)(((\g.(\x.g(x x))(\x.g(x x)))(\f n m.((\m n.(\l r.l r(\x y.y))((\m n.(\n.n(\x.(\x y.y))(\x y.x))((\m n.n(\n f x.n(\g h.h(g f))(\u.x)(\u.u))m)m n))m n)((\m n.(\n.n(\x.(\x y.y))(\x y.x))((\m n.n(\n f x.n(\g h.h(g f))(\u.x)(\u.u))m) m n))n m))n m)(\x y.x)(((\m n.(\n.n(\x.(\x y.y))(\x y.x))((\m n.n(\n f x.n(\g h.h(g f))(\u.x)(\u.u))m)m n))n m)(\x y.y)(f((\m n.n(\n f x.n(\g h.h(g f))(\u.x)(\u.u))m)n m)m)))n d)(\x y.y)(f n((\n f x.n(\g h.h(g f))(\u.x)(\u.u))d))))p((\n f x.n(\g h.h(g f))(\u.x)(\u.u))p))

Inspired by the turing machine solution, here is a Lambda Calculus solution! You can test this code in this lambda evaluator, or in this one. The second one stops every 400 reductions so its more stable on big inputs (>11), but the first one is by far the nicest. Just paste the code in the text box and type a number behind it.

For example

(\p.(\g.(\x.g(x x))(\x.g(x x)))(\f n d.((\n.n(\x.(\x y.y))(\x y.x))((\n f x.n(\g h.h(g f))(\u.x)(\u.u))d))(\x y.x)(((\g.(\x.g(x x))(\x.g(x x)))(\f n m.((\m n.(\l r.l r(\x y.y))((\m n.(\n.n(\x.(\x y.y))(\x y.x))((\m n.n(\n f x.n(\g h.h(g f))(\u.x)(\u.u))m)m n))m n)((\m n.(\n.n(\x.(\x y.y))(\x y.x))((\m n.n(\n f x.n(\g h.h(g f))(\u.x)(\u.u))m) m n))n m))n m)(\x y.x)(((\m n.(\n.n(\x.(\x y.y))(\x y.x))((\m n.n(\n f x.n(\g h.h(g f))(\u.x)(\u.u))m)m n))n m)(\x y.y)(f((\m n.n(\n f x.n(\g h.h(g f))(\u.x)(\u.u))m)n m)m)))n d)(\x y.y)(f n((\n f x.n(\g h.h(g f))(\u.x)(\u.u))d))))p((\n f x.n(\g h.h(g f))(\u.x)(\u.u))p))
4

gives

\x y. y  ;false

And

(\p.(\g.(\x.g(x x))(\x.g(x x)))(\f n d.((\n.n(\x.(\x y.y))(\x y.x))((\n f x.n(\g h.h(g f))(\u.x)(\u.u))d))(\x y.x)(((\g.(\x.g(x x))(\x.g(x x)))(\f n m.((\m n.(\l r.l r(\x y.y))((\m n.(\n.n(\x.(\x y.y))(\x y.x))((\m n.n(\n f x.n(\g h.h(g f))(\u.x)(\u.u))m)m n))m n)((\m n.(\n.n(\x.(\x y.y))(\x y.x))((\m n.n(\n f x.n(\g h.h(g f))(\u.x)(\u.u))m) m n))n m))n m)(\x y.x)(((\m n.(\n.n(\x.(\x y.y))(\x y.x))((\m n.n(\n f x.n(\g h.h(g f))(\u.x)(\u.u))m)m n))n m)(\x y.y)(f((\m n.n(\n f x.n(\g h.h(g f))(\u.x)(\u.u))m)n m)m)))n d)(\x y.y)(f n((\n f x.n(\g h.h(g f))(\u.x)(\u.u))d))))p((\n f x.n(\g h.h(g f))(\u.x)(\u.u))p))
5

gives

\x y. x ;true

Jens Renders

Posted 2015-09-11T14:06:29.040

Reputation: 1 476

3

MATL, 2 bytes

Zp

Try it online!

Takes input implicitly. For positive input, function Zp outputs true (displayed as 1) if the number is prime, ans false (0) otherwise.

Luis Mendo

Posted 2015-09-11T14:06:29.040

Reputation: 87 464

3

Turtlèd, 490 487 451 bytes

Turtlèd does not support newlines in code... so oneliner fun!

golfed some bytes for not needing to support 0

Golfed some bytes removing useless code, and some other tricks

?#0#.:l( >;,u,[ :ll[*,l]d],u{*{*r}l' d{ l}[ (*.d)(0'1d)(1'2d)(2'3d)(3'4d)(4'5d)(5'6d)(6'7d)(7'8d)(8'9d)(9.l( .))]u[ r]lu}u2[#[ ;{ l}[ (0u.)(1u'1)(2u'2)(3u'3)(4u'4)(5u'5)(6u'6)(7u'7)(8u'8)(9u'9)dl]ur[ r]l[#[ r]l[ (0'9l( '#;))(1.;)(2'1;)(3'2;)(4'3;)(5'4;)(6'5;)(7'6;)(8'7;)(9'8;)]uuu[*r]{*l}u{*r}'*{*l}:;{ l}[ l]r]' uu[*r]{*r}d]l(*,(*@1)(1@0)'*)u{*' l}:;d[ (0'9l( '#l))(1.d)(2'1d)(3'2d)(4'3d)(5'4d)(6'5d)(7'6d)(8'7d)(9'8d)]r( u[ r]uu)][ [ l]r[ ' r]ul],)

Try it online!

I am most certainly not going to explain this with the annotations for each part of the code, at least not right now.

General explanation:

The program writes 0, takes integer input, if it is not one (if it is, it skips the rest of the code), it writes out two lines of asterisks, removing the zero that was written, one to compare for the prime checking, one to turn into a decimal string. It turns the lower one into a decimal string, then moves the decimal string up one. It writes out a single asterisk into a line above the other for each decrement of the upper decimal string. when goes below zero, the program moves the decimal string back up again, and keeps going on the upper line until it either aligns with the lower line, or goes past it. If it aligns, it sets the character variable to 1, if the character variable has not already been set to one. This is because it has no method to distinguish one when testing divisibility, so this makes it so it has to have more than one factor match. If it has been set to one, it sets it to zero. After it has tested all the numbers from n-1 to 1, it cleans up all the mess that it used to test the primality, then writes the character variable, which will be one if prime, else 0

Destructible Lemon

Posted 2015-09-11T14:06:29.040

Reputation: 5 908

Input 1 seems to result in an infinite loop. – Dennis – 2016-09-25T01:45:48.953

@Dennis Fixed!! – Destructible Lemon – 2016-09-25T01:59:33.953

@ConorO'Brien Submissions must terminate by default.

– Dennis – 2016-09-25T03:42:41.077

@Dennis oh ok then – Conor O'Brien – 2016-09-25T03:48:32.933

I'm fairly sure I can golf some bytes, but the code is too.... ehhhh. It bugged out when I tried – Destructible Lemon – 2016-10-30T23:29:40.223

2

Clojure, 98 bytes

(let[n(Integer/parseInt(read-line))](println(and(not= 1 n)(not(some #(=(rem n %)0)(range 2 n))))))

Uses some to check if the number has any divisors, then negates the result to indicate whether or not it's prime.

A simple function would have only been 59 bytes :(

(fn[n](and(not= 1 n)(not(some #(=(rem n %)0)(range 2 n)))))

Ungolfed:

(let [n (Integer/parseInt (read-line))]
  (println
    (and (not= 1 n) ; 1 is an unfortunate special case
         (not ; Negate to indicate primality
           (some #(= (rem n %) 0) ; Check if n has any divsors...
                 (range 2 n)))))) ; in the range of 2 to (n-1)

There's a previous Clojure answer, but this beats it by a little over 60 bytes

Carcigenicate

Posted 2015-09-11T14:06:29.040

Reputation: 3 295

You can also turn Integer/parseInt to read-string to save a further 5 bytes. – clismique – 2017-07-09T11:38:20.970

1: You can replace (not= 1 n) with (> n 1) to handle the edgecase of 1. 2: You can replace (not (some ...)) with (every? ...) and change the condition around a bit. 3: You can swap the fn[n] and the shorthand around to remove a space, like so: #(and(> % 1)(every?(fn[a](>(rem % a)0))(range 2 %))). These three save 7 bytes. – clismique – 2017-07-09T11:46:23.753

@Qwerp-Derp Thanks. I'll try to remember to update my answer later today. – Carcigenicate – 2017-07-09T13:09:17.897

@Carcigenicate Here's a quick reminder. :P – totallyhuman – 2017-11-17T00:30:04.027

@totallyhuman LOL. JULY. I clearly dropped that ball. I hate to delay this again, but I'm not quite on the mental state to deal with this right now. I think I'll set a reminder on my phone – Carcigenicate – 2017-11-17T00:31:49.217

2

Swift 4, 58 bytes

let n=Int(readLine()!)!;print((1..<n).filter{n%$0<1}==[1])

Try it online!

Saved a few bytes thanks to Dennis.

Mr. Xcoder

Posted 2015-09-11T14:06:29.040

Reputation: 39 774

2

Broccoli, 107 bytes

(fn p ($a) (:= $d 0) (for $i in (range 2 $a) (if (= $a (* $i (int (/ $a $i)))) (:= $d (+ $d 1)))) (= $d 1))

With whitespace added:

(fn p ($a)
    (:= $d 0)
    (for $i in (range 2 $a)
        (if (= $a (* $i (int (/ $a $i))))
            (:= $d (+ $d 1))
        )
    )
    (= $d 1)
)
(map p (range 1 20))

This language is a bit of a pain to work with:

  • You can't take input.

  • You can't define a function from within a function.

  • You can't pass a lambda to a map or reduce or a filter.

  • There is no modulo function.

I'm sure this could be shorter somehow, but I'm just not seeing it.

James

Posted 2015-09-11T14:06:29.040

Reputation: 54 537

2

Wumpus, 13 bytes

I=&l2-&*=*r%O

Try it online!

Explanation

Uses Wilson's theorem in the form (n-1)!2isprime(n) (mod n) (where isprime gives 0 or 1, respectively).

The implementation doesn't really work the way it's supposed to for n = 1, but the result still ends up being 0.

I=  Read n and duplicate it.
&l  Push the stack depth n times. Since there's another copy of n on the stack to
    begin with, this pushes 1, 2, ..., n-1, n.
2-  Subtract 2 from n.
&*  Multiply the top two numbers that many times. This essentially folds
    multiplication over the range [1,n-1], which computes (n-1)!. At
    the end of this, the stack will be [n, (n-1)!]. However, in the case
    of n = 1, we only ever pushed one element for the range, so there's
    nothing left to form the factorial and the stack contains only one
    copy of 1.
=*  Square the (n-1)!.
r   Reverse the stack. This is normally just used to swap n and (n-1)!²,
    but we're using stack reversal instead of swap (~), so that the stack
    remains unchanged for n = 1.
%   Modulo. For n = 1, this computes 0 % 1 = 0 (because there's an implicit
    zero underneath the 1 on the stack). For all other n, this computes
    (n-1)!² % n, i.e. the result of the primality test.
O   Output the result as a decimal integer.

The IP then bounces off the end of the program and starts moving left again. % tries another modulo, but there's only implicit zeros on the stack, so the program ends due to the attempted division by zero.

Martin Ender

Posted 2015-09-11T14:06:29.040

Reputation: 184 808

2

Stax, 2 bytes

|p

Run and debug online!

Added for completeness. An internal.

Weijun Zhou

Posted 2015-09-11T14:06:29.040

Reputation: 3 396

2

Perl 6, 20 bytes

say get~~$_%%one ^$_

Try it online!

There's also the built-in is-prime, but I thought it would be more interesting to do it without it. Surprisingly, this is only 4 bytes longer.

Explanation:

    get                # Get a line of input
       ~~              # Smartmatch it by setting $_ to input
         $_%%          # Is input divisible by
             one       # Only one of
                 ^$_   # The range 0 to input-1?
say                    # And print

Note that this works because n%%0 is boolified to False.

Jo King

Posted 2015-09-11T14:06:29.040

Reputation: 38 234

2

05AB1E, 3 bytes

fнQ

find all factors and see if last one equals input.

Try it online!

Jackson

Posted 2015-09-11T14:06:29.040

Reputation: 101

1Built-ins are allowed, so just p is enough. – Grimmy – 2019-03-29T13:32:54.087

2

Gol><>, 4 bytes

ISPh

Thanks to ASCII-only for letting me now I didn't need 2 bytes, since it was redundant.

Try it online!

KrystosTheOverlord

Posted 2015-09-11T14:06:29.040

Reputation: 681

1The ?1 is needed?????? – ASCII-only – 2019-02-20T16:07:28.910

@ASCII-only Thank you for pointing that out, I didn't even think of that. Fixing it now! – KrystosTheOverlord – 2019-02-20T17:23:10.570

2At least in this particular implementation of Gol><>, P is implemented by theis_probably_prime function. Unfortunately, this challenge requires a deterministic primality test. – Dennis – 2019-02-20T19:37:03.830

@Dennis The is_probably_prime function is a wrapper for the sympy isprime function, which is deterministic up to $2^{64}$. If the sympy library is not available, then it does a non-deterministic check. – Jo King – 2019-03-15T01:58:14.540

2

dc, 17 22 bytes

?d*5+v[dz%rdz<M*]dsMxp

Try it online!

I've been on a bit of a dc kick lately and although there's already a dc solution here, this is 10 5 bytes shorter. It follows the I/O request of the original question, which does add two bytes for the explicit input and output (rather than the usual rules these days that allow implicit stack I/O), but okay.

This outputs zero for composite numbers and non-zero for primes.

How it works: this program builds a stack consisting of the modulo of the input with the current stack size (so the stack looks like, from bottom to top, i%2, i%3, i%4, ...), then when it decides it's gone far enough (stack size = input) it multiplies the stack as it unwinds the recursion.

Sophia Lechner

Posted 2015-09-11T14:06:29.040

Reputation: 1 200

2This fails for 1 and 2. – Ørjan Johansen – 2019-03-16T00:22:00.390

Oof, you're right. Not sure how I missed that. It took me five bytes to fix (by precomposing with d*5+v, i.e., x -> int(sqrt(x^2+5)), i.e., 1->2, 2->3, n->n for n>2) – Sophia Lechner – 2019-03-18T18:09:13.040

2

Java, 108 bytes

interface P{static void main(String[]a){long l=new Long(a[0]),i=1;for(;0<l%++i%l;);System.out.print(l==i);}}

Try it online!

Port of my Ink answer. Would beat all existing answers in C, C#, Python (2 and 3), and possibly other languages if ported.

Ungolfed

interface PrimeChecker {
    // Unlike members of classes, members of interfaces are public by default. 
    static void main(String[] args) {
        long input = new Long(args[0]),
             div = 1;
        for (; 0 < (  input % (++div) // Trial division, finish when div divides input
                    % input           /* If input is at least 2, this has no effect.
                                         But (1 % n) = 1 for any n > 1
                                         so without this, the loop would never end
                                         when the input is 1 */
                   );
            ) { /* The loop body is empty, we're just using it to set div */ }
        // div is now the lowest number greater than 1 that divides the input
        // (or 2, if the input is 1)

        // The input is prime iff that number is equal to the input
        System.out.println(input == div);
    }
}

Sara J

Posted 2015-09-11T14:06:29.040

Reputation: 2 576

Note for those who, like me, didn't understand the existence of the last %l at first, it's to shortcut the loop when the input is 1. – Olivier Grégoire – 2019-05-16T15:42:16.100

2

Shakespeare Programming Language, 300 176 bytes

(whitespace added for readability)

h.Ajax,.Puck,.Act I:.Scene I:.[Enter Ajax and Puck]
Ajax:Listen tothy.
You is the remainder of the quotient betweenthe square ofthe factorial ofthe sum ofyou a pig you.
Open heart

Try it online!

Explanation: Uses Wilson’s theorem to find primality. Due to integer constraints, only works properly for numbers up to 9, but theoretically would work for any integer.

I saved a ton of bytes by using builtins for square and factorial that I didn’t know existed before.

Hello Goodbye

Posted 2015-09-11T14:06:29.040

Reputation: 442

2Welcome to Code Golf! Unfortunately, the rules require your program to work for all integers between 1 and 255. – Dennis – 2019-10-09T14:44:30.103

I'd say that this only doesn't work due to the implementation. If I'm not mistaken, it never says in the specs for SPL that integers should be bounded, so this would work if the interpreter matched the specification – EdgyNerd – 2019-10-09T14:59:54.210

2

@EdgyNerd The implementation defines the language. The specification is irrelevant.

– Dennis – 2019-10-09T15:12:22.083

Well, there are quite a few Wilson’s Theorem answers here, and I doubt all of them can calculate 254! exactly... – Hello Goodbye – 2019-10-09T15:23:25.513

2

JavaScript, 50 47 bytes

n=+prompt();for(i=2;n%i&&i*i<n;i++);n<3?n-1:n%i

This is a basic translation of @steveverrill's C answer :)

Sam

Posted 2015-09-11T14:06:29.040

Reputation: 153

1"Write a full program that..." – FryAmTheEggman – 2015-09-11T15:10:17.930

It is now a full program, and I have attributed you at the bottom of my answer :) – Sam – 2015-09-11T15:15:19.947

6I believe you need an alert or something to output from a full program in JS? – FryAmTheEggman – 2015-09-11T15:17:13.737

2@FryAmTheEggman I'd say that a web browser's console counts as a standard JS interpreter, and pretty much all consoles output the result. This outputs 0 (which is falsy) if the number is not prime. – Reinstate Monica -- notmaynard – 2015-09-11T16:14:30.943

You could save three bytes by getting rid of the 'n' in prompt('n'). I believe moving the i++ into the condition, like so: for(i=2;n%i&&i*i++<n;); would also save a byte. – ETHproductions – 2015-09-11T16:35:06.653

@Sam Per this: http://meta.codegolf.stackexchange.com/questions/803/javascript-standards-for-io

We should be using alert or some other method to output with JavaScript. I've been hit with it before too.

– Mwr247 – 2015-09-11T19:29:51.607

@ETHproductions & is binary AND. i*i<n gives true or false (converted to binary 1 or 0). n%i can give an even number like 4. 4 && true is true, but 4 & 1 is 0 – edc65 – 2015-09-11T19:30:16.097

@Sam Drop the + from before prompt(). It saves one byte. Also, you can write the first bit like this, saving another byte: for(n=prompt(i=2); – Toothbrush – 2015-09-13T19:09:22.190

2

GolfScript, 15 bytes

~:z,{)z\%!},,2=

This uses trial division:

  • stores the input in variable z
  • computes the remainder of dividing z by all numbers from 1 to z
  • expects to find a remainder of 0 exactly 2 times

Try it here.

Cristian Lupascu

Posted 2015-09-11T14:06:29.040

Reputation: 8 369

2

Ruby, 21 + 1 = 22 19 + 1 = 20 bytes

Also throwing my hat into the Ruby battle using the regex approach (and improved using histocrat's suggestion):

p !/^(11+)\1+$|^1$/

Takes input as a unary string and invoked using the n flag:

$ ruby -ne 'p !/^(11+)\1+$|^1$/' <<< 11111
true

Reinstate Monica -- notmaynard

Posted 2015-09-11T14:06:29.040

Reputation: 1 053

Ooh, you can drop two in fact. p !/^(11+)\1+$|^1$/ – histocrat – 2015-09-11T18:25:40.767

@histocrat Interesting. I didn't know about that. However, it gives me warning: regex literal in condition. Since the rules state "if possible, output should consist solely of the string representation of a truthy or falsy value," I'd need to add the W0 flag, so it's a wash. Thanks for the tip, though. – Reinstate Monica -- notmaynard – 2015-09-11T18:52:14.380

@histocrat A-ha, it only gives that warning when I run it from a file rather than as a one-liner. So thank you indeed for that. – Reinstate Monica -- notmaynard – 2015-09-11T20:45:28.653

2

Perl, 38 or 47 bytes

use ntheory":all";say is_prime(shift);

Prints 0 if composite, 2 if definitely prime, 1 if a probable prime. The results for all 64-bit inputs are deterministic. With 0.53+ this extends to 82-bit inputs. It's also sometimes done for up to 200-bit inputs (special form or very easy proof). The probable prime test is an extra-strong BPSW test followed by 1-5 random-base Miller-Rabin tests.

use ntheory":all";say is_provable_prime(shift);

Uses BLS75-T5 or ECPP to prove the result. Deterministic for all inputs. Very fast to ~500 digits, works pretty well to 1000 or so digits.

This is included in Strawberry Perl for Windows. Certainly using a module is iffy under the golfing loopholes, but this is a normal module and Perl is all about CPAN. It's also ridiculously faster and more useful than the clever but stupid regex.

DanaJ

Posted 2015-09-11T14:06:29.040

Reputation: 466

What does "probably prime" mean? – ASCIIThenANSI – 2015-09-11T19:02:16.173

2

It means it passed some probabilistic prime test that, very rarely, might give a false positive, and you may optionally want to run a more thorough test.

– Lynn – 2015-09-11T19:11:06.850

For ntheory, it means it passed an ES BPSW test (that has no counterexamples found in the 35 years since it was introduced) plus one or more extra random-base Miller-Rabin tests. I just added the new result for deterministic M-R testing out to ~82 bits. A bit stronger than Mathematica's PrimeQ function.

Primality proofs can be done, but very few people care vs. good probable prime tests. Arguably the chance of failure is smaller than a software or hardware error in the proof code. Not to mention most people want the result for a 2000 digit number in 0.1 seconds instead of 1,5 hours. – DanaJ – 2015-09-12T05:05:59.910

The challenge explicitly asks for non-probabilistic solutions. – Martin Ender – 2015-09-14T10:19:46.657

@MartinBüttner Adjusted text and also asked the challenge to be more specific about what "deterministic" means. Thanks. – DanaJ – 2015-09-14T11:56:35.667

2

Lua, 71 67 bytes

n=io.read"*n"for i=2,n-1 do if n%i==0 then r=1 end end print(not r)

Ungolfed:

n=io.read"*n"
for i=2,n-1 do
    if n%i==0 then 
        r=1 
    end 
end 
print(not r)

This program just uses trial division to find if a number is prime or not.

3 bytes saved thanks to @Ruth Franklin

Nikolai97

Posted 2015-09-11T14:06:29.040

Reputation: 653

You can save a couple of bytes by using r=1 instead of r=true (as not 1 is false anyway) – Ruth Franklin – 2015-09-13T13:24:13.273

Ah okay, thanks for the tip :) – Nikolai97 – 2015-09-13T17:15:32.740

Inputs less than 2 (1, 0, and negative numbers) all return true. – ECS – 2016-01-25T08:33:25.977

2

Python 2, 46 65 52 51 bytes

  1. Went up to 65 bytes - Fix made thanks to feersum & Mauris
  2. Went down to 52 bytes - Suggestions made by kirbyfan64sos and accepts in input from STDIN to complete the code.
  3. Went down to 51 bytes - Suggestion made by kirbyfan64sos (thanks again!) to remove the space between 1 and and. Apparently if you have a letter that follows a number, a space isn't needed... how weird, but cool!

This finds the remainder / modulus of the input integer n divided by every number from 2 up to n-1. If there is at least one number in this sequence that has no remainder, or results in 0, this means that the number is not prime. If every value in this sequence is non-zero, the value is prime. Also by definition, 1 isn't prime and so that has to be taken care of separately.

n=input();print n!=1and all(n%i for i in range(2,n))

Example Runs

I ran this in IPython:

In [10]: n=input();print n!=1and all(n%i for i in range(2,n))
1
False

In [11]: n=input();print n!=1and all(n%i for i in range(2,n))
2
True

In [12]: n=input();print n!=1and all(n%i for i in range(2,n))
3
True

In [13]: n=input();print n!=1and all(n%i for i in range(2,n))
4
False

In [14]: n=input();print n!=1and all(n%i for i in range(2,n))
5
True

In [15]: n=input();print n!=1and all(n%i for i in range(2,n))
6
False

In [16]: n=input();print n!=1and all(n%i for i in range(2,n))
7
True

In [17]: n=input();print n!=1and all(n%i for i in range(2,n))
10
False

In [18]: n=input();print n!=1and all(n%i for i in range(2,n))
15
False

In [19]: n=input();print n!=1and all(n%i for i in range(2,n))
17
True

In [20]: n=input();print n!=1and all(n%i for i in range(2,n))
20
False

In [21]: n=input();print n!=1and all(n%i for i in range(2,n))
30
False

rayryeng - Reinstate Monica

Posted 2015-09-11T14:06:29.040

Reputation: 1 521

It has to return false for 1. – feersum – 2015-09-12T00:40:29.673

You made it return False for every possible input but 1, which should be False :) – Lynn – 2015-09-12T01:05:16.363

@Mauris - Oops. I didn't fix that properly lol. Was on mobile. I'm on my computer now. Fixing. – rayryeng - Reinstate Monica – 2015-09-12T01:07:03.033

@Mauris - Fixed now. Thanks. – rayryeng - Reinstate Monica – 2015-09-12T01:12:12.443

@feersum - Now officially fixed. Thanks. – rayryeng - Reinstate Monica – 2015-09-12T01:12:21.573

I think you can reduce this to def f(n):print(n==1 and all(n%i for i in range(2,n))) (but I haven't tested it). – kirbyfan64sos – 2015-09-12T01:24:12.393

@kirbyfan64sos - Let me experiment. Thanks for the tips. – rayryeng - Reinstate Monica – 2015-09-12T01:24:33.867

@kirbyfan64sos - Nope. Same problem as before. Returns True for n==1 and False otherwise. – rayryeng - Reinstate Monica – 2015-09-12T01:25:19.570

Try def f(n):print n!=1 and all(n%i for i in range(2,n)). It needs to be a complete program, though. – kirbyfan64sos – 2015-09-12T01:27:17.510

@kirbyfan64sos - OK, made it a full program. Went up to 52 bytes. Thanks for the tips! – rayryeng - Reinstate Monica – 2015-09-12T01:46:01.447

@rayryeng One more change: you can shave 2 or 3 bytes by dropping the parentheses for the print call and removing the space before the or thanks to Python's weird parser: n=input();print n==1or all(n%i for in range(2,n)). – kirbyfan64sos – 2015-09-12T02:35:32.217

@kirbyfan64sos - really? Wow I never knew that about the or. Thanks for the tips again! BTW, I went with your second attempt. The first attempt that I did to change the and to or is wrong. – rayryeng - Reinstate Monica – 2015-09-12T02:56:22.120

@rayryeng Yeah, I didn't know about it either until around a month ago! Apparently, any time a keyword immediately follows a number, you can leave out the space. – kirbyfan64sos – 2015-09-12T02:57:18.367

@kirbyfan64sos - Wicked. Thanks so much! BTW, I +1ed your O attempt. Nice and sweet! – rayryeng - Reinstate Monica – 2015-09-12T02:58:15.533

1Wow, this is almost the exact same code I wrote :) You can save a char by using > instead of !=. – J Atkin – 2015-09-21T15:14:55.053

2

Fortran 90, 208 bytes

program a
integer::n,i
logical::p
read(*,*)n
if(n==2) then
p=.true.
else if(n<2 .or. mod(n,2)<1) then
p=.false.
else
p=.true.
do i=3,n-1,2
if(mod(n,i)<1) then
p=.false.
exit
endif
enddo
endif
write(*,*)p
end

This uses trial division. Not a whole lot has been golfed here beyond removing spaces. Here it is with spaces for readability:

program primes
    integer :: n, i
    logical :: p

    ! Read n from STDIN
    read (*,*) n

    if (n == 2) then
        p = .true.
    else if (n < 2 .or. mod(n, 2) == 0) then
        p = .false.
    else
        p = .true.
        do i = 3, n-1, 2
            if (mod(n, i) == 0) then
                p = .false.
                exit
            endif
        enddo
    endif

    ! Write a logical to STDOUT
    write (*,*) p
end program

Alex A.

Posted 2015-09-11T14:06:29.040

Reputation: 23 761

Surely need a space before then? – Erik the Outgolfer – 2016-07-17T15:49:56.087

@EʀɪᴋᴛʜᴇGᴏʟғᴇʀ Unfortunately yes. – Alex A. – 2016-07-17T16:13:34.487

2

Bash+coreutils, 29 bytes

echo $[`factor $1|wc -w`==2]

Test:

echo 'echo $[`factor $1|wc -w`==2]' > primetest.sh
chmod +x primetest.sh
./primetest.sh <NUMBER_TO_TEST>

pawel.boczarski

Posted 2015-09-11T14:06:29.040

Reputation: 1 243

2

APL, 13 bytes

2=0+.=X|⍨⍳X←⎕

Inefficient, but it works. A number is a prime if it is only evenly divisible by 1 and itself, so it just tests all of these. Output is 0 or 1.

Explanation:

          X←⎕  ⍝ read a number, store it in X
      X|⍨⍳      ⍝ X mod [1..X]
  0+.=          ⍝ count the 0s
2=              ⍝ are there 2? 

marinus

Posted 2015-09-11T14:06:29.040

Reputation: 30 224

2

Pharo, 7 bytes

isPrime

Pharo is an open source flavour of Smalltalk, a fork of Squeak. All integer instances understand the message isPrime and will respond with a boolean value. To quickly test this, simply select the text containing the number and the message (e.g. 1 isPrime) and "print-it" (or "inspect-it") from the context menu or via keyboard shortcut.

enter image description here

For anyone interested, here's the implementation of that method on the class Integer:

isPrime
    "Answer true if the receiver is a prime number. See isProbablyPrime for a probabilistic
    implementation that is much faster for large integers, and that is correct to an extremely
    high statistical level of confidence (effectively deterministic)."

    self <= 1 ifTrue: [ ^false ].
    self even ifTrue: [ ^self = 2].
    3 to: self sqrtFloor by: 2 do: [ :each |
        self \\ each = 0 ifTrue: [ ^false ] ].
    ^true

So the receiver of this message first covers the base cases (numbers <= 1 aren't prime, even numbers are only prime if they're 2), then checks each odd number from 3 to the floor of its squareroot to see whether dividing itself by each of those numbers results in a zero remainder. If such a divisor is found, false is returned, otherwise true. As the method comment states, there is also a faster (but probabilistic) implementation.

And just for fun, because Smalltalk is so simple with collections, here's how to get a collection of prime numbers up to 100:

(1 to: 100) select: [:each | each isPrime]

The output would be:

#(2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97)

Amos M. Carpenter

Posted 2015-09-11T14:06:29.040

Reputation: 431

I'm pretty sure (but unable to confirm) that the same method exists in Squeak, and probably several other flavours of Smalltalk. If so, please feel free to update this answer. – Amos M. Carpenter – 2015-09-15T07:57:05.297

2

Clojure, 124

Full program, reading standard input and writing to standard output as required by the terms.

(let[n(read-string(first *command-line-args*))](print(and(> n 1)(=(reduce #(* %1(if(=(mod n %2)0)0 1))1(range 2 n))1))))

Obviously inefficient to test all the way up to n but trying to be clever with (range 2 (inc (int (Math/sqrt n)))) adds length.

Ungolfed version:

(let
    [n (read-string (first *command-line-args*))]
  (print
   (and
    (> n 1)
    (=
     (reduce #(* %1
                 (if (= (mod n %2) 0)
                   0
                   1)
                 )
             1
             (range 2 n)
             )
     1
     )
    )
   )
  )

Put in file prime.clj, execute as:

java -cp location-of-clojure.jar-in-your-system clojure.main prime.clj 1

E.g. in my system I get:

$ java -cp ../../repo-wide-libs/clojure.jar clojure.main prime.clj  1
false
$ java -cp ../../repo-wide-libs/clojure.jar clojure.main prime.clj  7
true
$ java -cp ../../repo-wide-libs/clojure.jar clojure.main prime.clj  8
false

Marcus Junius Brutus

Posted 2015-09-11T14:06:29.040

Reputation: 121

Is there a way to remove any of those spaces? Since this is Lisp-like, you could probably take out the ones before parentheses. – jqblz – 2015-09-28T13:37:39.130

yeah so this overflows easily, just uploaded version that doesn't overflow (3 chars longer) – Marcus Junius Brutus – 2017-01-06T19:26:12.313

2

Perl 6, 16 bytes

Using built-ins is allowed (even if makes for a boring answer), and this is about shortest solutions, so...

say is-prime get

get obtains a line from STDIN, is-prime when used on string converts input to integer, say outputs the boolean.

Konrad Borowski

Posted 2015-09-11T14:06:29.040

Reputation: 11 185

2

Common Lisp, 111 88 bytes

(defun p(n c)(cond((= n 1)0)((= n c)1)((eq(rem n c)0)0)(t(p n(1+ c)))))(print(p(read)2))

Try it online!

I'm kinda new to Lisp, so this could probably be improved. It's a very straightforward algorithm.

Ungolfed and commented:

(defun primep (n c)                ; define a function p with args n and c
    (cond                          ; conditional statement similar to switch-case
        ((= 1 n) 0)                ; if n is 1, return 0. Difficult to handle this.
        ((= n c) 1)                ; if c = n, then we have gotten all the way through
                                   ; without finding a single divisor. It's prime; return 1.
        ((eq (rem n c) 0) 0)       ; if n is evenly divisible by c, return 0
        (t (primep n (1+ c)))      ; if all else fails, increment c and recurse
    )
)
(print (primep (read) 2))          ; eval one line from stdin, call primep with an initial
                                   ; count of 2, and print the result

jqblz

Posted 2015-09-11T14:06:29.040

Reputation: 2 062

2

jq, 38 bytes

. as$n|[range(2;.)]|all($n%.>0)and$n>1

Sample run:

bash-4.3$ jq '. as$n|[range(2;.)]|all($n%.>0)and$n>1' <<< 2015
false

On-line test:

manatwork

Posted 2015-09-11T14:06:29.040

Reputation: 17 865

2

C,59 bytes

main(i,j){scanf("%d",&i);for(j=2;i%j++;);printf("%d",j>i);}

It outputs 1 if the input is a prime and 0 otherwise.

user12707

Posted 2015-09-11T14:06:29.040

Reputation: 41

+1 This very nearly fails for an input of 1, where the for() loop will repeat almost forever. But eventually j will overflow and become negative, and then count back up again towards zero. When j==-1, i%j==0, so the loop exits and the program outputs 0 because 1>-1. This is fortunate because the next iteration would raise a divide-by-zero error. – r3mainer – 2015-10-13T19:04:17.537

2

Minkolang, 29 5 bytes

I have since added a built-in for primality testing (it uses a parallelized version of the Sieve of Sundaram).

n2MN.

Explanation

n     Take integer from input
2M    Push 1 if prime, 0 otherwise
N.    Output as integer and stop.

Old version (does not use a built-in):

This was surprisingly long. I'll probably implement a built-in for this (since primality checking will probably be useful elsewhere).

nd2`4&1-N.d2-[0ci2+%3&0N.]1N.

Explanation

n                                Take integer as input.
 d2`4&1-N.                       Output 0 if 1, 1 if 2.
          d2-[           ]       Trial division loop.
              0ci2+%             Check to see if it's divisible by <loop counter>.
                    3&0N.        If so, output 0 and stop.
                          1N.    It's prime! Output 1 and stop.

El'endia Starman

Posted 2015-09-11T14:06:29.040

Reputation: 14 504

2

TeaScript, 5 bytes

$P(x)

Returns true if input is a prime and false of it is a composite number.

Try it online here Does NOT work in Chrome. Input is given in the first field.

user41805

Posted 2015-09-11T14:06:29.040

Reputation: 16 320

2

Carrot (version ^3), 3 bytes

#^P

Basically takes the input (#) and checks if it is a prime number (P) or not.

Note that this program takes the input as a string and,without converting it into a integer, it checks if the number is a prime or not.

Try it online here. Although my programming language has been created after the challenge, it was not created to "abuse" this challenge.

user41805

Posted 2015-09-11T14:06:29.040

Reputation: 16 320

This is scary good. +1 – Addison Crump – 2015-11-04T20:29:58.993

2

Stack, 132 bytes:

'' '' input num `n set 2 `i set { n i % 0 = n 1 = or { 0 print } { i n 2 - gt { 1 print } { i 1 + `i set p } ifelse } ifelse } `p def p

Your basic test every number from 2 to n primality test.

BookOwl

Posted 2015-09-11T14:06:29.040

Reputation: 291

Unnecessary whitespace is unnecessary? '' input num `n set 2 `i set { n i % 0 = n 1 = or { 0 print } { i n 2 - gt { 1 print } { i 1 + `i set p } ifelse } ifelse } `p def p saves 10 bytes – Florrie – 2015-11-06T17:00:16.640

@towerofnix thanks – BookOwl – 2015-11-07T15:49:57.630

2

pl, 1 byte

Try it online.

At the moment, pl uses a lazy trial division builtin. I'll replace it with something that doesn't suck later.

a spaghetto

Posted 2015-09-11T14:06:29.040

Reputation: 10 647

Is that really one byte? I count three but I'm not sure what the rules are. – histocrat – 2015-12-21T16:56:47.810

pl uses CP437 as its encoding. This char is one byte (0xF0) in it. – a spaghetto – 2015-12-21T16:57:57.207

And now I know a thing. Thank you! – histocrat – 2015-12-21T17:08:37.840

2

ROOP, 17 bytes

I
w#H

 P
  w
  O

The w operator reads a number from the keyboard because it has an input object above (I). The input object moves to the right and the number created falls down. The P operator checks whether the number is prime and places a 1 or a 0 on the right (eliminating the number). Then the input object is moved to the right, the number created can not move anywhere. The operator H is activated because it has a object above and terminates the program at the end of all operators. The operator w, puts the number 1 or 0 in the display.

DarkPhantom

Posted 2015-09-11T14:06:29.040

Reputation: 191

2

F#, 99 88 bytes

[<EntryPoint>]
let f a= 
 let i=int a.[0]
 Seq.forall((%)i>>(<)0)[2..i-1]|>printfn"%b";0

Explanation of the interesting part:

                      [2..i-1]              // Generate a list of possible divisors (from 2 to i-1) - for 2, this is an empty list.
Seq.forall(          )                      // Check if none of them are actual divisors, that is
           (%)i>>(<)0                           // That i % it is greater than 0. This is equivalent to (fun d -> i % d > 0)
                              |>printfn"%b" // And print the answer as a boolean

Update: Turns out, the entry point doesn't have to be called "main", and the argument array doesn't have to be called "argv"! =)

Roujo

Posted 2015-09-11T14:06:29.040

Reputation: 353

2

Oracle SQL 11.2, 73 bytes

SELECT SUM(DECODE(MOD(:1,LEVEL),0,1,0))-2 FROM DUAL CONNECT BY LEVEL<=:1;

Return 0 if prime, any other value is false

Jeto

Posted 2015-09-11T14:06:29.040

Reputation: 1 601

A slightly smaller version SELECT MIN(MOD(:1,LEVEL+1))FROM DUAL CONNECT BY LEVEL<:1-1; that will return 0 or 1 – MickyT – 2016-04-10T21:37:50.453

2

Jolf, 2 bytes

Try it here!

m{

Simple. m{ is the isPrime function of the math module, and j is the (implicit) user input as a number.

Conor O'Brien

Posted 2015-09-11T14:06:29.040

Reputation: 36 228

2

Reng v.1, 36 39 bytes

This tests if the input is a prime. (+3 bytes because I forgot to check if input = 1)

i:11#x   eqv0n~
x1+#x:x,?v$\
    ~nex$/

All because I forgot to implement member switching. I am rather proud of this, however, because it uses an interesting feature of Reng: you can redefine any character's meaning. In this case, we see constant redefinition.

i takes input, and 1 pushes 1. We also define x as 1 (1#x) and print zero and terminate if the input is 1 (eqv0n~). Then, the pointer is directed to the next line, x1+#x:x,?v$\. (The last character is a NOP for this instance.)

For the first iteration, 1+ increments x, yielding 2. This is where our trial division starts. #x defines x to be the top element of the stack, in this case, 2. : duplicates the TOS, the input element, and x puts down its value, 2 in this case. , is the modulus operator, and yields i % x. If this is zero, we are directed downwards by v. Otherwise, we drop the modulus and redo the line again.

When we are directed downward by v, we meet / which executes the line ~nex$, equivalent in a left-to-right form to $xen~. $ drops a value, x lays down x, and e checks for equality. If they are equal (in the case that x is the input), this is 1. Otherwise, this is 0.

Test cases

The programs pictured are still valid, but fail to handle 1 correctly, unlike the program above.

Is 5 a prime?

Yes!

Yes.

Is 169 a prime?

No!

No.

Conor O'Brien

Posted 2015-09-11T14:06:29.040

Reputation: 36 228

2

VHDL, 236 bytes

entity e is
port(n:natural;b:out bit);end;architecture a of e is
function p(n:natural)return bit is
begin
if n=1 then
return'0';end if;for i in 2 to n-1 loop
if n mod i=0 then
return'0';end if;end loop;return'1';end;begin
b<=p(n);end a;

The input n is an input port of the entity e; a natural number (starts at 0 for VHDL). b is an output port of entity e; a bit (meaning '1' or '0'). This works by the ever-famous method of trial division, with a special case for 1. This is what it looks like formatted nicely:

entity e is
    port(   n : in natural;
            b : out bit);
end;

architecture a of e is
    function p(n:natural) return bit is
    begin
      if n=1 then
          return '0';
      end if;
      for i in 2 to n-1 loop
          if n mod i=0 then
              return '0';
          end if;
      end loop;
      return'1';
    end;
begin
    b<=p(n);
end a;

Here's the testbench I used for verification:

entity m is
end;

architecture a of m is
    signal i_n : natural := 2;
    signal i_b : bit;

    type int_vector is array(natural range<>) of natural;

    constant primes : int_vector := (2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97);

    function is_prime(n : natural) return boolean is
    begin
        for i in primes'range loop
            if n=primes(i) then
                return true;
            end if;
        end loop;
        return false;
    end;
begin
    x:entity work.e port map(n=>i_n, b=>i_b);
    process
    begin
        for i in 1 to primes'right loop
            i_n <= i;
            wait for 10 ns;
            if is_prime(i) then
                assert i_b='1';
            else
                assert i_b='0';
            end if;
        end loop;
        report "finished" severity error; -- error to stop simulation
    end process;
end a;

This is the result.

Justin

Posted 2015-09-11T14:06:29.040

Reputation: 19 757

1@ais523 except the question specifies that it must be a full program – Justin – 2017-05-11T16:30:31.653

2

Labyrinth, 21 bytes

?
:
}()"{
{: *}{:*{%!

Terminates with an error, with the error message going to STDERR.

Try it online!

Explanation

This uses the same approach based on Wilson's theorem as Sp3000's answer, but I managed to decompose his 3x3 loop into two 2x2 loops which is generally better for golfing Labyrinth.

Labyrinth primer:

  • Labyrinth has two stacks of arbitrary-precision integers, main and aux(iliary), which are initially filled with an (implicit) infinite amount of zeros.
  • The source code resembles a maze, where the instruction pointer (IP) follows corridors when it can (even around corners). The code starts at the first valid character in reading order, i.e. in the top left corner in this case. When the IP comes to any form of junction (i.e. several adjacent cells in addition to the one it came from), it will pick a direction based on the top of the main stack. The basic rules are: turn left when negative, keep going ahead when zero, turn right when positive. And when one of these is not possible because there's a wall, then the IP will take the opposite direction. The IP also turns around when hitting dead ends.

Now we can look at the code. The program starts with a short linear (vertical) bit:

?   Read input N as an integer and push onto main.
:   Duplicate N.
}   Move one copy over to aux.

The IP is now at a junction, and in fact the small 2x2 block acts as a loop which is traversed in clockwise order:

(   Decrement N. If it hits zero, we exit the loop.
:   Duplicate N.
{}  Move a value over from aux and push it back. Together this does nothing.

So this loop leaves all the numbers from N-1 down to 0 on the main stack. Now there's the single ), which increments that 0 back up to 1, such that all the non-zero numbers on top of the stack will multiply together to (N-1)! (including the special case of 0! == 1).

The next 2x2 block is a loop which performs this multiplication and its traversed in counter-clockwise order. The tricky part is that we need to check the value below the product to see if we're done, otherwise we'd end up losing the product by multiplying it with an implicit 0 below.

"   No-op, does nothing.
*   Multiply the top two values on main.
}   Move the product over to aux, exposing the value below. If that's zero,
    we exit the loop.
{   Pull the product back onto main for the next iteration.

Now the main stack is empty and the aux stack contains (N-1)! on top of N. Time to wrap things up:

{   Pull (N-1)! over from aux.
:*  Duplicate and multiply, squaring the factorial.
{   Pull N over from aux.
%   Take modulo, computing (N-1)!^2 % N.
!   Output the result.

Now the IP hits a dead end and has to turn around. The next thing it tries to execute is % but that would now attempt to compute 0 % 0 which terminates with a division-by-zero error.

Martin Ender

Posted 2015-09-11T14:06:29.040

Reputation: 184 808

2

Fuzzy Octo Guacamole, 21 bytes

^-!.1C[d2ss.p*.+]s.Zm

Outputs 0 for false, 1 for true. Thanks to @xnor for the algorithm.

Explanation:

^-!.1C[d2ss.p*.+]s.Zm
^                      # Get Input
 -                     # Decrement
  !                    # Set for-loop counter
   .                   # Swap stacks
    1C                 # Set both stacks to [1].  (push 1 and copy to other stack)
      [         ]      # For loop for duration of input - 1
       d2              # Duplicate the top of stack and push 2 after
         ss            # Put them on the other stack, in reverse order.
           .           # Switches stacks
            p          # Power function. x^y (ToS^2)
             *         # Multiply
              .+       # Move back to the other stack and increment
                ]      # (repeat)
                 s.    # Move the ToS over and go with it
                   Z   # Reverse the stack.
                    m  # Modulus, x%y.
                       # (implicit output)

We start with each stack having 1. Let the bottom of one stack be n, the other bottom be P. We multiply P by n^2, and then increment n. Do this n-1 times. Then take P%n.

Rɪᴋᴇʀ

Posted 2015-09-11T14:06:29.040

Reputation: 7 410

2

WistfulC, 381 bytes

if only int n were 0...
wish for "%d", &n upon a star
if n < 2 were true...
    wish "not prime" upon a star
    if wishes were horses...
*sigh*
if only int i were 2...
someday i will be n...
    if n % i were 0...
        wish "not prime" upon a star
        if wishes were horses...
    *sigh*
    if only i were i + 1...
*sigh*
wish "prime" upon a star
if wishes were horses...

Outputs prime if prime, not prime if not prime.

I went for entertainment value more than small size. Here's a golfed, (arguably) less funny version (286 bytes):

if only int n were 0...wish for "%d",&n upon a star
if n<2...wish "0" upon a star
if wishes were horses...*sigh*if only int i were 2...someday i==n...if !(n%i)...wish "0" upon a star
if wishes were horses...*sigh*if only i were i+1...*sigh*wish "1" upon a star
if wishes were horses...

jqblz

Posted 2015-09-11T14:06:29.040

Reputation: 2 062

You can save some bytes by return truthy/falsy values? – Rɪᴋᴇʀ – 2016-07-21T17:55:27.660

2

APL (NARS2000), 6 bytes

Function: 0∘π

Program: 0π⎕

Adám

Posted 2015-09-11T14:06:29.040

Reputation: 37 779

2

Sesos, 40 39 bytes

0000000: 16f0be afcf9c 37fcfe 8c19d7 c671d7 668ee3 f57b33  ......7......q.f...{3
0000015: 877bc6 662edb b961ba 8763bc 666e3c 66ec01         .{.f...a..c.fn<f..

Try it online! Check Debug to see the generated binary code.

Background

To identify primes, we use a corollary of Wilson's theorem:

corollary of Wilson's theorem

How it works (WIP)

The binary file above has been generated by assembling the following SASM code.

set numin
set numout

get
jmp
    jmp, fwd 1, add 1, fwd 1, add 1, fwd 1, add 1, rwd 3, sub 1, jnz
    fwd 1, sub 1, fwd 2
    jmp, rwd 3, add 1, fwd 3, sub 1, jnz
    rwd 1, sub 1
jnz
rwd 1, add 1, rwd 2
jmp
    fwd 1
    jmp, fwd 2, add 1, rwd 2, sub 1, jnz
    fwd 1
    jmp
        fwd 1
        jmp, fwd 1, add 1, rwd 3, add 1, fwd 2, sub 1, jnz
        fwd 1
        jmp, rwd 1, add 1, fwd 1, sub 1, jnz
        rwd 2
        sub 1
    jnz
    fwd 1
    get
    rwd 4
jnz
fwd 2
jmp
    rwd 1, sub 1, rwd 1, add 1, fwd 1
    jmp, rwd 1, jnz
    rwd 1
    jmp, fwd 1, add 1, rwd 1, sub 1, jnz
    fwd 2
    jmp, fwd 1, jnz
    rwd 1, sub 1
jnz
rwd 2
put

Dennis

Posted 2015-09-11T14:06:29.040

Reputation: 196 637

2

CASIO-BASIC, 28 bytes

?->A:For 2->B To A:A Rmdr B=0=>B->A:Next

This prompts for a number, then outputs the number if it is prime, else zero.

I'm not really sure how this works. I originally thought it would output the value from the last assignment (stored in Ans), but then it would output the lowest factor of the number, not zero.

Note: -> and => are ASCII representations of one symbol each (the assign-to and conditional operators).

The size was calculated as the size of this program (60 bytes) minus the size of an empty program (32 bytes).

user253751

Posted 2015-09-11T14:06:29.040

Reputation: 818

2

PHP, 51 bytes

An alternative PHP answer, but required the GMP extension to be installed:

<?=gmp_strval(gmp_nextprime($argv[1]-1))==$argv[1];

Simply subtracts 1 from the input and compares the nextprime result against the input

gabe3886

Posted 2015-09-11T14:06:29.040

Reputation: 221

gmp_cmp can save 3 bytes. But unfortunately, that algorithm is only probably correct, not always. +1 anyway for showing it off. :) – Titus – 2016-10-16T14:14:32.400

2

Dyalog APL, 10 9 bytes

2=∘≢∘∪⍳∨⊢

I don't think this one has been posted.

Zacharý

Posted 2015-09-11T14:06:29.040

Reputation: 5 710

1

memes, 2 bytes

I decided to hop on the language train. Code:

}"

Explanation:

} //Takes next input.
" //Returns `True` or `False`, representing if the input is prime.

devRicher

Posted 2015-09-11T14:06:29.040

Reputation: 1 609

1

QBIC, 21 20 3 bytes (nc)

Since the purpose of the challenge is to build a catalog, I feel it's important to keep the QBIC post up-to-date with the current state of the language:

­?µ:

Explanation:

?    PRINT
 µ   QBIC's prime test, returns -1 for primes, 0 otherwise
  :  Read a number from the command line, insert that here
     The function is closed automatically because of EOF.

Previously, at 20 bytes:

:[a|~a%b|\p=p+1}?p<3

Simple trial divider. Every time b (the loop counter) cleanly divides a (our prime-candidate), p is increased. Primes will end with p at 2 (or p=1 for a=1). This then prints -1 for primes, and 0 for non-primes, which are QBasic default values for true and false resp.


Original entry, 21 bytes. This prints 1 for primes, and 0 for others. This feels more 'natural' to me than QBasic's default -1/0. Also, this only does slightly less than a/2 divisions for primes (and quits when it detects a non-prime), instead of doing a divisions regardlessly.

:[2,a/2|~a%b=0|_Xp}?q

Explanation:

:       Get the input number, 'a'
[2,a/2| FOR(b=2, b<=(a/2), b++)
~a%b=0  IF a MOD b == 0     --> Clean division == no prime
|_Xp    THEN exit program, printing 'p' (which never gets set and is 0 by default)
}       Close all language constructs: IF/END IF, FOR/NEXT
?q      We've made it through the FOR loop without division, N is prime.
        In QBIC, 'q' is auto-initialised to 1, '?' prints it.

steenbergh

Posted 2015-09-11T14:06:29.040

Reputation: 7 772

1

Maverick, 25 bytes

(1:(<>@-1)//$*)%<>@=<>@-1

Try it online!

This is a pretty fun language IMO. Infix and esoteric.

(1:(<>@-1)//$*)%<>@=<>@-1
 1:                         range from 1 to
   (<>@                     first command line arg (<> called with no args)
       -1)                  minus 1
          //$*              folded over multiplication
(             )%            modulus
                <>@         the input
                   =        does the above equal
                    <>@-1   the input minus 1? If so, yields prime

Conor O'Brien

Posted 2015-09-11T14:06:29.040

Reputation: 36 228

1

SmileBASIC, Forty Four 43 bytes

INPUT N
FOR D=1TO N
P=P+!(N MOD D)NEXT?P==2

Just checks if the number has exactly 2 divisors.

12Me21

Posted 2015-09-11T14:06:29.040

Reputation: 6 110

You could test if P<3 it's one byte shorter and it covers the 1 cornercase. – steenbergh – 2017-02-19T09:01:16.293

1

Valyrio, 14 bytes

s∫main [ipo]

Outputs 1 if the input is prime. Otherwise outputs 0.

Explanation

s∫ tells the interpreter to enter stack mode

main [ starts the main code block

i takes an input and evaluates it

p pushes 1 or 0 to the stack depending on primality

o outputs the top item on the stack

] ends the main code block

user63571

Posted 2015-09-11T14:06:29.040

Reputation:

1

√ å ı ¥ ® Ï Ø ¿ , 3 bytes

Ipo

p is the primality test.

caird coinheringaahing

Posted 2015-09-11T14:06:29.040

Reputation: 13 702

1

NO!, 34 bytes

This was taken from the NO! GitHub page. It isn't mine

NOOOOOOO?NOOOOOOOOOOO
NOOOOOOOO?no

user67719

Posted 2015-09-11T14:06:29.040

Reputation:

This should be marked as community wiki if you didn't write it. – Esolanging Fruit – 2017-05-11T14:09:15.323

1

Ruby 16+15 bytes

require 'prime';p->e{e.prime?}

marmeladze

Posted 2015-09-11T14:06:29.040

Reputation: 227

1

Add++, 14 17 bytes

D,f,@,P
+?
$f,x
O

Try it online!

10 bytes are boilerplate for the full program requirement.

Function, 7 bytes

D,f,@,P

Fairly basic, just defines a function that performs a primality test

Built-in, 1 byte

P

As functions and main code use both different memory models and different commands, this only works in function mode and so would be invalid.

caird coinheringaahing

Posted 2015-09-11T14:06:29.040

Reputation: 13 702

1

cQuents, 3 bytes

?pz

Try it online!

Explanation

?     Mode ? (query)
 p    Builtin: next prime after parameter
  z   Previous item in sequence
   )  Implicit closing parenthesis

Query mode returns true if the input is in the sequence, and false if it is not.

Stephen

Posted 2015-09-11T14:06:29.040

Reputation: 12 293

Note current version uses Z instead of z – Stephen – 2019-02-01T04:53:06.917

1

Taxi, 1519 1309 bytes

Go to Post Office:w 1 l 1 r 1 l.Pickup a passenger going to The Babelfishery.Go to The Babelfishery:s 1 l 1 r.Pickup a passenger going to Cyclone.Go to Cyclone:n 1 l 1 l 2 r.Pickup a passenger going to Cyclone.Pickup a passenger going to The Underground.Go to Zoom Zoom:n.[a]Go to Cyclone:w.Pickup a passenger going to Sunny Skies Park.Pickup a passenger going to Divide and Conquer.Go to Sunny Skies Park:n 1 r.Go to The Underground:s 1 l 1 r 2 l.Switch to plan "1" if no one is waiting.Pickup a passenger going to Cyclone.Go to Cyclone:n 3 l 2 l.Pickup a passenger going to Divide and Conquer.Pickup a passenger going to The Underground.Go to Divide and Conquer:n 2 r 2 r 1 r.Pickup a passenger going to Cyclone.Go to Cyclone:e 1 l 1 l 2 l.Pickup a passenger going to Trunkers.Pickup a passenger going to Equal's Corner.Go to Trunkers:s 1 l.Pickup a passenger going to Equal's Corner.Go to Equal's Corner:w 1 l.Switch to plan "b" if no one is waiting.Go to The Underground:n 3 r 1 r 2 l.Switch to plan "z" if no one is waiting.[1]'0' is waiting at Writer's Depot.[z]'1' is waiting at Writer's Depot.Go to Writer's Depot:n 3 l 2 l.Pickup a passenger going to Post Office.Go to Post Office:n 1 r 2 r 1 l.[b]Go to Sunny Skies Park:n.Pickup a passenger going to Cyclone.Go to Zoom Zoom:n 1 r.Switch to plan "a".

Try it online!

Un-golfed with comments:

[ Test for Primality ]
[ Inspired by: https://codegolf.stackexchange.com/q/57617 ]

[ Pickup stdin and triplicate it]
Go to Post Office: west 1st left 1st right 1st left.
Pickup a passenger going to The Babelfishery.
Go to The Babelfishery: south 1st left 1st right.
Pickup a passenger going to Cyclone.
Go to Cyclone: north 1st left 1st left 2nd right.
Pickup a passenger going to Cyclone.
Pickup a passenger going to The Underground.
Go to Zoom Zoom: north.

[a]
Go to Cyclone: west.
Pickup a passenger going to Sunny Skies Park.
Pickup a passenger going to Divide and Conquer.

[ Store a copy of the original stdin ]
Go to Sunny Skies Park: north 1st right.

[ Iterate down to the next lowest number ]
[ If the input is 1, the switch will immediately output '0' ]
Go to The Underground: south 1st left 1st right 2nd left.
Switch to plan "1" if no one is waiting.
Pickup a passenger going to Cyclone.
Go to Cyclone: north 3rd left 2nd left.
Pickup a passenger going to Divide and Conquer.
Pickup a passenger going to The Underground.

[ Divide the original by the current iteration and check if it's an integer ]
Go to Divide and Conquer: north 2nd right 2nd right 1st right.
Pickup a passenger going to Cyclone.
Go to Cyclone: east 1st left 1st left 2nd left.
Pickup a passenger going to Trunkers.
Pickup a passenger going to Equal's Corner.
Go to Trunkers: south 1st left.
Pickup a passenger going to Equal's Corner.
Go to Equal's Corner: west 1st left.
Switch to plan "b" if no one is waiting.

[ Someone was waiting so it was an integer result ]
[ This is going to eventually happen when we divide by one ]
[ If the current iteration is 1, we want to return 1 as a truthy result ]
[ It it's anything higher than 1, we want to return 0 as a falsey result ]
Go to The Underground: north 3rd right 1st right 2nd left.
Switch to plan "z" if no one is waiting.

[1]
'0' is waiting at Writer's Depot.
[z]
'1' is waiting at Writer's Depot.
Go to Writer's Depot: north 3rd left 2nd left.
Pickup a passenger going to Post Office.
Go to Post Office: north 1st right 2nd right 1st left.
[ No need to return to taxi garage or even switch to the end of the program ]
[ It will output to stderr because it can't drive in the next direction and terminate the program ]

[b]
[ No one was waiting so it was not an integer result ]
[ Continue on to the next iteration ]
Go to Sunny Skies Park: north.
Pickup a passenger going to Cyclone.
Go to Zoom Zoom: north 1st right.
Switch to plan "a".

The only thing that felt golf-y when writing it was allowing it to error out instead of terminate properly.

Engineer Toast

Posted 2015-09-11T14:06:29.040

Reputation: 5 769

1

TI-BASIC (nspire), 15 bytes

f(n):=isPrime(n

Uses the CAS isPrime() builtin. The environment by default adds an extra parenthesis on the end of the input, making it f(n):=isPrime(n) when the function is defined in the interpreter.

user50198

Posted 2015-09-11T14:06:29.040

Reputation:

I'm assuming nspire doesn't allow unclosed parentheses? – Zacharý – 2017-08-24T00:24:39.417

Yeah, it autocloses them. – None – 2017-08-24T00:36:11.967

I don't think that the TI-Nspire runs TI-BASIC. I was under the impression that it used a variant of Lua... – Scott Milner – 2017-08-24T00:38:12.207

It also uses a form of lua for code in documents, but it does use basic for on-calculator programming (without an interpreter page) – None – 2017-08-24T00:39:11.047

If it autocloses them, then isPrime(n should work. – Zacharý – 2017-08-24T00:44:04.893

Added changes and explanation. – None – 2017-08-24T00:47:34.773

1

Implicit, 30 29 21 bytes

<2è;:(-1>1?{;|_ñè};).

Try it online!

Whew, another 10 bugs uncovered during the writing of this program. This uses trial division.

While SimpleStack uses a ... stack, I'm going to call two stack values variables. The input will be variable i and the trial-division loop counter will be variable m. Note I might've messed up this explanation somewhere along the road of golfing.

<2è;:(-1>1?{;|_ñè};).
                       implicit input i
<2                     push i < 2
  è                    exit without implicit output if truthy
   ;                   pop i < 2
    :                   set m to input
     (.............)    do
      -1                 decrement m
        >1               push m > 1
          ?{.....}       if true
            ;             pop m > 1
             |            duplicate stack (stack: i, m -> i, m, i, m)
              _           push i % m
               ñ          compute logical NOT on i % m (truthy if i % m falsy)
                è         exit without implicit output if truthy
                  ;      pop top of stack (NOT(i % m) or m > 1)
                   )    while m > 0
                    .   increment

If the input is 0 or 1, it will print nothing, so the output will be falsy. If the input is not prime, the second è exits without implicit output (making hte output empty and therefore falsy). If the input is prime, it will reach the end of the program, and the top of stack will always be 0. . increments it, turning it to 1 (truthy). Implicit output.

MD XF

Posted 2015-09-11T14:06:29.040

Reputation: 11 605

1

Pyth, 3 2 bytes

I can't see a simpler way of doing this. My ignorance continues to amaze me.

P_

Explanation (if it needs one):

P   Prime function: factorization if positive, isPrime if negative
 _  Negate the implicit input

Test suite

Stan Strum

Posted 2015-09-11T14:06:29.040

Reputation: 436

No need to subtract from zero, as _ performs negation. – Dennis – 2017-09-06T03:53:55.243

@Dennis I tried that earlier, it didn't seem to work... – Stan Strum – 2017-09-06T03:54:32.243

1

Python 3, 57 bytes

k=int(input());print(all(k%j for j in range(2,k))and k>1)

I think it's pretty self-explanatory.

0WJYxW9FMN

Posted 2015-09-11T14:06:29.040

Reputation: 2 663

@JonathanFrech No. – 0WJYxW9FMN – 2017-09-22T21:15:33.737

@JonathanFrech The author of this problem asked for a full program. – 0WJYxW9FMN – 2017-09-23T17:47:56.850

1

Funky, 39 bytes

n=>{o=1for(i=2;i<n;i++)o=o&0<n%i o&n>1}

Try it online!

ATaco

Posted 2015-09-11T14:06:29.040

Reputation: 7 898

1

Julia, 15 7 bytes

isprime

knocked down 8 bytes thanks to caird coinheringaahing

EricShermanCS

Posted 2015-09-11T14:06:29.040

Reputation: 121

1Can you just submit isprime as a function submission? – caird coinheringaahing – 2017-12-09T18:07:55.767

i suppose so? good point – EricShermanCS – 2017-12-10T04:32:25.627

2This doesn't work in julia 0.6+, (isprime was deprecated in 0.5 and moved to the Primes.jl package). I guess retitle this as Julia 0.5? – Lyndon White – 2017-12-12T06:56:47.097

The challenge spec requires a full program. Also, isprime uses a probabilistic primality test and is thus cannot be used for this challenge.

– Dennis – 2017-12-12T12:56:12.450

1

Common Lisp, 64 bytes

(print(=(loop as x from 1 to(setq n(read))count(=(rem n x)0))2))

Try it online!

57 bytes in Common Lisp REPL:

(=(loop as x from 1 to(setq n(read))count(=(rem n x)0))2)

Renzo

Posted 2015-09-11T14:06:29.040

Reputation: 2 260

1

><>, 46 Bytes

i> :1\/ln;
~\?:-/^?(2l~
%$}:{<;n0/?
l1- ?/1n;\

Takes input as an ascii character, prints 1 for primes, 0 for non-primes, including 1. Loops forever if input is 0 or negative.

How it works:

 > :1\
 \?:-/

Fills the stack with integers between 0 and input, inclusive.

      /ln;
~    /^?(2l~

Removes the top two values on the stack, 0 and 1. Tests if the length of the stack is <2; if it is, the value is either 1 or 2. In either case, its truthy/falsy value is the length of the stack; print it. If the length is greater than 2, it continues to the next loop.

%$}:{<;n0/?
l1- ?/1n;\

Duplicates the value being tested from the bottom of the stack. Test if it's divisible by the value at the top. If it is, print 0. Otherwise, check that the stack length is not 1. If it is, the value being tested is prime; print 1.

Edit:

Added on to this program a bit, as a part in my attempt to conquer as much of project euler as possible in ><>.

This is for Project Euler 7: Calculating the 10001st prime

a:::***31[\>?!/l1-?\]r1-:?!/r:nao1+70.
\/{*::+1<0<\%$}:{  <~~
 \:}(?!\/-1l  \         ;nr\
\!?:-1:<\ ?!\~^.38
            \] 1+70.

Comes in at 130 Bytes, although I didn't try very hard to golf it. Pushes 10000 to the stack (since we start at 3), makes a new stack, runs the primality test on the number, starting at 3. If it's prime, decrement the counter and increase the value being tested. If its not prime, just increment the value.

The go-fish interpreter was a life saver for this--it ran orders of magnitude faster than fish.py.

Bolce Bussiere

Posted 2015-09-11T14:06:29.040

Reputation: 970

1

Whispers v2, 27 bytes

>>> ⊤ℙ
> Input
>> 1∘2

Try it online!

Whispers v1, 27 bytes

> Input
>> 1’
>> Output 2

Try it online!

Uses the builtin prime tester. Alternatively, instead of using a builtin, the following code uses Wilson's Theorem and weighs in at 54 bytes

> Input
> 1
> 2
>> 1-2
>> 4!
>> 5*3
>> 6%1
>> Output 7

Try it online!

caird coinheringaahing

Posted 2015-09-11T14:06:29.040

Reputation: 13 702

1

AnyDice, 68 bytes

function: p A {loop N over {2..A/2}{if A/N*N=A {result:0}}result: 1}

I was wondering if AnyDice counted as a language for PCG.se purposes, and it turns out it does,

This is trial division, making use of the fact that AnyDice only supports integer (truncating) division.

usage:

TryIt

output [p 333332]

outputs 0(100%)

output [p 333331]

outputs 1(100%)

Lyndon White

Posted 2015-09-11T14:06:29.040

Reputation: 1 021

1

SNOBOL4 (CSNOBOL4), 105 bytes

	N =INPUT
	X =1
	GT(N,1)	:F(C)
I	X =X + 1
	OUTPUT =EQ(X,N) 1	:S(END)
C	OUTPUT =EQ(REMDR(N,X)) 0	:F(I)
END

Try it online!

I thought about putting my explanation in all caps, but that would just look silly.

This is the brute force approach: Test if N==1, and output 0 on success, then increment X. If we reach a point where X==N, then we have found a prime number and we output 1 and terminate. If we reach a point where N %% X == 0, then we output 0.

I am still learning to golf in SNOBOL, so I think it should be possible to make this shorter.

Giuseppe

Posted 2015-09-11T14:06:29.040

Reputation: 21 077

1

JavaScript, input unary '1'*n, 33 Bytes

!/^(1+1)\1+$/.test(x=prompt())&x>1

l4m2

Posted 2015-09-11T14:06:29.040

Reputation: 5 985

1

Triangularity, 7 bytes

.).
IEp

Try it online!

Triangularity, 49 bytes

Much more interesting solution, without using built-ins for divisors / prime test. Outputs 1 if the input integer is a prime, 0 otherwise.

....)....
...If)...
..rF@)I..
.f/Df={L.
)2=......

Try it online!

How it works?

Removing the characters that make up for the triangular padding, here's what the program does:

)If)rF@)If/Df={L)2= || Full program. ToS = Top of the Stack
)I                  || Get the 0th input.
  f                 || And cast it to an integer.
   )r               || Create the integer range [0 ... ToS - 1).
     F        {     || Filter the elements of this list which satisfy this function:
      @)If/Df=      || Runs each (X) on a separate stack, keeps those that yield 1.
      @             || Increment X.
       )If/         || And divide the input by it.
           D        || Duplicate, push two copies of the ToS.
            f=      || Floor the second copy and compare it with the first one.
               L    || Length.
                2=  || Check if it equals 2. Implicitly output the result.

Mr. Xcoder

Posted 2015-09-11T14:06:29.040

Reputation: 39 774

1

Forth (gforth), 73 61 57 bytes

: f { n } 0 n 1 > if 1 n 2 ?do n i mod 0> * loop then . ;

Try it online!

Stack-Only variant, 76 68 64 61 bytes

: f 0 over 1 > if 1+ over 2 ?do over i mod 0> * loop then . ;

Try it online!

Explanation

0 over             \ place a 0 on the stack then place a copy of the input on top
1 >                \ check if the top of the stack is greater than 1
if                 \ if true execute the if, otherwise skip it
   1+              \ add 1 to our tracking variable (so replace 0 with 1) 
   over 2          \ place n and 2 on the top of the stack, 
   ?do             \ loop from 2 to n, skip loop if n = 2
      over i mod   \ get the modulo of n and the loop index (so n % i) 
      0>           \ check if modulo is greater than 0 (not divisible)
      *            \ multiply bool (-1 or 0) by the top of the stack (shorter version of and)
   loop            \ end the loop 
then               \ end the if statement
.                  \ output the top of the stack

reffu

Posted 2015-09-11T14:06:29.040

Reputation: 1 361

1

095, 35 bytes

1Xid1.=(DD,,{d_.%(yX]D}dYs]D1.=[1s]

Returns 0 for composite and 1 for prime.

Explanation:

1X                                  ~ Set True to X
  id                                ~ Take input and duplicate
    1.=                             ~ Check if equal to 2
       (DD                          ~ If not, delete last two items
          ,,                        ~ Subtract 2
            {                       ~ For that many times,
             d_.%                   ~ see if iterator divides into input
                 (yX]               ~ If it does, set False to X
                     D}             ~ Delete last item, close loop
                       dYs]         ~ Duplicate input, print X
                           D1.=[1s] ~ If input = 2, say True

nosekill

Posted 2015-09-11T14:06:29.040

Reputation: 81

1

17, 102

17 is a language I made a few days ago and am now trying out some challenges with.

A single ascii character is taken as input and 0 is returned for false, its lowest factor for true.

0{1 # 2 # 1 + : 2 @
% 0 == 2 * 2 # 1 # == 3 * + 0 @ }2{2 # $$ 0 @}5{0 $$ 0 @}777{0 0 @ I 1 @ 1 2 @}

Expanded:

0 {
  1 # 2 # 1 + : 2 @
  % 0 == 2 * 2 # 1 # == 3 * + 0 @
}
2 {
  2 # $$ 0 @
}
5 {
  0 $$ 0 @
}
777 {
  0 0 @ I 1 @ 1 2 @
}

17 starts at block 777 and runs blocks depending on value stored at 0.

Block 777: Set value 0 to 0, set value 1 to ascii code of input char, set value 2 to 1.

Block 0(first run after block 777): Increase 2 by 1, if 1 mod 2 == 0, go to block 2 unless value 1 is value 2, then goto block 5

Block 2: Load 2 and print, then exit

Block 5: Print 0 and exit

Hugh Williams

Posted 2015-09-11T14:06:29.040

Reputation: 51

1

COBOL (GNU), 305 bytes ( +5 for compiler flags)

ID DIVISION.PROGRAM-ID.A.DATA DIVISION.WORKING-STORAGE SECTION. 1 I PIC 9(9). 1 V PIC 9(9). 1 R PIC 9(9). 1 Q PIC 9(9). 1 P PIC 9 VALUE 1.PROCEDURE DIVISION.ACCEPT V PERFORM VARYING I FROM 2 BY 1 UNTIL I=V DIVIDE I INTO V GIVING Q REMAINDER R IF R=0 THEN MOVE 0 TO P END-IF END-PERFORM DISPLAY P STOP RUN.

Compile with -free flag. (This allows ignoring formatting.)

Try it online!

Ungolfed version:

IDENTIFICATION DIVISION.    *> Required in every program header.
PROGRAM-ID. A.

DATA DIVISION.
WORKING-STORAGE SECTION.
    01 I PIC 9(9).          *> Loop index
    01 V PIC 9(9).          *> Value to test
    01 R PIC 9(9).          *> Remainder
    01 Q PIC 9(9).          *> Quotient
    01 P PIC 9 VALUE 1.     *> Is prime?

PROCEDURE DIVISION.
    ACCEPT V                                    *> Get value from input
    PERFORM VARYING I FROM 2 BY 1 UNTIL I>V/2   *> for (i = 2; i <= v/2; i++)
        DIVIDE I INTO V GIVING Q REMAINDER R    *>     Q = V/I; R = remainder(V/I)
        IF R=0 THEN MOVE 0 TO P END-IF          *>     If remainder is zero, not prime
    END-PERFORM
    DISPLAY P               *> Output result (1 or 0)
    STOP RUN.

vasilescur

Posted 2015-09-11T14:06:29.040

Reputation: 341

1

Pyt, 1 byte

Try it online!

Implicit input
ṗ   primality test
Implicit output

FantaC

Posted 2015-09-11T14:06:29.040

Reputation: 1 425

1

Quarterstaff, 94

golfed 1!

10-?[-38a a a a a a a a a a>a10-?]a>b b[>b>d a>c c[1>c1d>d b d(.>e|>d1>e)c]e f>f1b]2-f(.|1)48!

Try it online!

How it works:

Quarterstaff has a register i call "value", and multiple other registers i call variables, which are referenced by name.

value starts as 0

10 add 10 to value

- multiply value by -1 (value now -10) (this is because it checks for the end of the integer by checking for newline, which has a charcode of 10)

? add an inputted characters charcode to the value (which will be 48 for "0", for example)


the first loop:

[ begin while loop. these work like brainf***'s while loops, but instead of a cell, it uses the value.

- multiply value by -1

38 add 38 to value

At this point, the program has 0 if the most recently input character was "0", -1 if it was "1", -2 if it was "2" etc.

a a a a a a a a a a this is a reference to the variable a 10 times. this means we add 10*a to the value. a starts of as zero

>a store this value in a. this means we have just done the following: a=10*a-int(inputted_char). a is always a negative number when using digits. when the loop is done, a will hold the inputted number *-1. this also happens to set the value to 0. assignment with > always sets the value to 0

10-? this is the same code as we executed before entering the loop. this means that we will input a new character, but if we got to the end of the number, we exit the loop.

] loop end. if the value is zero, we exit, otherwise returning to the start. this while loop executes until it has taken all the input into the variable a in negation (22 becomes -22)


a now holds the negative of the inputted number. why is it negative? well, because it's shorter. if we want to decrease the magnitude by 1, we only have to add 1, not use -, add 1, then use - again.

a>b the a adds a to the value (which is now exactly a because it was previously 0), and >b puts this value into b. in effect b=a, because the > sets the value back to 0. remember a and b are negative

b there is a space in between variable names, because otherwise they get parsed as one name. this just makes the value b, because the value was 0 before


another while loop

[ begin the loop

>b this puts the value inside b, and value now = 0. at the first execution of the loop, this has no effect apart from value = 0, but at subsequent executions, it will be adding 1 to b, which will decrease it in magnitude until reaches -1. we use b to represent divisors

>d value will always be 0 when executing this. this will set d to 0. this is necessary because it is set to other values later in the loop

a>c means c=a. remember a is negative, and so is c

c value += c (value =c because value was 0). c is negative,

[ nested while loop. tests for the value of c in first and subsequent executions because the value is set to c just before it checks. This loop performs modulus

1>c because we have c in the value, this means add 1 to c. remember c is never >0. this makes c a counter down to 0

1d>d add 1 to d (value was 0 before these commands). d is a non-negative integer

b d value += b + d. because b is negative and d is positive, this means that the value will now equal 0 iff abs(b)=abs(d).

(.>e|>d1>e) this is an if else expression/command thing. because it immediately follows the b d which is 0 iff the absolute values of b and d are equal, it means it will execute the else iff abs(b)=abs(d), otherwise the then part. so, the part that executes if the value isn't 0 is .>e. . sets the value to zero, >e puts that 0 in e. e is 0 by default. if b and d have equal magnitude though, >d1>e executes. this puts 0 in d (because the value was zero if we're executing this part) and then 1 in e. if this is the last execution of the loop, it means that we will exit the loop with 1 in e, which represents being divisible. otherwise we haven't finished the modulo process yet. if we executed the if not 0 part and this was the last execution, we exit with 0 in e, representing not divisible.

c value = c (because value was 0 before)

] close nested loop.

back to the not modulus part

e f>f value =0 prior to execution, and f += e (f = e + f)

1b this sets the value to b+1 (remember b is negative). this will get stored in b next execution of the loop, if there is one.

] close outer loop


tying up

2-f value (which equals 0) += f-2. prime numbers exited the modulus loop with e=1 precisely 2 times.

(.|1)48 if that isn't 0, set value to 0, but if it is 0, set to 1. then add 48, to the value, which is either 1 or 0.

! print character with the corresponding char code. thus, print "1" (49) for primes and "0" (48) for not primes

Destructible Lemon

Posted 2015-09-11T14:06:29.040

Reputation: 5 908

You can save a byte by moving the >e out of the if expression: Try it online!

– wastl – 2019-04-07T12:04:17.160

1

K, 23 bytes

x=&/(y*/:y:!x)?'x:0$0:`

yay for algorithmic improvements!

Explanation

No operator precedence kind of acts against us here, so we have to resort to paranthesis

x:0$0:`

reads the input and saves the result to the variable x

y:!x takes the list 0..(x-1) and saves it to the variable x

y*/:y applies * using /:, so every element in y is multiplied by y, creating a matrix

?'x searches every column of the matrix for x, returning the length if x is not found

x=&/ &/ takes the minimum of the list by folding it with the min operator, and then checks if it is equal to x. If not, there should exist a combination of two numbers smaller than x result in x, i.e it is not a prime

ulucs

Posted 2015-09-11T14:06:29.040

Reputation: 91

1

tinylisp, 23 bytes

There's a library function.

(load library
(prime? 1

(Since tinylisp is incapable of taking user input, "For scoring purposes, submit the program that corresponds to the input 1.")

Try it online!


Here's a 112-byte solution using only the base language, no library functions:

(d D(q((F A N)(i(l A N)(D F(a F A)N)(e N A
(d _(q((F N)(i(D F 0 N)(e F N)(_(a 1 F)N
((q((N)(i(e N 1)0(_ 2 N))))1

The first line defines a function D that takes a factor F, an accumulator A (initially 0), and a number N; it returns 1 if N is divisible by F, 0 otherwise.

The second line defines a function _ that takes a minimum factor F and a number N; it returns 1 if N is coprime to all numbers from F to N-1, 0 otherwise.

The third line constructs an anonymous function that takes a number N; it returns 0 if N is 1, and otherwise calls _ with number N and minimum factor 2. As above, the scored code calls the anonymous function with an argument of 1.

DLosc

Posted 2015-09-11T14:06:29.040

Reputation: 21 213

1

Zephyr, 88 bytes

input n as Integer
set f to 0
for i from 1to n
if(n mod i)=0
inc f
end if
next
print f=2

Try it online!

Uses the "n must have exactly two perfect divisors in the inclusive range [1, n]" approach. Run a for loop over that range, count the numbers i for which n mod i is zero, and output at the end whether the count equals 2.

DLosc

Posted 2015-09-11T14:06:29.040

Reputation: 21 213

1

Befunge-93, 28 bytes

&:0\1\1>-#1:__\#0:#*_$:*\%.@

Try it online!

Uses Wilson theorem, i.e. that ((n-1)!^2)%n returns 1 if n is prime else 0.

How It Works:

&:0\1\ Get input n and dupe it, putting a 0 and a 1 in-between
      1>-#1:_ Decrement and dupe until the number is empty
             _\#0:#*_$ Multiply until you reach the zero, yielding (n-1)!
                      :* Square it
                        \% Mod this value by the original value
                          .@ Print the mod and terminate 

Jo King

Posted 2015-09-11T14:06:29.040

Reputation: 38 234

1

><>, 23 22 bytes

:1vn%$*:~<
@*>$1-:?!^:

Try it online!

Uses Wilson's theorem again, i.e. (n-1)!**2 %n returns 1 if n is prime, 0 if it is not. Takes input via the -v flag.

How it Works:

:1v Dupe the value and push a 1 as the total
  >$1-  Decrement the copy
      :?!^  If the copy is 0 go up to the first line
@*>       : Else Multiply the total by the copy and repeat the decrement
If we went up to the first line
        ~< Pop the excess 0
      *:   Square the total
  vn%$     Print the total modulo the original value and exit with an error

Jo King

Posted 2015-09-11T14:06:29.040

Reputation: 38 234

1

Forked, 57 54 bytes

$P11>p1+"Us'v
    |       |
1%& :-msU"p-:-
    |
  &%<

The dual-fork loop is really beautiful. Try it online! Alternate versions with identical bytecount:

Short explanation

This uses a trial division method, equivalent to this C code:

int isPrime(int n) {
    for (int i = 2; i < n; i++)
        if (n % i == 0)
            return 0;
    return 1;
}

Long, thorough explanation

Before we begin, let us define two values:

  • n: the inputted number, to be tested for primality
  • i: the loop counter for the trial division method

Here's the program's control flow:

---->-------v
    |       |
--- :-------:-
    |
  --<

The forks : compose one big loop. The rightmost fork exits the loop and prints 1 if n-i is zero, i.e. if n mod i is always positive, meaning n has no divisors other than 1 and itself.

The leftmost fork exits the loop and prints 0 if n mod i is zero, meaning n has a divisor other than 1 and itself.

The initial four bytes set up the stack. Let's say the input n is 11.

CODE  STACK    EXPLANATION
$     [n=11]   read integer input n
 P    []       pop n, stash in register
  1   [i=1]    push i (initially 1)
   1  [i=1, 1] push filler value for later popping

The code p1+"Us' between the redirects > and v pushes n-i.

CODE    STACK        EXPLANATION
p       [i=1]        pop top of stack (useless value or result of n%i)
 1+     [i=2]        add 1 to top of stack
   "    [i=2, 2]     duplicate top of stack
    U   [i=2, 2, 11] copy register onto stack
     s  [i=2, 11, 2] swap top two stack values
      ' [i=2, 9]     subtract top two stack values

Then the IP turns South and forks. If n-i is zero, it turns East and immediately wraps around to hit 1%& (push 1, print, exit), therefore returning a truthy value if If there are still values of i to check, the IP turns West, hitting the code that computes n mod i:

CODE  STACK        EXPLANATION
p     [i=2]        pop n-i
 "    [i=2, 2]     duplicate top of stack
  U   [i=2, 2, 11] copy register to stack
   s  [i=2, 11, 2] swap top two stack values
    m [i=2, 1]     compute n mod i

The IP is forked, turning South and returning 0 if n mod i is 0. Otherwise it turns North and immediately turns East, where i is incremented and the whole loop is done again.

MD XF

Posted 2015-09-11T14:06:29.040

Reputation: 11 605

1

Forth, 82 65 bytes

: f 1 ?do i' i mod 0= + loop 0= ;
1 tib dup 9 accept evaluate f .

Ungolfed:

: f                        \ def f(counter, n):
  1 ?do                    \     for i in range(1, n):
    i' i mod 0= +          \         counter -= n % i == 0  # In Forth True == -1
  loop
  0=                       \     return counter == 0
;
1                          \ counter = 1
tib dup 9 accept evaluate  \ n = int(input()) # Forth version inputs 9 digits at most
f .                        \ print(f(counter, n))

It has at least one downside though: Forth echoes back what it accepted, because it is strongly terminal and/or REPL oriented, so feeding it 7 yields 7 -1

Run it!

Alex

Posted 2015-09-11T14:06:29.040

Reputation: 326

1

Python 3, 42 55 bytes

n=int(input());print(sum(n%m<1 for m in range(1,n))==1)

Try it online!

The n%m<1 comparison is sufficient to check that n is a multiple of m. I start the range at 1 to correctly handle the case n == 1.

Scott Norton

Posted 2015-09-11T14:06:29.040

Reputation: 101

1

Go, 231 bytes

package main
import."fmt"
type c chan int
func main(){a,k:=0,make(c);Scan(&a);go func(d c){for i:=2;;i++{d<-i}}(k)
for{p:=<-k;if a==p||a<p{Print(a==p);break};j:=make(c)
go func(i,o c,p int){for{j:=<-i;if j%p!=0{o<-j}}}(k,j,p);k=j}}

This solution contains a sieve that generates primes, when the primes exceed the candidate, or there is a match it outputs true for primes and false for non-primes.

The main code is blatantly stolen from this playground example.

Since it is rather silly I probably wont be golfing this solution any further

Try it online!

Kristoffer Sall-Storgaard

Posted 2015-09-11T14:06:29.040

Reputation: 489

1

Python 2, 51 bytes

p=input()
print all(p%i for i in range(2,p-1))==1<p

Try it online!

Neil

Posted 2015-09-11T14:06:29.040

Reputation: 2 417

1

Procedural Footnote Language, 41 bytes

[1]
[PFL1.0]
[1] [PRIME:[INPUT]]
[PFLEND]

Explanation

This program is fairly straight-forward: The body of the document (above the [PFL1.0] tag) is the result/output, and is equivalent to the evaluated contents of footnote [1]. Footnote [1] evaluates to the result of the [PRIME] function on input from STDIN.

vasilescur

Posted 2015-09-11T14:06:29.040

Reputation: 341

1

Reality, 1 byte

P

This takes input via stdinput and return true if it is a prime and false other wise


Alternate :

If taking input is not allowed then

p number

space is not required (replace number with your number)


Note : Reality is an under-development language and was created after this challenge

Muhammad Salman

Posted 2015-09-11T14:06:29.040

Reputation: 2 361

1

Racket, 67 42 bytes

(require math/number-theory)(prime?(read))

When run in DrRacket, this will prompt the user for an integer and return #f (false) if the number is not a prime and #t (true) if the number is a prime.

Previous (and much more complicated) version:

((λ(l)(or(= l 1)(ormap(λ(x)(=(modulo l x)0))(range 2 l))))(read))

Tango

Posted 2015-09-11T14:06:29.040

Reputation: 121

1

Z80Golf, 30 bytes

00000000: 040c 0934 0c28 0d0a b720 f960 6909 38f4  ...4.(... .`i.8.
00000010: 2534 18f9 24cd 0380 6f7e cd00 8076       %4..$...o~...v

Try it online!

Takes a single-byte input. Outputs a zero byte for prime byte values, and some non-zero byte for composite values. (This value is actually the number of distinct prime divisors!)

Disassembly:

  inc b
  inc c
  add hl, bc  ; HL = BC = $0101.
              ; We use $0100~$ffff as a prime sieve array.
  inc (hl)    ; Mark 1 as composite.

next:
  inc c       ; Loop for C from 2 to 255.
  jr z, done
  ld a, (bc)
  or a
  jr nz, next ; If *BC is marked as composite, skip.

  ; sieve
  ld h, b     ; Mark *(BC), *(2*BC-256), *(3*BC-512) ... as composite.
              ; (So for BC = $0103, this is $0103 $0106 $0109 ...)
  ld l, c
clear:
  add hl, bc
  jr c, next  ; Run until HL overflows! This is more work than necessary, but shorter.
  dec h
  inc (hl)
  jr clear

done:          ; Now H = 0.
  inc h        ; Now H = 1.
  call $8003
  ld l, a      ; Now HL = $0100 + input.
  ld a, (hl)
  call $8000   ; putchar(*HL)
  halt

Lynn

Posted 2015-09-11T14:06:29.040

Reputation: 55 648

1

Elixir, 122 bytes

o=fn f,p,1->1
f,1,c->0
f,p,c->case rem(p,c)do
0->0
_->f.(f,p,c-1)end end
{i,_}=Integer.parse IO.gets""
IO.puts o.(o,i,i-1)

Try it online!

Can definitely be golfed. Outputs 1 for prime and 0 for composite

Dave

Posted 2015-09-11T14:06:29.040

Reputation: 432

1This breaks on 1. Otherwise, replacing True by 0<1 and False by 1<0 shortens it a bit. – Ørjan Johansen – 2018-06-21T18:03:22.923

1 has been fixed, and I just switched True / False out for the easier int equivalents – Dave – 2018-06-21T18:49:11.173

1

Scala, 40 bytes

def f(n:Int)= !(2 to n-1).exists(n%_==0)

Try it online!

Shankar Shastri

Posted 2015-09-11T14:06:29.040

Reputation: 111

Unfortunately, this challenge doesn't allow functions. – Dennis – 2018-06-25T13:49:25.640

1

Julia 0.6, 44 bytes

n=parse(Int,ARGS[]);show(sum(n%(1:n).<1)==2)

Try it online!

Since the existing answers are invalid for the challenge and/or outdated by newer Julia versions, I'm adding a Julia answer that works in Julia 0.6.

For Julia 0.7 (and to avoid deprecation warnings in 0.6), this needs just one more byte (% becomes .%):

45 bytes

n=parse(Int,ARGS[]);show(sum(n.%(1:n).<1)==2)

Try it online!

sundar - Reinstate Monica

Posted 2015-09-11T14:06:29.040

Reputation: 5 296

1

Foam, 9 bytes

;# #.% .#

Foam isn't on TIO yet, but it should be coming soon!

Outputs 1 for prime and 0 for nonprime.

Foam has a prime checking builtin.

;#   <- read number
#.%  <- is number prime?
.#   <- output number

Esolanging Fruit

Posted 2015-09-11T14:06:29.040

Reputation: 13 542

1

Flobnar, 25 bytes

%\ @
:&|\<
-:1>
1*<
>>  *

Try it online!

Outputs 1 for prime, 0 for composite. Uses the -d flag to get numerical input. Be aware that the time it takes for each number doubles, where an input of 18 will take ~6 seconds, and 19 takes ~12 seconds. This is because the algorithm (based on Wilson's theorum) looks somewhat like:

def f(n):
    if n == 0:
        return 1
    else:
        return n*f(n-1)*f(n-1)
print f(input)*f(input)%input

So basically the function takes 2n+1 steps, doubling the time it takes for each step. If you want to test the code itself, you can use this (+2 bytes) which is merely 2n steps. It basically removes the surplus extra call of f(n-1) in the return of the function.

Jo King

Posted 2015-09-11T14:06:29.040

Reputation: 38 234

1

Elixir, 75 72 bytes

n=String.to_integer IO.gets""
IO.puts Enum.all?2..n-1,&rem(n,&1)>0||2==n

Try it online!

Mr. Xcoder

Posted 2015-09-11T14:06:29.040

Reputation: 39 774

1

Pixiedust, 59 bytes

Takes input in the ++ register, and leaves output in the .. register.

*.+*.*+.
+.
.++*++
+*..
**+..+++*
*+++*+*.*+
+**
+..
.*...*

Explanation

* . +* .*+.       Set the +* register to 2 - this is the factor being checked
+.                Define a label whose name is the empty string
. + +* ++         Test whether +* has reached ++ yet
+* . .            If it has, go to the "." label
* *+ .. ++ +*     Set the branch register to ++ modulo +*
* ++ +* +* .*+    Increment the factor
+* *              If the branch register is not 0 (meaning it's not a factor) restart the loop
+. .              The "." label
. * .. .*         Invert the .. register

Nissa

Posted 2015-09-11T14:06:29.040

Reputation: 3 334

1

Cardinal, 24 21 bytes

Thanks to @Jo King for golfing off 3 bytes

Code:

%
:
=>-!.
v#~M!@
-
R^

Try it online!

Explanation:

%      //Start program
:      //Read input
=      //Copy input to inactive value
v#     //Loop around and pass copy of pointer up and to right after each loop
-      //Decrement active value by 1
R^     //End program if input=1

*>-!.     //If active value =1 print 0 (occurs just before line below ends program)
*#~M!@    //If inactive value divisible by active value end program
**
**

fəˈnɛtɪk

Posted 2015-09-11T14:06:29.040

Reputation: 4 166

1

F#, 79 84 bytes

Fixed as per Dennis's suggestion.

stdin.ReadLine()|>int|>fun n->not(Seq.exists(fun i->n%i=0)[2..n-1]||n=1)|>printf"%b"

Now works for input = 1. Can be run same as before.


Original submission:

I didn't see an F# answer so...

stdin.ReadLine()|>int|>fun n->not(Seq.exists(fun i->n%i=0)[2..n-1])|>printf"%b"

Can be compiled with fsc or run directly in fsi. Takes a newline-terminated string from stdin and spits out a bool to stdout when run.

LSM07

Posted 2015-09-11T14:06:29.040

Reputation: 191

1

Unfortunately, that doesn't work for input 1. Try it online!

– Dennis – 2018-12-27T20:59:12.103

Remember, lambdas are allowed, so... 44?

– ASCII-only – 2019-01-31T08:38:52.410

1

C#, 116 bytes

Non fancy full program entry (cherry-picking some stuff from java):

class P{static void Main(string[] a){int i=2,n=int.Parse(a[0]);for(;i<n;)n=n%i++<1?0:n;System.Console.Write(n>1);}}

PmanAce

Posted 2015-09-11T14:06:29.040

Reputation: 131

1

Ink, 68 51 bytes

-(l)
+(n)[+]
->l
*(r)[{n}]
{n%(r+1)%n:->r|{n==(r+1)}}

Ink was made for writing interactive fiction. It was never meant to be a general-purpose programming language. So it only has one way to recieve input.
Choice prompts.
So the way it works is, if option 2 is labelled with the number you want to check, you pick option 2. Otherwise you pick option 1 to increment option 2's label. So, if you want to check if 15 is prime, you'd input

1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
2

TIO doesn't handle this, unfortunately.

-17 bytes by using read counts of named options instead of creating variables. This brings the overhead of taking input this way down to something like 24 characters (or even 18 if we don't care about minimising the text from the prompts), compared to the 10 for defining a parametrised stitch and returning from it.

Since full programs can use global variables and read counts, but stitches that have to be reusable can't, using full programs might actually pay off in places where I thought it wouldn't. Neat.

Sara J

Posted 2015-09-11T14:06:29.040

Reputation: 2 576

1

C++ (gcc), 85 bytes

#include<iostream>
int main(){int n,d=1;for(std::cin>>n;n%++d%n;);std::cout<<(n==d);}

Try it online!

  • -2 bytes thanks to @ceilingcat

Ungolfed

#include <iostream>
int main() {
    int n,d=1;                      
    for (std::cin >> n; n % ++d      // Trial division. If n dividible by d, end.
                          % n        /* No effect when n > 1.
                                        But required to make sure the loop ever ends if n == 1
                                     */
                            );
        ) { /* Empty loop body */ }  /* d is now the lowest number greater than 1 which
                                        divides n (or 2 if n == 1)
                                     */
    std::cout << (n==d);             // If that number is n, n must be prime.

}

Port of my Ink answer. Competitive in multiple other languages. I wasn't going to post this, but I couldn't find a C++ compiler that would compile the existing C++ answer.

Sara J

Posted 2015-09-11T14:06:29.040

Reputation: 2 576

@ceilingcat well spotted, thanks – Sara J – 2019-06-03T18:34:21.237

1

K (oK), 14 19 bytes

Solution:

2=+/d=_d:x%!x:. 0:`

Try it online!

Example:

root@c957fa0dccbd:/ok# echo 1 | node repl.js examples/prime.k
0
root@c957fa0dccbd:/ok# echo 2 | node repl.js examples/prime.k
1
root@c957fa0dccbd:/ok# echo 5 | node repl.js examples/prime.k
1
root@c957fa0dccbd:/ok# echo 97 | node repl.js examples/prime.k
1

Explanation:

Calculate number of factors for input, if equal to 2, then it's prime.

2=+/d=_d:x%!x:. 0:` / the solution
                0:` / read from stdin
              .     / value (ie convert "123" > 123)
            x:      / store input as x
           !        / range 0..x
         x%         / x divided by ...
       d:           / store as d
      _             / floor
    d=              / d equal to ...
  +/                / sum
2=                  / 2 equals?

streetster

Posted 2015-09-11T14:06:29.040

Reputation: 3 635

1

T-SQL, 116 bytes

WITH t AS(SELECT 2n UNION ALL SELECT n+1FROM t WHERE n<5e4)
SELECT*FROM z,t WHERE p%n=0AND n<p OPTION(MAXRECURSION 0)

There is an Oracle SQL answer here, but couldn't find a Microsoft T-SQL version for this classic question.

Notes:

  • Line break is for readability only.
  • Returns no rows if prime, returns 1 or more rows otherwise. This is allowed per this output rule.
  • Input is taken via pre-existing table \$z\$ with INT field \$p\$, per this input rule.
  • Handles values of \$p\$ up to the max value of the INT type, \$2^{31}-1\$

Explanation:

Generates an in-memory number table \$t\$ from 2 to 50k (which exceeds the square root of the max int), then joins to the input table \$z\$ and uses the modulo operator % to test for divisibility. Any values that divide evenly are returned, so an empty result means the input is prime.

OPTION(MAXRECURSION 0) is needed to recursively generate a table with more than 100 rows, unless you've altered this configuration setting on your SQL server.

Optimized for bytes, not speed; checks more values than is strictly necessary.

BradC

Posted 2015-09-11T14:06:29.040

Reputation: 6 099

1

Keg, 43 36 6 4 bytes (SBCS)

@MPƒ

-2 bytes using a BFL function

乁( ⏒ ͜ʖ ⏒ )ㄏ

Answer History

6 bytes

:;¡²$%

Try it online!

-30 bytes thanks to @JoKing

No TIO as it needs to be updated due to a bug found with the ² operator.

Transpiles to:

from KegLib import *
from Stackd import Stack
stack = Stack()
printed = False
duplicate(stack)
decrement(stack)
factorial(stack)
square(stack)
swap(stack)
maths(stack, '%')

if not printed:
    printing = ""
    for item in stack:
        if type(item) is Stack:
            printing += str(item)

        elif type(item) is str:
            printing += custom_format(item)
        elif type(item) == Coherse.char:
            printing += item.v

        elif item < 10 or item > 256:
            printing += str(item)
        else:
            printing += chr(item)
    print(printing, end="")

36 bytes (SBCS)

 ¿®n21®t{:©n≠©t0≠*|:©n$%[1|0]®t1+}©t.

Try it online!

-7 bytes by not using a variable to keep track of the count.

Explained

¿®n21®t{:©n≠©t0≠*|:©n$%[1|0]®t1+}©t.
¿®n                                 #Take the number to check and store it in variable n
   2                                #This will act as the divisor counter
    1®t                             #This will act as the return value, indicating the primality
       {:©n≠©t0≠*|                  #While the counter isn't equal to n and there isn't a divisor yet
                  :©n$%             #Take the counter and number, then swap for modulus
                       [1|0]®t      #Depending upon the result set t to 1 if it doesn't divide otherwise 0
                              1+}   #Increment the counter
                                 ©t.#Print the result

43 bytes (SBCS)

¿®n2®c1®t{©c©n≠©t0≠*|©n©c%[1|0]®t©n1+®c}©t.

It's a real ugly mess, but it works. I'll explain it all later. Also, doesn't work on TIO due to a bug that I found. It should work soon however.

Use the latest github interpreter

Lyxal

Posted 2015-09-11T14:06:29.040

Reputation: 5 253

1

W d, 5 bytes

(Possibly the only <=5-byter that does not use a prime or number factorization built-in?) (The two spaces here represent a tab)

àTéBΦ

repr'd:'‡T\tB⑸'

Unpacked:

m!Wk2=

Explanation

This is really difficult to read, so an explanation will be nice.

    W    % (implicit) Foreach the inclusive range from 1 to the input:
bam      % The "bam" magic! Just kidding. Evaluates b % a
         % Since b is out-of-bounds, this takes the upper-level input
         % as the operand.
   !     % Remove those that aren't valid multiples.
     k   % Is the length of the array ...
      2= % ... equal to 2?
         % i.e. primes have exactly 2 factors: 1 and itself

user85052

Posted 2015-09-11T14:06:29.040

Reputation:

Crax! That sounds like a good golfing language name. – Lyxal – 2020-01-01T06:16:05.230

I'm more saying that the source code looks like it says CRAX – Lyxal – 2020-01-01T06:19:29.483

Looks like there is a bug in the interpreter and I had to change the compression... – None – 2020-01-20T00:14:05.897

1

Pyth, 8 bytes

&>Q1!tPQ

Alternative that doesn't support values less than 2:

!tPQ

Loovjo

Posted 2015-09-11T14:06:29.040

Reputation: 7 357

Context: The 4-byte version was the only one at the time that comment was posted. – CalculatorFeline – 2016-03-20T04:18:51.900

1

Perl, 35 bytes

Uses regular expressions...

$_=1x$_;s/^(11+?)\g1+$//;print$_>1

That's 34 bytes of code, plus one byte for the -n switch needed to fetch a line from stdin. Outputs 1 if the number is prime, or nothing otherwise

r3mainer

Posted 2015-09-11T14:06:29.040

Reputation: 19 135

1

K, 25 bytes

`0:$~x!1+*/1+!(x:. 0:`)-1

kirbyfan64sos

Posted 2015-09-11T14:06:29.040

Reputation: 8 730

1How should this be run? I tried kona/k program <<< input, but that doesn't seem to work. – Dennis – 2015-09-11T19:00:01.197

1

Stuck, 3 bytes

iv|

Prints 1 for primes and 0 for non-primes. (The definition of "truthy/falsy values" means I can't use iv, because Stuck prints False/True without knowing what those are.)

Lynn

Posted 2015-09-11T14:06:29.040

Reputation: 55 648

1

Scala, 96 bytes

#!/usr/bin/env scala
print(((a:Int)=>if(a==2)true;else!2.to(a-1).exists(a%_==0))(args(0).toInt))

JVM and yet not last place :D

Does use some bash functionality but I'm using Scala so don't be too hard on me.

Martijn

Posted 2015-09-11T14:06:29.040

Reputation: 161

It's possible in 50 bytes. :)

– Emil Lundberg – 2015-09-24T12:07:16.137

@EmilLundberg That code won't run. You need a main method or use the same trick I used. – Martijn – 2015-09-24T15:13:47.447

Huh, you're right that it doesn't compile as scalac prime.scala. But it does run as scala prime.scala. – Emil Lundberg – 2015-09-24T15:47:39.863

1

K, 29 bytes

(x>1)&&/x!'2_!1+_sqrt x:0$0:`

Got this off Rosetta Code, so marked it as community wiki.

kirbyfan64sos

Posted 2015-09-11T14:06:29.040

Reputation: 8 730

1

XPath 2.0, 45 40 bytes

$i>1 and empty((2 to $i -1)[$i mod .=0])

For readability, incl. non-mandatory spaces (45 bytes)

$i > 1 and empty((2 to $i - 1)[$i mod . = 0])

In XPath, the only way to hand input like an integer to the processor is by passing it a parameter, in this case $i. This is hardly performant, and obvious improvement would be to use:

$i > 1 and empty((2 to math:sqrt($i) cast as xs:integer)[$i mod . = 0])

But since "shortest in any given language" and not performance was the goal, I'll leave the original in.

How it works

For people new to XPath, it works as follows:

  1. Create a sequence up to the current number:

    (2 to $i - 1)
    
  2. Filter all that have a modulo zero (i.e., that divide properly)

    [$i mod . = 0]
    
  3. Test if the resulting sequence is empty, if it non-empty, there is a divisor

    empty(...)
    
  4. Also test for special-case 1:

    $i > 1
    

The query as a whole returns the string true (2, 5, 101, 5483) or false (1, 4, 5487).

As a nice consequence, you can find all divisors (not prime divisors!) using an even shorter expression:

(2 to $i - 1)[$i mod . = 0]

will return (3, 5, 7, 15, 21, 35) for input 105.

Abel

Posted 2015-09-11T14:06:29.040

Reputation: 153

1

XSLT 3.0, 209 203 201 bytes

<transform xmlns="http://www.w3.org/1999/XSL/Transform" xmlns:x="x" version="3.0"><function name="x:p" expand-text="1"><param name="i"/>{$i>1 and empty((2 to $i -1)[$i mod .=0])}</function></transform>

Update 1: removed spaces in $i > 1, . = 0 and $i - 1.
Update 2: changed expand-text="yes" in expand-text="1", which is a new XSLT 3.0 feature

In expanded form, with the usual prefixes:

<xsl:stylesheet 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:x="x"
    version="3.0">    

    <xsl:function name="x:p" expand-text="yes">
        <xsl:param name="i"/>{
            $i > 1 and empty((2 to $i - 1)[$i mod . = 0])
    }</xsl:function>

</xsl:stylesheet>

This method uses the XSLT 3.0 feature to have a function as entry point (earlier versions did not support this). It uses the same XPath expression explained in my other post.

XSLT is notoriously verbose and starts with quite a few bytes declaring namespaces etc.

The function must be called with a typed value that derives from xs:integer. Most processor will consider that the default type if given an integer literal.

Abel

Posted 2015-09-11T14:06:29.040

Reputation: 153

1

TI-BASIC, 37 bytes

Prompt N
N=1
For(A,2,√(N
Ans+not(fPart(N/A
End
not(Ans

Prompts for N and displays 1 if N is prime or 0 otherwise.

Also in TI-BASIC:

Jakob

Posted 2015-09-11T14:06:29.040

Reputation: 2 428

1I rewrote it. This would have done better as a comment as well. – Zenohm – 2015-09-13T20:19:27.653

Nice. I modified my answer. Incidentally, I would have commented on your post but I created this account just to contribute this and didn't have enough reputation to comment. – Jakob – 2015-09-13T21:40:30.360

1

SWI-Prolog, 51 bytes

a(X):-X>1,\+ (Y is X-1,between(2,Y,I),0=:=X mod I).

This uses predicate between/3 which is not an ISO-Prolog predicate.

Fatalize

Posted 2015-09-11T14:06:29.040

Reputation: 32 976

You can save 6 bytes by shifting the range of I up by 1 (thus removing the necessity for Y) and using I-1 in the mod. TIO link

– 0 ' – 2017-12-20T20:42:46.133

You can save another 2 bytes by replacing a(X) with -X. – 0 ' – 2017-12-23T17:33:20.857

A bit of competition: https://codegolf.stackexchange.com/questions/57617/is-this-number-a-prime/151626#151626

– 0 ' – 2017-12-24T06:30:10.993

1

JavaScript, 57

Prime finding regex.

alert(!/^1?$|^(11+?)\1+$/.test(Array(prompt()+1).join(1)))

RK.

Posted 2015-09-11T14:06:29.040

Reputation: 497

That won't work. prompt() returns a string, not a number, so for input 3, you wind up with the string 31. The whole conversion isn't needed though. The questions allows reading the input in unary. – Dennis – 2015-09-17T19:40:25.877

Yeah, I was thinking of a way to fix it. Guess I never got around to actually fixing it... – RK. – 2015-09-17T19:59:41.357

1Just adding + before the prompt() should work. – ETHproductions – 2015-12-06T18:49:26.523

1

Python 3, 54 bytes

A just for fun post that abuses the all function.

y=int(input());print(all(y%p for p in range(2,y))|y>1)

Explanation:

Takes all the numbers from 2 to y and calculate the mod of y and that number and return false if any of those are 0.

Edits:
Add the 1 check (+4 bytes)
Fix the check 1 logic (0 bytes)
Remove the [] (Thanks FryAmTheEggman!) (-2 bytes)
Remove the -1 from range (Thanks FryAmTheEggman!) (-2 bytes)

J Atkin

Posted 2015-09-11T14:06:29.040

Reputation: 4 846

1all can take a generator, so you don't need []. Also range(a,b) already returns [a, ..., b-1]. – FryAmTheEggman – 2015-09-17T19:50:52.053

Cool, thanks for the tip :) – J Atkin – 2015-09-17T23:37:57.697

1

Python 2, 45 bytes

Not the smallest entry, but I took a slightly different approach to detecting the prime numbers. Maybe it inspires someone to create an even smaller version. I couldn't discover any more savings myself.

i=a=n=input()
while i>2:i-=1;a*=n%i
print a>1

Willem

Posted 2015-09-11T14:06:29.040

Reputation: 211

1

Scala, 50 bytes

val i=readInt
print(i>1&&(2 to i-1 forall(i%_>0)))

In case output to STDERR is forbidden, 65 bytes:

val i=scala.io.StdIn.readInt
print(i>1&&(2 to i-1 forall(i%_>0)))

Emil Lundberg

Posted 2015-09-11T14:06:29.040

Reputation: 181

1

Groovy, 92 91

i=System.in.newReader().readLine()as int
print i==2||i>1&&!(true in(2..i-1).collect{i%it<1})

Such a verbose way to read from stdin...

Kleyguerth

Posted 2015-09-11T14:06:29.040

Reputation: 61

Welcome to Programming Puzzles & Code Golf! – Dennis – 2015-09-26T01:44:08.407

Can you remove the space between ) and as? – HyperNeutrino – 2017-11-10T01:11:19.520

@HyperNeutrino Good catch! – Kleyguerth – 2017-11-10T18:54:39.260

1

Desmos, 108 104 94 bytes

k=2
1\left\{\sum _{n=2}^k\operatorname{sign}\left(\operatorname{mod}\left(k,n\right)\right)+2=k,0\right\}

To use, enter a new line. Then, call p\left(n\rgiht). The output will be bottom right on that line.

Edit 1: Shaved {x-1} to x.

Edit 2: Changed input format to a more STDIN-esque model.

Conor O'Brien

Posted 2015-09-11T14:06:29.040

Reputation: 36 228

I'm not sure how Desmos works or is scored, but \left\{2=\sum _{n=1}^x\left\{0=\operatorname{mod}\left(x,n\right),0\right\},0\right\} at... possibly 101 bytes? – lirtosiast – 2015-09-29T04:31:08.223

>

  • The link gives me timeouts. If I change the protocol to HTTP, it works. 2. The question doesn't allow submitting functions. The closest to user input I could find is putting k=1 in one field and \left\{\sum _{n=2}^k\operatorname{sign}\left(\operatorname{mod}\left(k,n\right)\right)+2=k,0\right\} in the next.
  • < – Dennis – 2015-09-29T06:58:46.933

    1

    Ceylon, 107 112

    That was my first try:

    shared void p(){if(exists a=process.readLine(),exists c=parseInteger(a)){print(!(2:c-2).any((d)=>c%d<1));}}
    

    This is a simple trial division of all numbers between 2 and c - 1, using the range operator : and the any method for iterating.

    But this will also note 1 as prime. So here the corrected version:

    shared void p(){if(exists a=process.readLine(),exists c=parseInteger(a)){print(1<c&&!(2:c-2).any((d)=>c%d<1));}}
    

    That works for numbers up to 2^63-1 ... and takes quite long for larger primes (or composite numbers with large factors) – I think it took about half a minute to confirm 2147483647 (=2^31-1) as prime.

    The formatted version:

    shared void p() {
        if (exists a = process.readLine(),
            exists c = parseInteger(a)) {
            print(1 < c && !(2 : c - 2).any((d) => c % d < 1));
        }
    }
    

    I originally wrote the check as d.divides(c), but the modulo operator is so much shorter.

    As a bonus, here is a variant for arbitrary size integers, though there is not really a point in using this due to the slowness.

    import ceylon.math.whole {
        w=parseWhole,
        t=two,
        o=one
    }
    shared void q() {
        if (exists a = process.readLine(),
            exists c = w(a)) {
            print(c == t || !(t .. c - t).any((d) => c % d < o));
        }
    }
    

    The : range operator needs an Integer (the number of elements) as a second argument, so we use the .. operator, which takes the same type as the first argument, i.e. here a Whole, as second argument (the upper limit). 2..c-1 is the same as 2:c-2 for large c, but this becomes an empty list for two (and a non-empty list for one), so we need a different exception here than in the Integer version. It becomes mainly longer due to the imports.

    The space-reduced version has length 153:

    import ceylon.math.whole{w=parseWhole,t=two,o=one}shared void q(){if(exists a=process.readLine(),exists c=w(a)){print(c==t||!(t..c-o).any((d)=>c%d<o));}}
    

    Paŭlo Ebermann

    Posted 2015-09-11T14:06:29.040

    Reputation: 1 010

    1

    Simplex v.0.5, 23 bytes

    Can probably be golfed. It's really the square root declaration that hurts. *regrets removing p (prime checking) command from syntax and sighs*

    i*R1UEY{&%n?[j1=o#]R@M}
    i                        ~~ takes numeric input
     *                       ~~ copies and increments pointer
      R1UEY                  ~~ takes the square root and rounds it down
           {              }  ~~ repeats until zero cell met at end
            &                ~~ read and store the value to the register
             %               ~~ takes input mod current, move pointer left
              n              ~~ logically negates current (0 -> 1, 1 -> 0)
               ?[     ]      ~~ evaluates inside if the current cell
                 j1=         ~~ inserts a new cell to check for a 1 case
                    o#       ~~ outputs the result and terminates program
                       R@    ~~ goes right, pulls the value from the register
                         M   ~~ decrement value
    

    Conor O'Brien

    Posted 2015-09-11T14:06:29.040

    Reputation: 36 228

    1

    Mathematica, 48 47 bytes

    <<PrimalityProving`
    Echo@ProvablePrimeQ@Input[]
    

    Saved 1 byte thanks to Martin Büttner. Echo is a new function in Mathematica 10.3. In older versions, use Print.

    alephalpha

    Posted 2015-09-11T14:06:29.040

    Reputation: 23 988

    1

    Groovy, 36 bytes

    I found a variation of this in a course on groovy that I'm taking:

    p={x->x==2||!(2..x-1).find{x%it==0}}
    

    Test code:

    println ((2..20).collect {"Is $it prime? ${p(it) ? 'Yes':'No'}"})
    

    J Atkin

    Posted 2015-09-11T14:06:29.040

    Reputation: 4 846

    p={x->!(2..<x).find{x%it==0}|x==2}, saves 2 bytes. – Gurupad Mamadapur – 2017-01-17T09:06:24.120

    1

    PHP, 59 bytes

    Credit goes to Geobit's answer. I basically just changed it from java to PHP.

    function f($n){for($i=2;$i<$n;)$n=$n%$i++<1?:$n;echo $n>1;}
    

    Reed

    Posted 2015-09-11T14:06:29.040

    Reputation: 141

    2function f($n){for($i=1;++$i<$n;)$n=$n%$i?$n:0;echo$n>1;} is shorter. The language name is "Java" by the way, not "java". – Blackhole – 2015-11-07T10:59:55.093

    1

    AppleScript, 158 Bytes

    Note that the special case for 1 adds a full 20 bytes.

    set x to(display dialog""default answer"")'s text returned's words as number
    repeat with i from 2 to x/2
    if x mod i=0 then return 0
    end
    if x=1 then return 0
    1

    If this program ever returns 0, it won't get to the final statement, which returns 1. Therefore, truthy is 1, falsey is 0.

    Addison Crump

    Posted 2015-09-11T14:06:29.040

    Reputation: 10 763

    This might be golfed more. Might not need words. Might not need as number, because x+0 might make a number. Might not need spaces after numbers (from2, 0then, 1then). I can't check because I'm off my Mac. – kernigh – 2017-11-17T00:55:08.353

    1

    Microscript II, 2 bytes

    N;
    

    Unlike the original Microscript, Microscript II provides a builtin for primality testing.

    SuperJedi224

    Posted 2015-09-11T14:06:29.040

    Reputation: 11 342

    1

    DStack, 66 bytes

    025SSd01kKCccscS0kT0cK1kAsd34SSd1ccd0sd1ddsScsdk0cD0cS0kTcdsKkdtcK
    

    DarkPhantom

    Posted 2015-09-11T14:06:29.040

    Reputation: 191

    1

    C, 59 bytes

    main(_,i){scanf("%d",&_);for(i=_;_%--i;);putchar(!--i+48);}
    

    If anything other than 1 counted as "falsey", then this would be 3 bytes smaller:

    main(_,i){scanf("%d",&_);for(i=_;_%--i;);putchar(i+48);}
    

    Please tell me if the second one is valid.

    takra

    Posted 2015-09-11T14:06:29.040

    Reputation: 793

    >

  • Your code gives a FP exception for input 1 on my system. 2. An integer n is falsy if the statement if(n){...} does not execute .... For C, this means that only 0 is falsy.
  • < – Dennis – 2015-11-16T03:33:48.523

    1

    Mouse-2002, 22 bytes

     ?x:x.1-&FACT &SQR x.\!
    

    Uses Wilson's theorem:

    ?x:                      ~ get an integer input; put it in x
       x.1-                  ~ put x-1 on the stack
           &FACT             ~ factorial it and push
                 &SQR        ~ square it and push
                      x.\!   ~ modulo (x-1)!^2 % x; print
    

    The version that doesn't use a variable is eight bytes longer, but this is because Mouse's stack operations have four- and five-byte long names. :(

    cat

    Posted 2015-09-11T14:06:29.040

    Reputation: 4 989

    1

    Pyke, 2 bytes

    _P
    

    If the P function is given a negative number, it returns whether it's prime or not.

    Blue

    Posted 2015-09-11T14:06:29.040

    Reputation: 26 661

    1

    Factor, 56 bytes

    Literally ungolfable.

    USING: math.primes conv io ;
    readln string>number prime?
    

    cat

    Posted 2015-09-11T14:06:29.040

    Reputation: 4 989

    I haven't used Factor recently, so I don't remember what must be done to print the t/f result of prime? – kernigh – 2017-11-17T01:04:31.640

    @kernigh print or maybe >string print? why are you asking – cat – 2017-11-18T18:24:29.993

    The task wants the result on STDOUT or something like it. This code leaves the result on the stack, and I can't remember if Factor would print the result, or if one must add more code to print the result. – kernigh – 2017-11-20T05:59:12.890

    1

    Pyth, 2 bytes

    P_
    

    Try it here!

    Pyth now has implicit input!

    Blue

    Posted 2015-09-11T14:06:29.040

    Reputation: 26 661

    1

    Befunge 93, 33 bytes

    &10p1 >1+:10g\`v
    1.@.0_^#!%\g01:_
    

    Also works by trial division, as the other befunge post, but is substantially shorter.

    Interpreter here.

    SE - stop firing the good guys

    Posted 2015-09-11T14:06:29.040

    Reputation: 529

    1

    Unipants' Golfing Language, 53 47 bytes

    i$cu^d^-l_u^^/%cu%?%d%:_?coc$d$$:__^d^-:_u?cuo:
    

    Try it online!

    (It has been implemented as a standard example in the interpreter.)

    Leaky Nun

    Posted 2015-09-11T14:06:29.040

    Reputation: 45 011

    1

    Molecule, 3 bytes

    Inp
    

    Explanation:

    Inp
    I   read input
     n  convert to number
      p primality test
    

    user47018

    Posted 2015-09-11T14:06:29.040

    Reputation:

    1

    LiveCode 8, 708 bytes

    on mouseUp
        ask ""
        put p(it) into field "a"
    end mouseUp
    function p n
        if n is 1 then return false
        repeat with i=2 to n-1
            if n mod i is  0 then return false
        end repeat
        return true
    end p
    

    This code can be placed inside any button. It will print true or false to a field named a. It should work for small-ish integers, but will probably freeze/crash on anything too large. Byte count is size of saved LiveCode stack with one button and one field and with this code in the button.

    penalosa

    Posted 2015-09-11T14:06:29.040

    Reputation: 505

    1

    Fith, 30 bytes

    *math* load line >int prime? .
    

    This language isn't great for golfing...

    This uses the prime? function in the *math* library. Here's a version using only builtins, which is pretty much what the library function does:

    line >int -> x 2 x range { x swap mod } map all .
    

    Explanation:

    line           \ line of input
    >int           \ cast to int
    -> x           \ set x to the top of the stack
    2 x            \ push 2, then x
    range          \ range from [2, x)
    { x swap mod } \ anonymous function which calculates x mod its argument
    map            \ map that function onto the generated range
    all            \ return 1 if everything in the list is Boolean true, else 0
    .              \ print the top of the stack
    

    You may be able to tell that this language is just a little bit inspired by Forth. It also takes cues from PostScript, Python, and functional programming.

    jqblz

    Posted 2015-09-11T14:06:29.040

    Reputation: 2 062

    1

    C, 54 bytes

    i;main(j){for(scanf("%d",&i);i%++j;);putchar(49^j<i);}
    
    • Must be run without parameters, so j is initialized to 1.
    • Reads from standard input.
    • Prints 1 for primes, 0 for composites (no newline).
    • Could save 2 bytes with unprintable output - I'm not sure if \x00 and \x01 qualify as falsy/truthy.

    ugoren

    Posted 2015-09-11T14:06:29.040

    Reputation: 16 527

    '\0' and '\1' are truthy and falsy according to our definition, so printing NUL or SOH should be fine. – Dennis – 2016-06-16T22:58:35.467

    1

    brainfuck, 208 202 bytes

    This is the code I wrote for my Sesos answer. See that answer for a detailed explanation.

    This code assumes input fits in a single cell. Attempting to take input from an empty file must set the cell to zero. It works on an unbounded tape with no wrapping, including one that can go negative. It works for all positive numbers. Output will be a byte with a value of 0 or 1.

    [->+>+<<]++>[-<->]+<[>-<,]>[->+<]>[[->>>+<<<]>>>[->>>+<<<<<<+>>>]>>>-]<<<<<<+[<<<<<<]>>>>>>[-<+>]++[<[->>>+<<<]>>>[->>>+<<<<<<+>>>]>>>>][[->-[>+>>]>[+[-<+>]>+>>]<<<<<]<<<<<<]>>>>>>>>[>]>[<+>,]+<[>-<-]>.
    

    mbomb007

    Posted 2015-09-11T14:06:29.040

    Reputation: 21 944

    1

    Nim, 70 56 bytes

    import os,math
    let x=1.paramStr.len
    echo fac(x-1)^2mod x
    

    Uses Wilson's theorem; that is, x is prime if (x - 1)!² mod x is 1.

    Takes input in unary (any character) via the first command-line argument. Outputs 1 if the input is prime, and 0 otherwise. To test:

    $ nim c prime.nim
    $ ./prime 11111111
    0
    $ ./prime 1111111
    1
    $ ./prime 1
    0
    

    Note that according to the Nim os docs, this solution will not work on POSIX as paramStr isn't available for some reason.

    Copper

    Posted 2015-09-11T14:06:29.040

    Reputation: 3 684

    1

    Idris, 78 bytes

    main:IO()
    main=let S n=length!getLine in print$0<(pow(product[1..n])2`mod`S n)
    

    This takes input as a unary string (it uses the string length as input).

    Compile this with idris -O3 or you’ll spend forever in Nat-computey hell.

    Lynn

    Posted 2015-09-11T14:06:29.040

    Reputation: 55 648

    1

    RPN, 16 bytes

    lambda x isprime
    

    This define a function checking the primality of it's argument.

    Call like this:

    <number> lambda x isprime eval
    

    TuxCrafting

    Posted 2015-09-11T14:06:29.040

    Reputation: 4 547

    1

    Python 3.5, 58 52 58 bytes

    import math;n=int(input());print(math.factorial(n-1)**2%n)
    

    I unassumed. Thanks Dennis.

    I assumed something about the way the input would be given. Thanks daHugLenny.

    import math;lambda n:print(math.factorial(n-1)**2%n)

    NegativeEpsilon

    Posted 2015-09-11T14:06:29.040

    Reputation: 11

    You can save 13 bytes by using a lambda: import math;lambda n:math.factorial(n-1)**2%n – acrolith – 2016-08-07T02:21:56.027

    In this particular challenge, lambdas are not allowed. The spec says *Write a full program that...* (CC @daHugLenny) – Dennis – 2016-08-10T02:57:41.237

    1

    PHP, 70 65 bytes

    for($i=2;$i<$n=$argv[1];$i++)if(is_int($n/$i)?1:0){echo 0;break;}
    

    Empty output if the number is prime, print 0 if the number is not prime.

    Not very original or best answer, but this is it...

    Test online

    Mario

    Posted 2015-09-11T14:06:29.040

    Reputation: 3 043

    1

    Ruby, 33 bytes

    A little longer than other solutions, but doesn't require flags:

    require'prime';p$*[0].to_i.prime?

    Can be run putting in a file, say prime.rb:

    ruby prime.rb 97

    Or directly on the command line:

    ruby -e "require'prime';p$*[0].to_i.prime?" 97

    jose_castro_arnaud

    Posted 2015-09-11T14:06:29.040

    Reputation: 229

    1

    Logy, 82 bytes

    f[X]->X<2&1|X*f[X-1];p[&1]->0<0;p[X]->f[X-1]%X==X-1;main[A]->print[p[atoi[A(1)]]];
    

    I have no idea what I am doing.

    Use Wilson's theorem.

    Ungolfed code:

    factorial[X] -> X < 2 & 1 | X*factorial[X - 1];
    prime[&1] -> FALSE;
    prime[X] -> factorial[X - 1]%X == X - 1;
    main[Args] -> print[prime[atoi[Args(1)]]];
    

    TuxCrafting

    Posted 2015-09-11T14:06:29.040

    Reputation: 4 547

    1

    Racket 75 bytes

    (let p((c 2))(cond[(> c(sqrt n))#t][(= 0(modulo n c))#f][else(p(+ 1 c))]))
    

    Ungolfed:

    (define (f n)                 ; COMMENTS: 
      (let loop ((c 2))
        (cond
          [(> c (sqrt n)) #t]     ; if no divisor found till sq root of number, it must be a prime
          [(= 0 (modulo n c)) #f] ; if divisor found, it is not a prime
          [else (loop (add1 c))]  ; try with next number
          )))
    

    Following version is longer but more efficient for checking larger numbers since it keeps & uses previously found prime numbers:

    (λ(N)(define(p n(o'(2))(c 2))(cond[(> c n)o][(ormap(λ(x)(= 0(modulo c x)))o)
    (p n o(+ 1 c))][else(p n(cons c o)(+ 1 c))]))(= N(car(p N))))
    

    Ungolfed version:

    (p=subfunction to build prime number list till n; o= list of prime numbers found; c=current number being checked)

    (define f
      (λ (N) 
        (define (p n (o '(2)) (c 2))
          (cond
            [(> c n) o]
            [(ormap (lambda(x) (= 0 (modulo c x))) o)
                    (p n o (add1 c)) ]
            [else (p n (cons c o) (add1 c))]))
        (= N (first (p N)))))
    

    Testing:

    (f 109)
    (f 10)
    (f 11)
    (f 12)
    (f 13)
    (f 49)
    (f 43)
    (f 57)
    (f 47)
    

    Output:

    #t
    #f
    #t
    #f
    #t
    #f
    #t
    #f
    #t
    

    rnso

    Posted 2015-09-11T14:06:29.040

    Reputation: 1 635

    If I understand correctly, the solution given is a snippet, not a full program, because it inputs from the variable n. – Esolanging Fruit – 2017-04-24T06:43:17.700

    1

    Unary, 165192711826249169871716905363504385377479408598339493836 bytes

    Well, basically 165192711826249169871716905363504385377479408598339493836 zeros. Here is a Java program that prints the actual program:

    public class Main{
      public static void main(String[]args){
        for(BigInteger i = new BigInteger("165192711826249169871716905363504385377479408598339493836"),BigInteger j = BigInteger.ZERO;j.compareTo(i)!=0;j = j.add(BigInteger.ONE)){
          System.out.println("0");
        }
      }
    }
    

    This a port of this Brainfuck answer.

    Roman Gräf

    Posted 2015-09-11T14:06:29.040

    Reputation: 2 915

    1

    PHP, 43 39 54 44 43 bytes

    improved my answer from stackoverflow:

    for($i=$n=$argv[1];--$i&&$n%$i;);echo$i==1;
    

    Run with -r. prints 1 if argument is prime, empty string else.

    loops $i down from $n-1 until it finds a divisor of $n; $n is prime if that divisor is 1.

    6 bytes extra for the 1 case. almost happy.

    Titus

    Posted 2015-09-11T14:06:29.040

    Reputation: 13 814

    1

    Batch, 121 bytes

    set i=2
    if %1%==2 exit /b
    if %1%==1 echo 0
    :b
    set /a a=%1%%%i%
    set /a i=%i%+1
    if %i%==%1% exit /b
    if %a%==0 echo 0
    goto b
    

    Outputs 0 if it's no prime. This works only for inputs input > 0.

    Roman Gräf

    Posted 2015-09-11T14:06:29.040

    Reputation: 2 915

    1

    Dip, 1 byte

    P
    

    Pretty self-explanatory...

    Oliver Ni

    Posted 2015-09-11T14:06:29.040

    Reputation: 9 650

    0

    Pyret, 58 bytes

    i=1
    (i == 1) or any({(x):num-modulo(i,x) == 0},range(2,i))
    

    You can try this online by copying the code into the online Pyret editor!

    Pyret programs are designed to be run in the online editor, so there's no way to read from STDIN. Here, the variable i represents the input. The program returns true if i is not prime and false if i is prime.

    Tango

    Posted 2015-09-11T14:06:29.040

    Reputation: 121

    1If there's no input from STDIN, maybe its best to do a function instead – Jo King – 2018-06-20T00:14:01.610

    1@JoKing This challenge doesn't allow functions. – Ørjan Johansen – 2018-06-20T01:46:16.427

    0

    Yabasic, 43 bytes

    Input""n
    For d=2To n
    p=p+!Mod(n,d)Next
    ?p=1
    

    Try it online!

    Taylor Scott

    Posted 2015-09-11T14:06:29.040

    Reputation: 6 709

    0

    Pepe, 53 bytes

    Uses trial-and-error modulus, so gets slower in larger integers.

    REeEREErEEEEErREEeEeEReeREEEEeEeEReRererrEEreEErEeree
    

    Try it online! Outputs 0 for truthy and none for falsy.

    u_ndefined

    Posted 2015-09-11T14:06:29.040

    Reputation: 1 253

    0

    strict, 122 bytes

    num t
    num i 2
    num r
    get t
    jen t 1 n
    jen t 2
    mrk m
    set r t
    mod r i
    add i 1
    jen t i
    jgn r 0 m
    mrk n
    out: not 
    mrk
    out: prime
    

    Reads input from stdin. Because the language doesn't have truthy/falsy values, it outputs either "prime" or "not prime".

    Ungolfed:

    num target
    num i 2
    num remainder
    get target
    
    # exceptions
    jen target 1 not
    jen target 2 prime
    
    # loop from 2 to target
    mrk main
        set remainder target
        mod remainder i
        add i 1
    jen target i prime
    jgn remainder 0 main
    
    mrk not
        out: not\s
    
    mrk prime
        out: prime\n
    

    programical

    Posted 2015-09-11T14:06:29.040

    Reputation: 41

    Welcome to Code Golf Stack Exchange! I'm not sure how best to test this; do you have a link to some sort of online interpreter? I can't access the link thanks to my stupid corporate network restrictions. – Giuseppe – 2019-06-28T15:50:44.273

    @Giuseppe Unfortunately, there is no online interpreter. How much are you restricted? Would a direct mega.nz download link work?

    – programical – 2019-06-28T16:12:03.970

    0

    Rust, 124 bytes

    fn main(){let s=&mut"".into();std::io::stdin().read_line(s);let n=s.parse().unwrap();print!("{}",n>1&&(2..n).all(|x|n%x>0))}
    

    Try it online!

    How complicated it has to be to read an integer from stdin...

    ruohola

    Posted 2015-09-11T14:06:29.040

    Reputation: 414

    0

    Wren, 48 43 bytes

    I feel surprised that I think better when using W instead of Wren. Also, the 1-primalty problem is fixed.

    Fn.new{|i|(1..i).where{|x|i%x==0}.count==2}
    

    Try it online!

    user85052

    Posted 2015-09-11T14:06:29.040

    Reputation:

    1Fails for 1, just like your previous answer. – Jo King – 2019-12-21T22:23:17.200

    0

    Spice, 93 bytes

    ;i;m;t;c;r@REA i;ADD 0 1 c;SUB i 1 m;ADD c 1 c;MOD i c t;SWI t 1 9;SWI c m 3;ADD 0 1 r;OUT r;
    

    This outputs [1] for a prime, and [] for a non-prime.

    Un-golfed explanation:

    ;i;m;t;c;r@ - Declare vars (@ marks end of declarations)
    (line 0) REA i;      - Read input into i
    (line 1) ADD 0 1 c;  - Set counter c = 1
    (line 2) SUB i 1 m;  - Set store input-1 as m
    (line 3) ADD c 1 c;  - Start of loop, c+=1
    (line 4) MOD i c t;  - Store mod of  t=i%c
    (line 5) SWI t 1 8;  - if t < 1 (if non-prime, leave loop) to line 8
    (line 6) SWI c m 3;  - if c < m continue loop (go to line 3)
    (line 7) ADD 0 1 r;  - set result as truthy value - r = 1
    (line 8) OUT r;      - output result, r
    

    Slord6

    Posted 2015-09-11T14:06:29.040

    Reputation: 71

    0

    tq, 10 bytes

    ?-1!xp*p%?
    

    Explanation

    Uses Wilson's theorem to do the prime-checking.

    ?-1!        # Generate all numbers from 2 to input - 1
        x,      # Preserve this value, prevent it from being printed
          p*p   # Generate all possible combinations of those numbers
             %? # Does the product of any combination equal to the input?
    

    user85052

    Posted 2015-09-11T14:06:29.040

    Reputation:

    0

    Symbolic Raku, 17 bytes

    $_=?($_%%[^] ^$_)
    

    Try it online!

    Suprisingly, this is shorter than the my previous Raku answer, though one byte longer than using the built-in

    Explanation:

    $_=                # Set the output to
       ?(           )  # The boolean of
         $_            # If the input is
           %%          # Divisible by
             [^]       # Only one of
                 ^$_   # The range from 0 to input-1
    

    Jo King

    Posted 2015-09-11T14:06:29.040

    Reputation: 38 234

    0

    C# (Visual C# Interactive Compiler), 62 bytes

    int n=int.Parse(ReadLine()),i=2;while(n%i>0&i++<n);Write(n<i);
    

    Try it online!

    Jirka Picek

    Posted 2015-09-11T14:06:29.040

    Reputation: 171

    0

    LCGFuck inputNumeric=true, 138 bytes

    (Will turn this into a GitHub repository soon)

    1 1im1-1
    1 1om2-1
    1 1om1 1
    <{>>>o+1 1m0<<om
    <}>>1-1om0>+om
    <<<++1 1 1 32
    1 4 12 69 4
    1 1 7 78 2
    {>>>+++++c+c+++++c+++<<c<{+}}>>>c++c<c+c+c
    

    Try it online through this TIO JS interpreter

    A brainfuck variant invented by me but uses a list of linear congruential generators (LCGs) instead of cells.

    The \$N=1\$ case took too much more bytes to deal with.

    Ungolfed and Explanation

    1 1 im 1 -1                    # Storage for the input, since the input can only be retrieved once
    1 1 om 2 -1                    # Used to prevent wrapping. Necessary for handling N = 1 case
    1 1 om 1 1                     # Counts up from 2 to N
    <{                             # While N % counter == 0 (If N == 1 the loop is skipped)
     >>>o+                         #  Gets and increments the counter value
     1 1 m 0 <<om                  #  Use a new LCG to check N % counter
    <}                             # Loop
    >>
    1 -1 om 0 >+om                 # The output of this LCG will be 2 if N is prime
    <<<++                          # Move two states forward (2 - 2 = 0 if prime)
    1 1 1 32                       # LCG for space
    1 4 12 69 4                    # LCG for E, I, M
    1 1 7 78 2                     # LCG for N, O, P, R, T
    {>>>+++++c+c+++++c+++<<c<{+}}  # Output "NOT " if this LCG is not at state 0
    >>>c++c<c+c+c                  # Output "PRIME"
    

    Technical Details

    (Copied from my obsolete challenge proposal on an LCGFuck interpreter)

    An LCG is defined with 5 numbers, like 12345 678 90 -123 45. The last two numbers are optional and default to 0.

    LCGFuck has 3 storage variables, the LCG list, the number list and the number memory. The functions are as follows:

    • LCG list: Cyclic list that stores all LCGs created. Every entry is in turn a list of 5 numbers in the order [a, b, c, d, e] as notated in the introduction. It has a pointer that controls which LCG is chosen at the moment, and moves in a cycle through the list.
    • Number list: Ordinary list that stores the numbers for LCG definition. It can store at most 5 numbers.
    • Number memory: A single variable that can be read and written with an input or an LCG output.

    LCGFuck has 14 operators, namely:

    • \n (newline): Creates an LCG with the numbers in the order of [a, b, c(, d(, e))] and clears the list if the number list contains 3 or more numbers. No-op if the number list contains no more than 2 numbers.
    • <: Shifts to the previous LCG circularly (back to the last when moving from the first).
    • >: Shifts to the next LCG circularly (back to the first when moving from the last).
    • +: Moves the current LCG to the next state (calculates x' = (a * x + b) mod c)
    • c: Prints the output of the current LCG as a character (with the output as the codepoint).
    • n: Prints the output of the current LCG as a number (with a trailing space)
    • o: Writes the output of the current LCG to number memory
    • i: Reads a value from the input and writes it to number memory. There is two input mode, one reading a character one time (inputNumeric=false), and one reading a number one time (inputNumeric=true)
    • s: Reads the value from number memory and seeds the current LCG with it
    • m: Reads the value from number memory and pushes it to the number list
    • []: Output loop. Executes the loop if the current LCG is non-zero
    • {}: State loop. Executes the loop if the state of the current LCG is non-zero

    An integer, optionally with a negative sign at the front, pushes the number to the number list. Any characters other than the newline \n and any of -0123456789[]{}<>+cimnos are no-ops.

    The code runs linearly from the first characters, except when loop ends are encountered. Loops can be nested but must be paired accordingly from the innermost loop to the outermost loop.

    Shieru Asakoto

    Posted 2015-09-11T14:06:29.040

    Reputation: 4 445

    0

    Help, WarDoq!, 1 byte

    p
    

    Outputs 1 if the input is prime (positive or negative, differs from the other solution), 0 otherwise.

    VFDan

    Posted 2015-09-11T14:06:29.040

    Reputation: 173

    0

    Mathcad 15, 15 bytes

    valid in both Mathcad 15 and Mathcad Prime 3.1

    not valid in Mathcad Prime Express (no symbolics)

    n:=103   IsPrime(n)->
    

    where -> is the symbolic evaluation operator

    although

    IsPrime(103)->
    

    would be shorter still

    edit 103 to taste

    In response to the question from @Dennis the following might be helpful

    Mathcad is, from a user perspective, a whiteboard in which you directly type in your expressions, programs, plots, etc and obtain the results either directly in the worksheet or export them (in some instances (eg, Excel) you can embed a "component" directly in the worksheet and access the application through the component. It also allows you mix text and expressions across the page(s) as well as down, making it almost a literate programming system.

    In the above example, the input and output are all on the worksheet. All you need to do to change the 103 is click on it, delete the digits and add your own. If auto calculation is on, then Mathcad updates the output from the IsPrime function, otherwise choose the manual calculate option (which is simply pressing F9 in the case of Mathcad 15). Simples.

    There are also file read and write functions if you want to do it the hard way, but Mathcad's interface makes it completely unnecessary for such a simple task.

    See this link https://www.ptcusercommunity.com/message/420452#420452 for an example of an over-the-top means of finding the middle value of a vector (list). What you see is what I typed in or what Mathcad computed in response. If I were to edit the worksheet to, say, change v1's value from seq(6)+11 to seq(9)+20, Mathcad would almost instantly change the output from the "augment" expression to show the new value of v1 together with its corresponding indices. I'd have to manually rearrange the v2 expressions to make room for the new v1 but that's as simple as dragging the mouse around the affected expressions, which selects them, and then dragging the expressions to the new location. Easy. Even I can do it!

    Stuart Bruff

    Posted 2015-09-11T14:06:29.040

    Reputation: 501

    2Is there no way of obtaining user input in Mathcad? – Dennis – 2015-09-12T01:28:29.033

    That depends upon what you mean by "obtaining user input". I've added a bit more text to my original post to help clarify how a typical Mathcad might change the number to be tested - just edit it directly on the Mathcad "worksheet" and Mathcad will update the result. – Stuart Bruff – 2015-09-12T08:23:14.207

    Please make sure you add in the byte count to the title of your post so that it can register on the leader board. Check the question on how to format it. – rayryeng - Reinstate Monica – 2015-09-12T16:28:47.430

    Do you which algorithm is used by IsPrime? Are you sure it's not a probabilistic one like Miller-Rabin? – Martin Ender – 2015-09-14T09:45:12.300

    0

    STATA, 62 bytes

    di _r(a)
    gl c=$a-1
    forv b=2/$c{
    if!mod($a,`b') gl c=0
    }
    di $c
    

    Ungolfed

    display _request(number)
    global maxtest = $number-1
    forvalues counter=2/$maxtest{
        if !mod($number,`counter') global $maxtest = 0
    }
    display $maxtest
    

    This tests every integer from 2 to number-1 for divisibility. By printing number-1 as the output, it prints a 0 for an input of 1. Today I learned that STATA has truthy/falsey values. Previously I assumed I could only use (in)equalities in conditions. Note: this does not yet work in the online interpreter and is only valid in the proprietary STATA interpreter.

    bmarks

    Posted 2015-09-11T14:06:29.040

    Reputation: 2 114

    0

    Mathematica, 33 bytes or 61 bytes

    Two answers are provided:

    Manipulate[PrimeQ[n],{n,1,255,1}]
    
    
    <<PrimalityProving`
    Manipulate[ProvablePrimeQ[n],{n,1,255,1}]
    

    As explained in the implementation documentation, PrimeQ[] is known to produce correct output for arguments less than 10^16 (though is widely believed, on heuristic arguments, to be correct for any input). (User Charles claims this bound can be increased to 2^64 ~= 10^19 in an answer here.) It tests for small primes, uses Miller-Rabin with bases 2 and 3, then uses a Lucas test. ProvablyPrimeQ[] uses a much slower testing procedure, Atkin-Morain (reference with further citations), but is known to be correct and can produce a certificate of primality or witness of compositeness, if desired. So, PrimeQ[] meets the challenge (255 is much less than 10^16), but ProvablyPrimeQ[] meets the specification "primality test" without reservation.

    These wrap the PrimeQ[] call into a widget for user interaction. See the docs for a visual example. This widget provides a slider over the range 1 to 255 with unit increments. Next to the slider is a button to provide additional controls: a textbox for direct input and "playback" controls. It's interesting (at most, three times :-) ) to watch this widget iterate through the range.

    Eric Towers

    Posted 2015-09-11T14:06:29.040

    Reputation: 706

    The rule for hardcoding are "If (and only if) your language is unable to accept any kind of user input" (emphasis mine). Mathematica can take user input, so I believe it needs to use that (see this answer for an example).

    – Sp3000 – 2015-09-13T12:50:40.567

    @Sp3000 : Huh. Did not know about Input[]. So, ..., is the local protocol to withdraw the whole solution, or keep the documentation that the solution you cite (and many others here) do not provably test for primality? – Eric Towers – 2015-09-13T23:55:13.477

    @Sp3000 : I now also perceive a bit of a gap between the rule you reference and the other rule "If your language is able to read from STDIN or accept command-line arguments". I certainly can't make Mathematica do that (and I think this negative capability is why I went the way I did). But I agree that Mathematica meets the description you have. Yet I can imagine settings where neither clause applies. – Eric Towers – 2015-09-13T23:59:35.980

    The rule you mention was indeed badly phrased, so I've rephrased it. However, the rule Sp3000 mentioned clearly stated that hardcoding is only allowed if there are no alternatives. Since Mathematica has Input[], your three snippets are invalid in its current form. – Dennis – 2015-09-14T03:59:00.673

    @Dennis : The third snippet receives user input, as described. – Eric Towers – 2015-09-14T06:33:42.107

    ProvablePrimeQ doesn't work without Needs["PrimalityProving"]`. – Martin Ender – 2015-09-14T09:29:20.193

    @MartinBüttner : Right you are. You wouldn't believe how much I've customized my init.m. Or maybe you would (especially after this). – Eric Towers – 2015-09-15T04:59:35.393

    0

    Mumps, 29

    R J F I=2:1:J W:I=J J Q:J#I=0
    

    Output is nothing if not prime, repeat the number if prime. [[ Looks kind of ugly, but I did this to save a carriage return, as carriage returns aren't echoed on user input. It was also easier to see the number out again than just a 1 or zero, that was confusing on longer queries, especially if they contained a '1' at the end. ]]

    Test runs:

    USER>R J F I=2:1:J W:I=J J Q:J#I=0
    1234312343
    USER>R J F I=2:1:J W:I=J J Q:J#I=0
    12345
    USER>R J F I=2:1:J W:I=J J Q:J#I=0
    11141111114111
    

    In these three runs, 12343, 12345 and 1114111 are the inputs (the 'R J' reads standard input into the J variable - but the carriage return is not echoed to the output).

    As 12343 is a prime, it's echoed directly after the input; as 12345 is not prime, it sits alone. 1114111 is prime, but if I output just a single '1' to indicate prime, it would be much more difficult to tell if it was or not. I leave it up as an exercise to the reader to remember what was entered for the input number. :-)

    [[ I wanted to output a character like '*', but then I would have needed to enclose that in quotes thereby making the answer longer. As would adding my own carriage returns...]]

    zmerch

    Posted 2015-09-11T14:06:29.040

    Reputation: 541

    0

    VBA, 160 bytes

    For casuals only. Is MsgBox a STDOUT equivalent?

    Sub p(n)
    Dim t
    For i = 2 To n - 1
    If n / i = Int(n / i) Then t = True
    Next
    If t <> True Then
    MsgBox n & " is truthy"
    Else
    MsgBox n & " is falsy"
    End If
    End Sub
    

    user3819867

    Posted 2015-09-11T14:06:29.040

    Reputation: 439

    1Your code can be significantly improved to Sub p() For j=2To n+1 t=IIf(n Mod j=0Or n<2,"falsy","truthy") Exit For Next Debug.? IIf(n=2,"truthy", t) End Sub 112 bytes – Anastasiya-Romanova 秀 – 2016-08-21T12:50:39.597

    0

    Actionscript 3, 504 bytes

    There are 265 bytes for the actual code. Nearly half the bytes come from the required xml file used by Adobe Debug Launcher to run the swf.

    This is run by executing adl P-app.xml -- n to test n, followed by echo %errorlevel% to see the result.

    <?xml version="1.0" encoding="UTF-8"?>
    <application xmlns="http://ns.adobe.com/air/application/18.0"><id>P</id><filename>P</filename><versionNumber>1.0</versionNumber><initialWindow><content>P.swf</content></initialWindow></application>
    
    package{import flash.desktop.*;import flash.display.*;public class P extends Sprite{public function P(){var n=NativeApplication.nativeApplication;n.addEventListener('invoke',function(e){var p=e.arguments[0],i=2,c=p==1?0:1;while(c>0)c*=p%++i;n.exit(int(p==i));});}}}
    

    Brian

    Posted 2015-09-11T14:06:29.040

    Reputation: 231

    0

    C++, 96 bytes

    main(int c,char**a){int n=atoi(a[1]);for(c=2;c<n;c++){if(n%c==0){puts("0");return;}}puts("1");}
    

    It the same method as the C program using trial division, it just reads the value in as command line arguement.

    HSchmale

    Posted 2015-09-11T14:06:29.040

    Reputation: 181

    0

    Yorick, 20 characters

    is_prime(read(,"i"))
    

    read(,"i") reads an integer from STDIN and is_prime is the builtin prime tester of Yorick. Outputs 1 if prime and 0 otherwise.

    plannapus

    Posted 2015-09-11T14:06:29.040

    Reputation: 8 610

    0

    VBA Excel - 53 bytes

    Golfed, to run in the immediate window :

    t=false:for i=2 to j-1:t=(t Or j mod i=0):next:?not t
    

    Performs a simple check for all values from 2 to j (the input)-1. If every division has a non-0 modulo, then it returns true (prime), otherwise it returns false (non-prime or error).

    If you want to test this here is a larger code snippet that provides an inputbox for j:

    j=inputbox(a):t=false:for i=2 to j-1:t=(t or j mod i=0):next:?not t
    

    And for people that need it in a sub/function (ungolfed) :

    function isprime(byval j as integer)  ' sanitize input to an integer.
      t=false                             ' set default return to false.
      for i=2 to j                        ' loop starting at 2 (so 1 or less is not prime).
        t=(t or j mod i=0)                ' check if i is a divisor of j
      next                                '      and add to existing return
      isprime=not t                       ' if any i is a divisor then the return is false
    end function                          '      so return the opposite (isprime)
    

    Shazback

    Posted 2015-09-11T14:06:29.040

    Reputation: 51

    By the rules, if you have an input method you have to incorporate it. Also, little bug, you're returning TRUE for 1. In the spirit of your approach, I suggest turning the test around (and saving a couple of bytes): j=inputbox(7):t=j>1:for i=2 to j\2:t=t and 0<j mod i:next:?t – Joffan – 2016-06-24T05:42:04.160

    0

    Hassium, 140 Bytes

    use Math;func main(){n=Convert.toNumber(input());b=Math.floor(Math.sqrt(n));for(i=2;i<=b;++i)if(n%i==0){println("0");exit(0);}println("1");}
    

    Lengthy:

    use Math;
    func main () {
            n = Convert.toNumber(input());
            b = Math.floor(Math.sqrt(n));
            for (i = 2; i<= b; ++i)
                    if (n % i == 0) {
                            println("0");
                            exit(0);
                    }
            println("1");
    }
    

    Jacob Misirian

    Posted 2015-09-11T14:06:29.040

    Reputation: 737

    0

    Burlesque, 7 bytes

    blsq ) 71fC
    {71}
    blsq ) 71fCL[1==
    1
    blsq ) 72fC
    {2 2 2 3 3}
    blsq ) 72fCL[1==
    0
    

    fC does a prime factorization. If the length of the resulting block is 1, then we have a prime. Note that this isn't as fast as a Miller-Rabin or similar obviously :p. With the next planned release this will be two bytes.

    mroman

    Posted 2015-09-11T14:06:29.040

    Reputation: 1 382

    0

    AniRad, 30 29 bytes

    Probably not the shortest program:

     #
     W
     I
     P
     v
     >~1=
     ~
     0
     =
    

    Another possible program is, 30 bytes:

    #WIP>v~0=
         ~
         1
         =
    

    Information:

    • # = Starting point, pointer direction is to the right
    • W = User input, directly adds this to the stack
    • I = If-statement, or is equal to
    • P = If the stack is prime
    • v = If false, assign the pointer direction to the right
    • > = If true, assign the pointer direction to the bottom
    • ~ = Reset the stack
    • 0 / 1 = Assign the stack to 0 / 1
    • = = Print the result of the stack

    The following paths are executed in this program:

    • If W = prime, path = WIP~1=
    • If W != prime, path = WIP~0=

    This is a 2D esolang I created, which looks a bit like a magic square. This is a programming language in it's early stage, so a lot of functions aren't implemented yet. Also, a lot of bugs might occur, if you find a bug, please report it.

    Adnan

    Posted 2015-09-11T14:06:29.040

    Reputation: 41 965

    0

    JavaScript function golf, 16 13 bytes

    p(pr(iai()));
    

    function golf has just got a primality test function, that you can try online in the console on the language page! The primality test function took me 3 commits on GitHub.

    Returns 1 if prime, else 0.

    The variant with the alert costs us 15 bytes.

    p2a(pr(iai()));
    

    And... the first ever explanation of a function golf program!

    p(pr(iai()));
    p( prints
      pr( if
         iai() an integer input into a prompt
              ) is prime
               ); into the console.
    

    user48538

    Posted 2015-09-11T14:06:29.040

    Reputation: 1 478

    So, why does this have a negative score? – cat – 2016-03-20T02:18:33.203

    @cat I assume it's because people think it's a function. The challenge calls for a complete program and this has function in the name. It looks like a full program to me though. – DanTheMan – 2016-06-23T22:40:34.263

    0

    Gogh, 1 byte

    e
    

    Run using:

    $ ./gogh oi "e" <input>
    

    The operator e pushes the primality of the TOS. Implicit input and output.

    Zach Gates

    Posted 2015-09-11T14:06:29.040

    Reputation: 6 152

    0

    Pylongolf2, 4 bytes

    cnz~
    

    Explanations:

    cnz~
    c    read input
     n   convert to integer
      z  primality test (beta8+)
       ~ print it.
    

    user47018

    Posted 2015-09-11T14:06:29.040

    Reputation:

    0

    Racket, 31 bytes

    (require math)(λ(n)(prime? n))
    

    A standard library has a function for it. If we want to keep the import but write our own, we could do this for 51 bytes:

    (require math)(λ(n)(equal?(list 1 n)(divisors n)))
    

    On the other hand, if builtins were disallowed, for 64 55 bytes, thanks to @MatthewButterick, we can write our own:

    (λ(n)(=(count(λ(i)(=(modulo n(+ i 1))0))(range n))2))
    

    Old one, for 64:

    (λ(n)(equal?'(1)(filter(λ(i)(zero?(modulo n i)))(range 1 n))))
    

    cat

    Posted 2015-09-11T14:06:29.040

    Reputation: 4 989

    55 bytes: (λ(n)(=(count(λ(i)(=(modulo n(+ i 1))0))(range n))2)) – Matthew Butterick – 2016-04-11T05:49:09.180

    @MatthewButterick Nice! I'll add that. – cat – 2016-04-11T10:47:44.753

    @MatthewButterick from the docunentation, it seens like range needs two args, but I guess I was very tired when I was reading it. – cat – 2016-04-11T10:57:13.177

    Also, (λ(n)(prime? n)) is the same as prime?, so if (require math)(λ(n)(prime? n)) is an acceptable 31-byte answer, then (require math)prime? ought to be an acceptable 20-byte answer – Matthew Butterick – 2016-04-11T17:29:43.613

    @MatthewButterick I was *very* tired when I edited that in last night and I swore I read your name correctly... sorry about that :P – cat – 2016-04-11T19:20:14.283

    @MatthewButterick In fact, I think I fell asleep immediately after clicking "Save Edits". Also, no, just importing a stdlib function isn't usually enough, it needs to have a wrapper implementation around it. – cat – 2016-04-11T19:21:11.470

    0

    UGL, 26 bytes

    id?u:cuul^^/_c=?_=ocd$:_u:
    

    Try it online!

    How it works:

    id?u:cuul^^/_c=?_=ocd$:_u:
    id?u:                       int i = input(); if i==1: i=0;
         cuu                    int test = 2;
            l              _u:  while test != -1:
             ^^/_c=?                if i % test == 0:
                    _=o                 print(i==test);
                       cd$              test = -1;
    

    Leaky Nun

    Posted 2015-09-11T14:06:29.040

    Reputation: 45 011

    0

    Sage, 22 bytes

    lambda N:N in Primes()
    

    Try it online

    Mego

    Posted 2015-09-11T14:06:29.040

    Reputation: 32 998

    (Super old but...) Submissions have to be full programs. – totallyhuman – 2017-09-02T02:17:59.293

    0

    reticular, 6 bytes

    in@pp;
    

    A four-byte function:

    [@p]
    

    Explanation

    in@pp;
    i       take input
     n      convert to number
      @p    check for primality
        p   print
         ;  terminate
    

    Conor O'Brien

    Posted 2015-09-11T14:06:29.040

    Reputation: 36 228

    (non-competing) language is newer than the challenge. – mbomb007 – 2016-09-22T19:39:32.070

    @mbomb007 is that really necessary? This is a catalog. – Conor O'Brien – 2016-09-22T20:24:02.913

    Oh, idk. I guess not then – mbomb007 – 2016-09-22T20:29:52.223

    0

    R, 19 Bytes

     ->n;sum(!n%%1:n)==2
    

    Starts with a right assign to n. i.e. precede the code with the desired n. then it merely checks primeness of n. Prints TRUE or FALSE implicitly

    If you don't like right assign as an input method then for 24 bytes you get:

    sum(!(n=scan())%%1:n)==2
    or
    n=scan();sum(!n%%1:n)==2
    

    Which assigns within the operator.

    If you want explicit answer printing, then for 28 bytes:

     p=function(n)sum(!n%%1:n)==2
    

    user5957401

    Posted 2015-09-11T14:06:29.040

    Reputation: 699

    0

    RProgN, 102 Bytes

     'i' asoc true i 1 - ] 1 > while [ 'a' asoc i a / i a // = if [ [ false else [ end a 1 - ] 1 > end [ [
    

    Explanation

     'i' asoc           # Associate the implicit input with i
    true i              # Push true to the stack, push i to the stack
    1 -                 # Subtact 1 from the top of the stack
    ] 1 >               # Duplicate the top value of the stack, compare that it's larger than 1
    while               # While the top of the stack contains a truthy value
        [               # Pop the top of the stack (The conditional in this case)
        'a' asoc        # Associate the top of the stack with a
        i a /           # Push i divided by a to the top of the stack
        i a // =        # Push i integeral divided by a to the top of the stack, compare the top and underneith the top for equality
        if              # if i/a = floor(i/a), essentially
            [ [ false   # Pop the conditional, and the 'true' we slipped in earlier, push false in in place of it
        else            #
            [           # Pop the conditional anyway
        end             #
        a 1 - ] 1 >     # Subtract 1 from a, duplicate it, and compare that it's larger than 1
    end                 #
    [ [                 # Pop the conditional, the value of a, which leaves only the bottom value we previously inserted, which is implicitly printed
    

    RProgN doesn't have any method of input as of yet, as such, Any input needs to be written in pure form at the very start of the code, such that 23 'i' asoc .... An Extra byte is added to the score, because a space is required in front of any command.

    ATaco

    Posted 2015-09-11T14:06:29.040

    Reputation: 7 898