Hot answers tagged

255 votes

Is this number a prime?

hello, world!, 13 hello, world!
  • 21.9k
169 votes

Is this number a prime?

Hexagony, 29 bytes .?'.).@@/'/.!.>+=(<.!)}($>(<% The readable version of this code is: ...
  • 3,351
135 votes

Calculate the number of primes up to n

C, 0.026119s (Mar 12 2016) ...
  • 207k
100 votes
Accepted

Find the Smoothest Number

CJam - 13 q~,>{mfW=}$0= Try it at http://cjam.aditsu.net/ Example input: 2001 2014 Example output: ...
74 votes

Is this number a prime?

Hexagony, 218 92 58 55 bytes Notice: This answer has been solidly beaten with a side-length 4 solution by Etoplay. ...
72 votes

Find the Smoothest Number

Regex (.NET PCRE flavour), 183 129 bytes Don't try this at home! This is not really a contender for the win. But Eric Tressler suggested solving this problem with nothing but a regex, and I couldn't ...
70 votes

Is this number a prime?

Pyth, 4 bytes }QPQ Prints True or False.
  • 38.8k
65 votes

Is this number a prime?

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 ...
65 votes

Coprimes up to N

Python 2, 61 47 bytes lambda n:[k/n for k in range(n*n)if k/n*k%n==1] Try it online! Background Consider the ring \$(Z_n, +_n, \cdot_n)\$. While this ring is ...
  • 207k
51 votes

Is this number a prime?

CJam, 4 bytes qimp CJam has a built-in operator for primality testing.
  • 42.8k
51 votes

Is this number a prime?

Hexagony, 28 bytes Since Etoplay absolutely trounced me on this question, I felt that I had to outgolf his only other answer. ...
  • 11.4k
50 votes
Accepted

Is it almost-prime?

Sagemath, 2 bytes GF Outputs via exception. Try it online! The Sagemath builtin \$\text{GF}\$ creates a Galois Field of order \$n\$. However, remember that \$\...
  • 12.9k
49 votes

Is this number a prime?

Help, WarDoq!, 1 byte P Outputs 1 if the input is prime, 0 otherwise.
  • 38.8k
49 votes

Compute the Carmichael function

Mathematica, 16 bytes CarmichaelLambda Well...
46 votes

Calculate Phi (not Pi)

Regex (.NET), 122 113 bytes ^(?=((?=.*$(?<=^(\3+(.+.))(.*?(?>(.\4)?)))).)+(.*))((?=.*(?=\6$)(?<=(?!(.+.)\8*(?=\6$)(?<=^\8+))(.+?(?>\9?)))).)+ ...
39 votes
Accepted

Prime counting function

05AB1E, 3 bytes !fg This assumes that factorization built-ins are allowed. Try it online! How it works ...
  • 207k
38 votes

Is this number a prime?

Mornington Crescent, 2448 bytes We're back in London! ...
38 votes
Accepted

Determine whether a number is 2017-friable without primes in your source code

Jelly, 8 bytes 44‘²!*ḍ@ Try it online! Note that test cases 11111 and above are a bit too much for TIO. Verification ...
  • 207k
37 votes

Primality testing formula

43 bytes (4**n+1)**n%4**n**2/n&2**(2*n*n+n)/-~2**n<1 Try it online! The method is similar to Dennis' second (deleted) answer, but this answer is easier ...
  • 17k
36 votes
Accepted

Prime Nerd Sniping Pattern

CJam, score 276496.9062958626 2,128*_(]128* It turns out to be optimal, because: There aren't any non-orthogonal pairs with distance 2. So the distance must be ...
  • 36.2k
34 votes

A prime test that's LITERALLY prime

hello, world!, 13 bytes, 1193 hello, world!
34 votes
Accepted

Output the Trinity Hall Prime

Jelly, 74 71 69 68 66 bytes “©ạ-3ṗÇñ"ỤḍV8żṢ?ḤsMVE[,Ṃƭ"ḞÇsẇʂ(ụFsẠʂẆŀṣ’ḃ19ĖŒṙị⁾81s30m0Z062 ȷ446‘ Try it online! How it works The literal ...
  • 207k
30 votes
Accepted

Longest Prime Sums

Score 100 8605 I used an algorithm that starts with one solution and repeatedly tries to split a prime \$p\$ in the solution into two other primes \$q_ 1\$ and \$q_ 2\$ that satisfy \$\frac1{p-1} = \...
  • 14.9k
28 votes

Is this number a prime?

Brachylog (V2), 1 byte Try it online! Brachylog (V1), 2 bytes #p This uses the built-in predicate #p - Prime, which ...
  • 36.8k
28 votes

Is this number a prime?

Labyrinth, 29 bytes 1 ? : } +{%!@ (:'( } { :** Reads an integer from STDIN and outputs ((n-1)!)^2 mod n. Wilson's theorem ...
  • 61.1k
28 votes

Is this number a prime?

Haskell, 49 bytes Using xnor's Corollary to Wilson's Theorem: main=do n<-readLn;print$mod(product[1..n-1]^2)n>0
  • 66.2k
28 votes
Accepted

Calculate Phi (not Pi)

GS2, 12 10 bytes V@'◄l.1&‼l The source code uses the CP437 encoding. Try it online! Test run ...
  • 207k
28 votes

Compute the Carmichael function

Python, 76 73 67 bytes f=lambda n,k=1:1-any(a**-~k*~-a**k%n for a in range(n))or-~f(n,k+1) Try it online! A further byte could be saved by returning True instead ...
  • 207k
26 votes

Approximate Brun's Constant

Python 3, 78 77 75 70 68 62 bytes f=lambda n,k=3,m=1,j=0:k<n and-m%k*j*2/k+f(n,k+2,m*k**4,m%k/k) Thanks to @xnor for golfing off 2 4 bytes and paving the way ...
  • 207k

Only top scored, non community-wiki answers of a minimum length are eligible