Shortest test for primality for byte

-3

Write a function which gets as input a number (0 <= n <= 255) stored in 32-bit variable and returns non-zero integer if n is prime number and 0 otherwise.
Considern = 0 and n = 1 as non-primes.

Arrays and pointers are not allowed.

Score:
1 - for | & ^ ~ >> << operators
1.1 - for + - < > <= >= == != ! && || operators and branches & loops
1.2 - for * operator
2 - for / % operators
10 - for casting to/using other types (other than 32-bit integer)
Score for operators inside loops is score * max iterations count.

Answer with lowest score wins. If many answers with identical score, wins first one.

Somnium

Posted 2014-07-17T11:51:07.613

Reputation: 2 537

Question was closed 2014-07-17T19:06:00.473

3You might want to explicitly state in the rules that this is a C-specific challenge (even though people tend to dislike language specific challenges). Also: wouldn't this be [atomic-code-golf] instead of [code-challenge]? – ɐɔıʇǝɥʇuʎs – 2014-07-17T12:42:11.670

1this is tagged as c, so please write in the question that it should be answered in c. or remove the tag. whatever you choose – proud haskeller – 2014-07-17T12:43:48.660

I do like the rule that casts also cost points: this prevents "look-up tables in disguise" like my answer, which opens up competition. – ɐɔıʇǝɥʇuʎs – 2014-07-17T12:46:31.470

2I think questions like this would produced more satisfying answers if literals were restricted to numbers 0 through 9, disallowing multicharacter literals. Otherwise, you're still going to get massive lookup tables pushing the boundary of the integer size. – xnor – 2014-07-17T13:11:05.117

1Adding a new scoring system to a question which already has a ton of variants doesn't really add any value to the site. – Peter Taylor – 2014-07-17T13:14:47.587

@PeterTaylor I wanted this not to be algorithmical problem, but low-level optimisation. – Somnium – 2014-07-17T13:35:28.993

If that is the case, someone would need to compress all those prime number information into a handful of 32 bit constants soon. Hopefully it is still within the bounds of information theory. – Vectorized – 2014-07-17T13:37:29.467

@bitpwner It can be done in 4. – ɐɔıʇǝɥʇuʎs – 2014-07-17T13:40:22.013

@ɐɔıʇǝɥʇuʎs Let do it in a clever way!) – Somnium – 2014-07-17T13:44:19.277

2Why the language restriction? – Kyle Kanos – 2014-07-17T14:46:07.690

1@KyleKanos To prevent using language-specific tricks. And I like C) – Somnium – 2014-07-17T14:53:35.520

4@user2992539: Isn't half the point of this site to see what kind of language-specific tricks one can do? – Kyle Kanos – 2014-07-17T14:54:26.377

@KyleKanos Maybe... – Somnium – 2014-07-17T14:58:32.457

I'm thinking to delete my question, it's a lot more unsuccessful than I thought it will be. – Somnium – 2014-07-17T15:03:40.043

1I like the question, but making it language-specific ruins it - please reconsider. – Paul R – 2014-07-17T21:36:51.463

Answers

4

Python (4)

f = lambda n: (3622934801701902697686555901643181168549895964810913375391936802953324275884 >> n) & 1

Damn casting. No trickery here, just magic. Number generated with J: #.x:1 p: i._256. Output:

for i in enumerate(map(f,range(256))):
    ...:     print i
    ...:     
