Hot answers tagged

253 votes

Is this number a prime?

hello, world!, 13 hello, world!
user avatar
  • 21.8k
169 votes

Is this number a prime?

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

Calculate the number of primes up to n

C, 0.026119s (Mar 12 2016) ...
user avatar
  • 206k
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: ...
user avatar
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. ...
user avatar
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 ...
user avatar
70 votes

Is this number a prime?

Pyth, 4 bytes }QPQ Prints True or False.
user avatar
  • 38.6k
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 ...
user avatar
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 ...
user avatar
  • 206k
52 votes

Is this number a prime?

CJam, 4 bytes qimp CJam has a built-in operator for primality testing.
user avatar
  • 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. ...
user avatar
  • 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 \$\...
user avatar
  • 11.7k
49 votes

Is this number a prime?

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

Compute the Carmichael function

Mathematica, 16 bytes CarmichaelLambda Well...
user avatar
46 votes

Calculate Phi (not Pi)

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

List of primes under a million

Python 3, 45 bytes k=P=1 while k<1e6:P%k>0==print(k);P*=k*k;k+=1 Try it online! By the time the loop reaches testing k, ...
user avatar
  • 137k
39 votes

Is this number a prime?

Mornington Crescent, 2448 bytes We're back in London! ...
user avatar
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 ...
user avatar
  • 206k
37 votes
Accepted

Prime counting function

05AB1E, 3 bytes !fg This assumes that factorization built-ins are allowed. Try it online! How it works ...
user avatar
  • 206k
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 ...
user avatar
  • 16.8k
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 ...
user avatar
  • 35.8k
34 votes

A prime test that's LITERALLY prime

hello, world!, 13 bytes, 1193 hello, world!
user avatar
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 ...
user avatar
  • 206k
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} = \...
user avatar
  • 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 ...
user avatar
  • 35.2k
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 ...
user avatar
  • 60.9k
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
user avatar
  • 65.5k
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 ...
user avatar
  • 206k
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 ...
user avatar
  • 206k

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