Hot answers tagged

27

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} = \frac1{q_1-1}+\frac1{q_2-1}\$. It is known (and can be quickly checked) that the positive integer solutions to \$\frac1n = \frac1x + \frac1y\$ are in one-to-one ...


23

JavaScript (V8), 54 bytes A full program that prints cuban primes forever. for(x=0;;){for(k=N=~(3/4*++x*x);N%++k;);~k||print(-N)} Try it online! NB: Unless you have infinite paper in your printer, do not attempt to run this in your browser console, where print() may have a different meaning. JavaScript (ES6),  63 61 60  59 bytes Returns the \$...


16

Regex (PCRE flavour), 66 (65🐌) bytes Inspired by seeing that both Martin Ender and jaytea, two regex geniuses, wrote regex solutions to this code golf, I wrote my own from scratch. The famous prime-checking regex does not appear anywhere in my solution. Do not read this if you don't want some unary regex magic spoiled for you. If you do want to take a ...


14

MATL, 25 24 21 bytes Q:qJyZpbB!sEq*^YpYsXG Try it at MATL online Thanks @LuisMendo for a nice golfing session in chat that ultimately led to this 21 byte version, by suggesting Eq*^ Explanation Q:q % Push 0:n J % Push 1i for later use. y % Duplicate 0:n from below Zp % Vector result of isprime() b % Bubble 0:n from bottom of stack B!s % Sum of ...


13

