Determine if a number is prime without using arithmetic

12

1

Write a program which determines if a given number is prime.

The catch: No digits or arithmetical operators. This means:

  1. No digits from 0 to 9, anywhere in the program, and whatever the meaning
  2. No using any built-in operators for addition (and incrementing), subtraction (and decrementing), multiplication, division, exponentiation, or modulus, anywhere in the program
  3. This includes symbols like + and functions like num.add()
  4. All other operations are permitted
  5. You may define your own functions for the forbidden operations
  6. You may use symbols like - when they mean things other than the forbidden operations

Shortest code wins - any language permitted.

Example solution

An ungolfed solution in Java can be found here.

Ypnypn

Posted 2014-04-07T20:41:42.440

Reputation: 10 485

Question was closed 2014-04-09T11:48:24.973

It would be greatly appreciated if someone would explain the downvotes. – Ypnypn – 2014-04-07T20:47:35.310

4Probably because this is impossible? (Without abusing built-in prime functions, that is.) – Doorknob – 2014-04-07T20:48:16.763

2

Even if not impossible, it is so exclusive of the established techniques for prime detection to be kind of silly ... Feel free to post to Sandbox first to get a feel for how things will turn out before you post.

– ProgrammerDan – 2014-04-07T20:51:11.527

1@ProgrammerDan I added a solution using a typical prime-detection method. – Ypnypn – 2014-04-07T21:35:49.540

2

Quite similar to Is it a prime? w/o math?

– daniero – 2014-04-08T00:19:01.757

@Ypnypn you should submit your solution as an answer :). – ProgrammerDan – 2014-04-08T00:31:37.790

1In light of algorithmshark's solution; Should point two be updated to prohibit built in primality tests? – Taemyr – 2014-04-08T08:26:38.210

1Any constraints on performance or size of the primes? i.e. does it matter if the program only supports 31 bit primes or has exponential runtime? – CodesInChaos – 2014-04-08T10:41:39.683

it is no sense because even if in any programming language it is not used arithmetic, basically any computer will use arithmetic to make any "for" of "if". – albanx – 2014-04-08T12:05:36.630

Since we may not use any numbers, are we allowed to use length functions (like ''.length, 'a'.length, 'aa'.length,'aaa'.length for 0 up to 3, for example)? – Nzall – 2014-04-08T12:12:18.100

@Nate yes; that's okay. – Ypnypn – 2014-04-08T13:19:39.943

Does the increment operator also count as addition? – celtschk – 2014-04-08T21:24:08.383

Answers

25

PHP, 117 characters

This one is based on regular expressions. If a string containing $n characters can be split into chunks of equal length, then $n must be composite.

(Assumes $n is a positive integer.)

function p($n){$u=true;$s=str_repeat('x',$n);return($s!='x'&&!preg_match('/^(xx{'.$u.',})\\'.$u.'{'.$u.',}$/U',$s));}

Output:

for ($i=1; $i<=100; $i++) if (p($i)) echo "$i ";

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 

r3mainer

Posted 2014-04-07T20:41:42.440

Reputation: 19 135

5I believe this is the epitome of "two problems." +1 – wchargin – 2014-04-08T04:13:40.010

15

Sage, 18

Looks like this is allowed.

input()in Primes()

3 gives True, 4 gives False

user12205

Posted 2014-04-07T20:41:42.440

Reputation: 8 752

10

PERL 53 54 52 48

('a'x($_=pop))=~/^a?$|^(?<a>aa+?)\g{a}+$/||print

Thanks to reddit: http://www.reddit.com/r/programming/comments/crmhe/regular_expression_to_check_for_prime_numbers_1111/

ilmale

Posted 2014-04-07T20:41:42.440

Reputation: 1 147

Interesting. However, from the question: "1. No digits from 0 to 9, anywhere in the program, and whatever the meaning." The variable can be read in a shorter way: $a=pop; – Heiko Oberdiek – 2014-04-08T00:48:25.130

Thanks, any character will do, i used 'a'.. but I need the \1 for the capture. :-S – ilmale – 2014-04-08T00:52:27.443

A named capture helps: ('a'x($_=pop))=~/^a?$|^(?<a>aa+?)\g{a}+$/||print (48 bytes) – Heiko Oberdiek – 2014-04-08T00:57:50.947

Thanks you, I was looking for a short way to use name reference. Also I didn't know the empty print trick. :D – ilmale – 2014-04-08T01:03:16.800

use of + not allowed by rule 3 – SeanC – 2014-04-08T18:59:08.420

1@SeanCheshire Rule 3 only seems to disallow using + as an arithmetic operator, not as a regex symbol. This is in contrast to rule 1, which expressly forbids any use of digits in any context. – Venge – 2014-04-08T22:44:57.933

7

J - 7 char

Trivial.

(p:~=~)

This verb is in the form of a hook. Applied to an argument y, it will evaluate as follows. (Feel free to check for yourself: Hook, Reflex/Passive, Equals, Primes.)

(p:~=~) y          NB. (F G) y  becomes  y F (G y)   (Hook)
y p:~ (=~ y)       NB. x F~ y   becomes  y F x       (Passive)
(=~ y) p: y        NB. F~ y     becomes  y F y       (Reflex)
(y = y) p: y       NB. anything is equal to itself, so y=y becomes 1
1 p: y             NB. 1 p: y tests for primality of y

Usage:

   (p:~=~) 5
1
   (p:~=~) 6
0
   (p:~=~) 8675309
1
   NB. test every element of a list
   (p:~=~)every 1627 5231 7610 6311 4549 6990 4220 9028 4066 3496
1 1 0 1 1 0 0 0 0 0

algorithmshark

Posted 2014-04-07T20:41:42.440

Reputation: 8 144

6I feel that using a built in primality tester goes against the spirit of the challenge. – Taemyr – 2014-04-08T08:27:15.463

@Taemyr (*@{.=*./)@(+.}.@i.) for 20 char. +. is GCD, *. is LCM, * is Signum. But until the specs change, the 7 char solution is the shortest valid solution.

– algorithmshark – 2014-04-08T19:36:11.330

5

Python 3, 85

(lambda i:not any(' '*i==(' '*u)*v for u in range(i)for v in range(i)))(int(input()))

Where * is not a multiplication operator but a string multiple self-concatenation operator.

Evpok

Posted 2014-04-07T20:41:42.440

Reputation: 558

4

Ruby, 30

require'prime';f=->n{n.prime?}

Declares a function that returns whether its argument is prime.

Alternative version (same length):

require'prime';f=proc &:prime?

Obviously bending the rules, but you never said built-in prime functions aren't allowed.

Doorknob

Posted 2014-04-07T20:41:42.440

Reputation: 68 138

He shouldn't have to say it: http://meta.codegolf.stackexchange.com/a/1078/1416

– corsiKa – 2014-04-08T19:40:03.903

3

PHP - 208

<?php
function i($n){if($n==strlen(" "))return!!"";foreach(range(strlen("  "),$n) as$_){if($_!=$n){$a=array_fill(!!"",$n,"");while(count($a)>=$_)array_splice($a,!!"",$_);if(!count($a))return!!"";}}return!"";}

Type juggling in PHP is fun!

TimWolla

Posted 2014-04-07T20:41:42.440

Reputation: 1 878