New answers tagged

1

Brachylog, 12 bytes +₁;?≜^₂ᵐ⟧₂ṗⁿ Try it online! How it works Brachylog will try to find a value N that fulfills the following program: +₁;?≜^₂ᵐ⟧₂ṗⁿ +₁ N+1 ;? [N+1, N] ≜ Try possible numbers, e.g. [5, 4] ^₂ᵐ Map square [25, 16] ⟧₂ Range from min to max ṗⁿ Succeeds if there is no prime in this ...


3

Charcoal, far too slow ≔…⁰⊘Xφ³θ§≔θ¹¦⁰FθF∧ι…·ι÷Lθι§≔θ×ικ⁰IΦθι Don't try it online! Link is to verbose version of code. Takes about a quarter of a minute on TIO just to generate the primes up to half a million (I didn't try printing them out). Explanation: ≔…⁰⊘Xφ³θ Get a list of integers up to (but not including) half a billion. §≔θ¹¦⁰ 1 is not prime. Fθ ...


4

Ruby, unknown time GC.disable require 'prime' p Prime.each(499999993).to_a Try it online! (Nothing is printed within the 60 s limit.) Instead of printing each prime individually, print an array of primes once (which ought to be faster). Uses the sieve of Eratosthenes implemented in the standard Prime library. The library itself uses a hard-coded initial ...


3