(0, 0L)
(1, 0L)
(2, 1L)
(3, 1L)
(4, 0L)
(5, 1L)
(6, 0L)
(7, 1L)
(8, 0L)
(9, 0L)
(10, 0L)
(11, 1L)
(12, 0L)
(13, 1L)
(14, 0L)
(15, 0L)
(16, 0L)
(17, 1L)
(18, 0L)
(19, 1L)
(20, 0L)
(21, 0L)
(22, 0L)
(23, 1L)
(24, 0L)
(25, 0L)
(26, 0L)
(27, 0L)
(28, 0L)
(29, 1L)
(30, 0L)
(31, 1L)
(32, 0L)
(33, 0L)
(34, 0L)
(35, 0L)
(36, 0L)
(37, 1L)
(38, 0L)
(39, 0L)
(40, 0L)
(41, 1L)
(42, 0L)
(43, 1L)
(44, 0L)
(45, 0L)
(46, 0L)
(47, 1L)
(48, 0L)
(49, 0L)
(50, 0L)
(51, 0L)
(52, 0L)
(53, 1L)
(54, 0L)
(55, 0L)
(56, 0L)
(57, 0L)
(58, 0L)
(59, 1L)
(60, 0L)
(61, 1L)
(62, 0L)
(63, 0L)
(64, 0L)
(65, 0L)
(66, 0L)
(67, 1L)
(68, 0L)
(69, 0L)
(70, 0L)
(71, 1L)
(72, 0L)
(73, 1L)
(74, 0L)
(75, 0L)
(76, 0L)
(77, 0L)
(78, 0L)
(79, 1L)
(80, 0L)
(81, 0L)
(82, 0L)
(83, 1L)
(84, 0L)
(85, 0L)
(86, 0L)
(87, 0L)
(88, 0L)
(89, 1L)
(90, 0L)
(91, 0L)
(92, 0L)
(93, 0L)
(94, 0L)
(95, 0L)
(96, 0L)
(97, 1L)
(98, 0L)
(99, 0L)
(100, 0L)
(101, 1L)
(102, 0L)
(103, 1L)
(104, 0L)
(105, 0L)
(106, 0L)
(107, 1L)
(108, 0L)
(109, 1L)
(110, 0L)
(111, 0L)
(112, 0L)
(113, 1L)
(114, 0L)
(115, 0L)
(116, 0L)
(117, 0L)
(118, 0L)
(119, 0L)
(120, 0L)
(121, 0L)
(122, 0L)
(123, 0L)
(124, 0L)
(125, 0L)
(126, 0L)
(127, 1L)
(128, 0L)
(129, 0L)
(130, 0L)
(131, 1L)
(132, 0L)
(133, 0L)
(134, 0L)
(135, 0L)
(136, 0L)
(137, 1L)
(138, 0L)
(139, 1L)
(140, 0L)
(141, 0L)
(142, 0L)
(143, 0L)
(144, 0L)
(145, 0L)
(146, 0L)
(147, 0L)
(148, 0L)
(149, 1L)
(150, 0L)
(151, 1L)
(152, 0L)
(153, 0L)
(154, 0L)
(155, 0L)
(156, 0L)
(157, 1L)
(158, 0L)
(159, 0L)
(160, 0L)
(161, 0L)
(162, 0L)
(163, 1L)
(164, 0L)
(165, 0L)
(166, 0L)
(167, 1L)
(168, 0L)
(169, 0L)
(170, 0L)
(171, 0L)
(172, 0L)
(173, 1L)
(174, 0L)
(175, 0L)
(176, 0L)
(177, 0L)
(178, 0L)
(179, 1L)
(180, 0L)
(181, 1L)
(182, 0L)
(183, 0L)
(184, 0L)
(185, 0L)
(186, 0L)
(187, 0L)
(188, 0L)
(189, 0L)
(190, 0L)
(191, 1L)
(192, 0L)
(193, 1L)
(194, 0L)
(195, 0L)
(196, 0L)
(197, 1L)
(198, 0L)
(199, 1L)
(200, 0L)
(201, 0L)
(202, 0L)
(203, 0L)
(204, 0L)
(205, 0L)
(206, 0L)
(207, 0L)
(208, 0L)
(209, 0L)
(210, 0L)
(211, 1L)
(212, 0L)
(213, 0L)
(214, 0L)
(215, 0L)
(216, 0L)
(217, 0L)
(218, 0L)
(219, 0L)
(220, 0L)
(221, 0L)
(222, 0L)
(223, 1L)
(224, 0L)
(225, 0L)
(226, 0L)
(227, 1L)
(228, 0L)
(229, 1L)
(230, 0L)
(231, 0L)
(232, 0L)
(233, 1L)
(234, 0L)
(235, 0L)
(236, 0L)
(237, 0L)
(238, 0L)
(239, 1L)
(240, 0L)
(241, 1L)
(242, 0L)
(243, 0L)
(244, 0L)
(245, 0L)
(246, 0L)
(247, 0L)
(248, 0L)
(249, 0L)
(250, 0L)
(251, 1L)
(252, 0L)
(253, 0L)
(254, 0L)
(255, 0L)

ɐɔıʇǝɥʇuʎs

Posted 2014-07-17T11:51:07.613

Reputation: 4 449

Question is tagged as C, which doesn't have built-in large integers. However nice try! – Somnium – 2014-07-17T12:39:50.610

2@user2992539 Then you should explicitly say that the answer has to be in C. I was about to post something almost exactly the same as this (still in Python) but ɐɔıʇǝɥʇuʎs beat me to it. – Calvin's Hobbies – 2014-07-17T12:42:38.830

@Calvin'sHobbies ok. – Somnium – 2014-07-17T12:43:52.273

I changed score for non-32-bit types from 2 to 10. – Somnium – 2014-07-20T20:12:20.670

1

The most obvious solution scores 32.3.

Test Ideone

int p(int v) {
  return (v > 1)
  & (v == 2 || (v % 2))
  & (v == 3 || (v % 3))
  & (v == 5 || (v % 5))
  & (v == 7 || (v % 7))
  & (v == 11 || (v % 11))
  & (v == 13 || (v % 13));
}

edc65

Posted 2014-07-17T11:51:07.613

Reputation: 31 086