JavaScript (ES6), 53 bytes n=>(g=(o,d=N=n+o)=>N%--d?g(o,d):d-1?g(o<0?-o:~o):N)`` Try it online! Commented n => ( // n = input g = ( // g = recursive function taking: o, // o = offset d = // d = current divisor, initialized to N N = n + o // N = input + offset ) => ...


13

Trial division: score 59407, 6243 layers, 16478 neurons in total Given as a Python program which generates and validates the net. See the comments in trial_division for an explanation of how it works. The validation is quite slow (as in, running time measured in hours): I recommend using PyPy or Cython. All layers use ReLU (\$\alpha \to \max(0, \alpha)\$) ...


13

Score 263 385 425 426 with only primes < 1.000.000 (was: non-competitive, now it is; score can be increased by running the program longer) I followed the same path as Wheat Wizard: iteratively search for primes in the solution that can be replaced with a longer list of primes with the same result. I wrote that Python program that does exactly this. It ...


12

Sledgehammer 0.4, 22 20 bytes ⢂⡐⠥⡄⠡⢒⣩⣀⣼⡝⢄⡎⣛⠅⡉⣱⡆⢀⡠⣽ Decompresses into this Wolfram Language function: ListPlot[AnglePath[Array[If[PrimeQ@#, ArcSin[(-1)^ThueMorse@#], 0] &, #]]] Ungolfed First we define a function that returns the angle to turn at each step: If[PrimeQ[#], ArcSin[(-1)^ThueMorse@#], 0 ]& ThueMorse is the parity of the sum ...


11

Score 32 34 36 {5, 7, 11, 13, 17, 23, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 101, 113, 131, 137, 151, 211, 229, 241, 281, 313, 379, 401, 433, 457, 491, 521, 571, 601, 25117, 293362609} This is an improvement of Arnauld's answer. I just noticed that \$ \dfrac{1}{19-1}=\dfrac{1}{73-1}+\dfrac{1}{61-1}+\dfrac{1}{41-1} \$ But 41 and 61 were already used in ...


10

05AB1E, 5 bytes Åps.x Try it online! or as a Test Suite Inefficient for big numbers


10

JavaScript (ES6),  45  44 bytes Takes input as (n)(p1), where \$n\$ is 0-indexed. n=>g=(p,d=2)=>n?~p%d?g(p,d+1):--n?g(p*d):d:p Try it online! Commented n => // n = 0-based index of the requested term g = ( // g is a recursive function taking: p, // p = current prime product d = 2 ...


9

Gaia, 3 bytes ṅD⌡ Try it online! Rather slow for large inputs, but works given enough memory/time. I'm not sure why D⌡ implicitly pushes z again, but it makes this a remarkably short answer! ṅ | implicit input z: push first z prime numbers, call it P D⌡ | take the absolute difference between P and (implicit) z, | returning the smallest value ...


9

05AB1E, 6 bytes This produces and infinite output stream. λλP>fW Try it online! (link includes a slightly modified version, λ£λP>fW, which instead outputs the first \$n\$ terms) Explanation Very straightforward. Given \$p_1\$ and \$n\$, the program does the following: Starts with \$p_1\$ as an initial parameter for the infinite stream (which is ...


8

Wolfram Language (Mathematica), 98 96 91 77 76 63 bytes ListPlot@AnglePath@Array[Pi/2If[PrimeQ@#,2ThueMorse@#-1,0]&,#]& -14 bytes: Thanks to @lirtosiast for showing me how to use AnglePath... -13 bytes: ...and ThueMorse! usage example: %[20000] Step-by-step explanation: If[PrimeQ@#, 2 ThueMorse@# - 1, 0] & is a function that takes the step ...


8

C (gcc), 179 bytes o;i;d;k;h;f(n,p)char*p;{h=2*n+1;memset(p,0,h*h);p+=h--*n+n;*p=1;for(d=k=0;k++<n;){for(i=1;k%++i%k;);for(o=k;o/2;o=o/2^o&1);i==k?d+=o*2+3:0;p+=(d%2*h+1)*((d&2)-1);*p=1;}return++h;} Try it online! A function. First argument is N, second argument is an allocated buffer of size at least \$4 n^2 + 4 n + 1\$ bytes. A square image ...


8

Python 3,262 236 209 196 179 114 93 92 97 bytes def f(n): m=k=1;s='' while n:m*=k*k;k+=1;n-=m%k while k:s=chr(~-k%26+97)+s;k=~-k//26 return s Try it online! Fixed a bug mentioned by @benrg about getting a wrong output for the input 123. Thanks to: - @AdmBorkBork for help me getting started and save a few bytes - @Sriotchilism O'Zaic for saving me 6 ...


7

Regex (ECMAScript), 276 205 201 193 189 bytes Comparing the multiplicities (exponents) of different prime factors is an interesting problem for solving with ECMAScript regex – the lack of backreferences that persist through iterations of a loop makes it a challenge to count anything. Even if counting the numerical trait in question is possible, often a more ...


7

Octave, 40 bytes @(n)p([~,k]=min(abs(n-(p=primes(2*n))))) Try it online! This uses the fact that there is always a prime between n and 2*n (Bertrand–Chebyshev theorem). How it works @(n)p([~,k]=min(abs(n-(p=primes(2*n))))) @(n) % Define anonymous function with input n p=primes(2*n) % ...


7

Score 984314, 82027 layers, 246076 neurons in total We can keep things entirely in the integers if we use activation function ReLU, which simplifies the analysis. Given an input \$x\$ which is known to be an integer, we can test whether \$x = a\$ with two layers and three neurons: First layer: outputs \$\textrm{ge}_a = (x - a)^+\$ and \$\textrm{le}_a = (-...


7

Neim, 3 bytes ᚺ>: Explanation: ᚺ halve > increment : previous prime Try it online!


7

05AB1E, 16 12 9 bytes Generates an infinite list. Saved 4 bytes with Kevin Cruijssen's port of Arnaulds formula. Saved another 3 bytes thanks to Grimy ∞n3*4÷>ʒp Try it online! Explanation ∞ # on the list of infinite positive integers n3*4÷> # calculate (3*N^2)//4+1 for each ʒp # and filter to only keep primes


7

R, 75 73 bytes n=scan() while(F<n)F=F+any(!(((T<-T+1)*1:4-1)/3)^.5%%1)*all(T%%(3:T-1)) T Try it online! -2 bytes by noticing that I can remove brackets if I use * instead of & (different precedence). Outputs the nth Cuban prime (1-indexed). It uses the fact (given in OEIS) that Cuban primes are of the form \$p=1+3n^2\$ or \$4p=1+3n^2\$ for ...


7

MATL, 31 30 28 26 bytes J4:^0i:Zpl_G:B!s^*hYs)YsXG 3 Bytes saved thanks to @LuisMendo 2 Bytes saved thanks to @Sanchises Try it at MATL Online Explanation This solution uses complex numbers to represent the X and Y components of the 2D plane J % Push the literal complex number 0 + 1j to the stack 4: % Create the array [1, 2, 3, 4] ^ % ...


7

Score 22 Just to get the ball rolling. {5,7,11,13,17,19,23,31,37,41,43,47,53,59,61,67,71,79,137,491,25117,293362609} Compute the fraction I suspect that the sequence can be made arbitrary large, but my code is currently too messy and inefficient for anything significantly better than that.


6

Japt, 5 bytes _j}cU Try it or run all test cases _j}cU :Implicit input of integer U _ :Function taking an integer as an argument j : Test if integer is prime } :End function cU :Return the first integer in [U,U-1,U+1,U-2,...] that returns true


6

J, 15 bytes -10 bytes thanks to miles! Returning the sequence up to n (zero-indexed) – thanks to @miles (,0({q:)1+*/)^: Try it online! J, 25 bytes Returns the n th item _2{((],0{[:q:1+*/@])^:[]) Try it online!


5

Brachylog, 18 17 15 16 15 bytes ℕ₁<l4&≜sᶠ{Ḋ|ṗ}ᵐ Try it online! -1 byte after a discussion with Fatalize inspired me to just see what happens if I swap the l and the < around. This predicate generates the output through the input variable, so long as the output variable is left unconstrained. Since duplicates are allowed, each number is ...


5

05AB1E, 4 bytes z-Ån Try it online!


5

Brachylog, 7 5 bytes ;I≜-ṗ Try it online! Saved 2 bytes thanks to @DLosc. Explanation ;I≜ Label an unknown integer I (tries 0, then 1, then -1, then 2, etc.) - Subtract I from the input ṗ The result must be prime


5

Wolfram Language (Mathematica), 31 bytes Nearest[Prime~Array~78499,#,1]& Try it online! & (*pure function*) Prime~Array~78499 (*among the (ascending) first 78499 primes*) 1 (*select one*) Nearest[ ,#, ] (*which is nearest to the argument*) 1000003 is the ...


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