Python 3 (PyPy), time unknown import sys sys.stdout.write("2 3 ") n = 500000000 flag = n % 6 == 2 sieve = bytearray((n // 3 + flag >> 3) + 1) for i in range(1, int(n**0.5) // 3 + 1): if not (sieve[i >> 3] >> (i & 7)) & 1: k = (3 * i + 1) | 1 for j in range(k * k // 3, n // 3 + flag, 2 * k): ...


3

Ruby, Unknown Time Faster implementation(removes logic and directly prints list): Uses Class: Prime(Ruby 2.5.1) require 'prime' Prime.each(499999993) do |prime| p prime end Old code: require 'prime' for i in 2..499999993 i.prime?? p(i):() end Try it online!


5

Python 3, time unknown from sympy import isprime print(2) for i in range(1,499999995,2): if(isprime(i)): print(i) Try it online! TIO limits output to 128KiB. I'm hoping sympy's isprime is written in a lower-level language (which will improve its speed) because some Python builtins do that.


2

05AB1E, unknown time 26355867 Åp Try it online! TIO limits output to 128KiB, so the full output isn't generated. Simply pushes the first 26355867 primes.


3

Python 2, 45 bytes i=k=P=1 while~i*~i-k:P*=k;k+=1;i+=i*i<k>0<P%k Try it online! Here's a demonstration of the code halting if we modify it to claim that all of range(36,49) is non-prime. We use the Wilson's Theorem prime generator. We count up potential primes k, and the condition P%k>0 is met exactly for primes. Except, we use P*=k instead of ...


0

Scala, 142 bytes def f(d:java.time.LocalDate)=LazyList.iterate(d)(_ minusDays 1)find{d=>val s=d.getYear+d.getMonthValue+d.getDayOfMonth 2 to s/2 forall(s%_>0)} Uses java.time.LocalDate as input and output. Try it in Scastie


2

Python 3, 107 99 88 86 90 86 bytes n=2 while n:n=n+1if sum(min(i%j for j in range(2,i))for i in range(n*n+1,~n*~n))else 0 Try it online! Initially, n=2. Then it checks if any of the 2n numbers between n^2 and (n+1)^2 are prime or not. If yes, then n is incremented, otherwise n is set to 0 and the loop terminates.


2

C (gcc), 88 84 bytes Saved 4 bytes thanks to ceilingcat!!! q;h;i;j;f(n){for(h=n=1;h;++n)for(h=0,i=n*n;q=j=++i<~n*~n;h|=q)for(;++j<i;)q=q&&i%j;} Try it online! Will run forever on an infinite machine (with new infinity-bit int types!) so long as there's always a prime number in the interval \$(n^2,(n+1)^2)\$. Here's the same code modified to ...


4

05AB1E, 11 bytes First attempt: [N>nÅMNn‹#] Fixed (after @ovs notes): [NÌnÅMN>n‹# Explanation: [NÌnÅMN>n‹# [ Infinite Loop N Current loop index (starts from 0 to Infinity) Ì add 2 ( we want to start from N=1 instead of N=0) n Squaring - (N+1)**2 ÅM ...


5

C (gcc), 194 180 169 bytes #include<gmp.h> main(){mpz_t n,l,h;for(mpz_init_set_ui(n,1),mpz_init(l),mpz_init(h);mpz_mul(l,n,n),mpz_add_ui(n,n,1),mpz_mul(h,n,n),mpz_nextprime(l,l),mpz_cmp(l,h)<1;);} Try it online! -14 bytes thanks to ceilingcat! -11 bytes again thanks to ceilingcat! To test, here's one that outputs the prime in each range: C (gcc), ...


2

Scala, 98 93 bytes -(2+5) bytes thanks to Dominic Van Essen LazyList.iterate(2:BigInt)(_+1)forall(n=>n*n to(n+1 pow 2)exists(x=>n to(2,-1)forall(x%_>0))) Without BigInt, it could be made a few bytes shorter, but then it would overflow. It first creates an infinite list starting at 2, then checks for each n in that list if, in the range n^2 to (n+1)^...


5

Jelly, 7 bytes ‘ɼ²ÆCµƬ A niladic Link which, if the conjecture is False, will yield a list of counts of primes between \$2\$ and \$k^2\$ where \$k\$ is the zero-based index of the element (although the zero-indexed element will be None rather than 0). The final value in the list will be the count of primes between \$2\$ and \$n^2\$ (the next term would be ...


3

Io, 124 bytes method(x :=1;loop(s :=0;for(i,x*x,x*(x+2)+1,if(Range 1 to(i)asList select(o,i%o<1)size<3,s :=1;break));if(s<1,break);x=x+1)) Try it online!


5

Retina 0.8.2, 67 62 bytes _¶¶_ {`(_+)¶_*(¶_+) _$1$2$2$1$1_ ¶(_+)¶(?!_*(?!(__+)\2+$)\1) Don't try it online! Instead, try a Retina 1 version which takes as input the number of iterations. Explanation: _¶¶_ The working area contains n+1, n² and (n+1)², where n starts at 0 but is immediately incremented (saving 5 bytes over my previous answer which started ...


7

R, 60 55 54 bytes Edit: -1 byte thanks to Robin Ryder while(sd(sapply(lapply(T^2:(T=T+1)^2,`%%`,2:T),all)))T Try it online!, or, since it's rather boring to run a program that (probably) never halts and produces no output, try a slightly longer version (exchanging n=sum( for any() that prints n and the number of primes in the interval (n-1)^2..n^2 for each ...


2

Wolfram Language (Mathematica), 30 bytes For[n=1,NextPrime[n++^2]<n^2,] Try it online! Special thankks to @att for saving 9 bytes


7

05AB1E, 17 11 bytes ∞.∆DnÅNs>n@ Try it online! -6 bytes thanks to @ovs Explained ∞.∆DnÅNs>n@ ∞ Push an infinite list .∆ Find the first item in that list that: D s>n (n+1)^2 is @ larger or equal than nÅN the next prime from n^2


12

JavaScript (Node.js),  49  47 bytes A full program that stops only if there's some \$n\ge2\$ such that all \$x\in[(n-1)^2..n^2]\$ are composite. for(x=n=2n;x-n*n;d?0:x=n*n++)for(d=x++;x%d--;); Try it online! Commented for( // outer loop: x = n = 2n; // start with x = n = 2 x - n * n; // stop if x = n² d ? 0 : x = ...


3

MATL, 11 10 bytes `@U_Yq@QU< Try it online! -1 byte thanks to Luis Mendo. Otherwise, pretty straightforward. ` % Start a loop @ % Push loop index (n) U % square _Yq % Get next prime @QU % Loop index plus one, squared < % Continue loop if the prime is smaller than this.


2

Japt, 12 11 bytes bytes _²ôZÑ dj}f1 Test it (May cause your browser to explode!) _ :Function taking an integer Z as argument ² : Z squared ZÑ : Z times 2 ô : Range [Z²,Z²+Z*2] d : Any j : Prime } :End function f1 :Return the first Z≥1 that ...


6

Jelly, 9 bytes ²+æR$Ṇµ2# Try it online! -1 byte thanks to caird coinheringaahing -1 byte thanks to Jonathan Allan


8

Raku, 34 bytes 1...{is-prime none $_²..($_+1)²} Try it online! Counts upwards until it finds a number where none of the given range are prime.


0

FEU, 178 bytes a/lllmtwrzxyzrrszwqqwqqwzqwzxzwqqwqqwszvyzxyzxzwszszzqvzvzwszwzxzvzvzwzqwzxzvzv1yrtvooop m/l/mmmm/m/nnn/n/7777/o/pppp/p/333/q/1z/r/sy/s/x1/t/uuu/u/vvv/v/ww/w/xx/x/11/y/zzz/z/88/g Try it online! Compression. My compression testing wasn't very rigorous, so this might be able to be optimized.


1

qalc/Qalculate, 8 bytes If reading from any variable is allowed: p-1)!²%p Otherwise 12 bytes: ans−1)!²%ans The great thing about "programming" in a calculator is that you can basically just enter formulas directly. Example usage in Bash for the "ans" case: echo "5" | qalc -t -f - "ans−1)! ²%ans" Note that I added a ...


Top 50 recent answers are included