-3
\$\begingroup\$

This question already has an answer here:

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.

\$\endgroup\$

marked as duplicate by Peter Taylor, Kyle Kanos, William Barbosa, isaacg, user16402 Jul 17 '14 at 19:06

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 3
    \$\begingroup\$ You 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]? \$\endgroup\$ – ɐɔıʇǝɥʇuʎs Jul 17 '14 at 12:42
  • 1
    \$\begingroup\$ this is tagged as c, so please write in the question that it should be answered in c. or remove the tag. whatever you choose \$\endgroup\$ – proud haskeller Jul 17 '14 at 12:43
  • 2
    \$\begingroup\$ I 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. \$\endgroup\$ – xnor Jul 17 '14 at 13:11
  • 2
    \$\begingroup\$ Why the language restriction? \$\endgroup\$ – Kyle Kanos Jul 17 '14 at 14:46
  • 4
    \$\begingroup\$ @user2992539: Isn't half the point of this site to see what kind of language-specific tricks one can do? \$\endgroup\$ – Kyle Kanos Jul 17 '14 at 14:54
4
\$\begingroup\$

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)
\$\endgroup\$
  • \$\begingroup\$ Question is tagged as C, which doesn't have built-in large integers. However nice try! \$\endgroup\$ – Somnium Jul 17 '14 at 12:39
  • 2
    \$\begingroup\$ @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. \$\endgroup\$ – Calvin's Hobbies Jul 17 '14 at 12:42
  • \$\begingroup\$ @Calvin'sHobbies ok. \$\endgroup\$ – Somnium Jul 17 '14 at 12:43
  • \$\begingroup\$ I changed score for non-32-bit types from 2 to 10. \$\endgroup\$ – Somnium Jul 20 '14 at 20:12
1
\$\begingroup\$

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));
}
\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.