Die meisten von uns wissen ...
dass alle Primzahlen p>3
von der Form sind
Aber wie viele Plus-Primzahlen ( 6n+1
) und wie viele Minus-Primzahlen ( 6n-1
) befinden sich in einem bestimmten Bereich?
Die Herausforderung
Gegeben eine ganze Zahl k>5
, zählen , wie viele primes<=k
sind PlusPrimes und wie viele sind MinusPrimes .
Beispiele
für k=100
wir haben
[5, 11, 17, 23, 29, 41, 47, 53, 59, 71, 83, 89]
12 MinusPrimes
und
[7, 13, 19, 31, 37, 43, 61, 67, 73, 79, 97]
11 PlusPrimes
für k=149
wir haben
[5, 11, 17, 23, 29, 41, 47, 53, 59, 71, 83, 89, 101, 107, 113, 131, 137, 149]
18 MinusPrimes
und
[7, 13, 19, 31, 37, 43, 61, 67, 73, 79, 97, 103, 109, 127, 139]
15 PlusPrimes
Regeln
Ihr Code muss 2 Ganzzahlen ausgeben : eine für die MinusPrimes und eine für die PlusPrimes in beliebiger Reihenfolge (bitte geben Sie an, welche welche ist).
Das ist Code-Golf : Die kürzeste Antwort in Bytes gewinnt!
Testfälle
Eingabe -> Ausgabe [ MinusPrimes , PlusPrimes ]
6->[1,0]
7->[1,1]
86->[11,10]
986->[86,78]
5252->[351,344]
100000->[4806,4784]
4000000->[141696, 141448]
0%6
ein Vielfaches von 6,1%6
kann nicht bestimmt werden,2%6
ist ein Vielfaches von 2,3%6
ist ein Vielfaches von 3,4%6
ist ein Vielfaches von 2 und5%6
kann nicht bestimmt werden.Antworten:
05AB1E ,
109 Bytes1 Byte gespeichert dank Erik the Outgolfer gespart
Ausgänge als
[PlusPrimes, MinusPrimes]
Probieren Sie es online! oder als Testsuite
Erläuterung
quelle
MATL , 10 Bytes
Probieren Sie es online! Oder überprüfen Sie alle Testfälle .
Erläuterung
quelle
Python 2 , 77 Bytes
-2 Bytes dank Neil
Probieren Sie es online!
Vorherige Lösung,
838179 Bytes-1 Byte dank Mr. Xcoder
-2 Byte dank Halvard Hummel
Probieren Sie es online!
Beide Ausgabe als [MinusPrimes, PlusPrimes]
quelle
[]
s benötigen .Gelee , 7 Bytes
Plus, dann minus.
Probieren Sie es online!
Wie es funktioniert
quelle
Mathematica, 51 Bytes
Probieren Sie es online!
@ngenisis hat es abgespielt und dabei 4 Bytes gespart
Mathematica, 47 Bytes
quelle
Mod
can also be infix, and if you're going to name the first arguments
, just use a named argument:sPrime~Array~PrimePi@s~Mod~6~Count~#&/@{5,1}
Japt,
151311 bytesOutput order is
[+,-]
.Test it
ë
method for arrays to my attention.Explanation
Implicit input of integer
U
.Generate an array of integers (
õ
) from 1 toU
and check if each is a prime (j
), giving an array of booleans.Partition the array into sub-arrays of length 6.
Transpose (
y
) and sum the columns.Get every 4th element of the array and implicitly output them.
Original,
19171615 bytesTest it
quelle
J, 23 bytes
Try it online!
quelle
Retina,
5351 bytesTry it online! Explanation:
Convert to unary.
Count from 1 up to
n
.Delete numbers less than 4.
Delete composite numbers.
Take the remainder modulo 6.
Print the number of numbers with a remainder between 3 and 5.
Print the number of numbers with a remainder of 1.
quelle
Ruby,
6160 bytes(52 bytes + 8 for the
-rprimes
flag)Returns an array of the form [plus primes, minus primes].
Saved 1 byte thanks to G B!
Try it online.
quelle
count
on the range without the splat operator (save 1 byte).Perl 6, 42 bytes
Saved 1 byte by removing a useless space...
Saved 2 bytes by reorganizing the
map
call — thanks to @Joshua.Saved 3 bytes because
.round
equals.round: 1
.Actually the complex exponential is cool but very expensive characterwise. Saved 10 bytes just by ditching it...
Try it online!
This was the version with the complex exponential. (I like it too much to delete it.) The new version works exactly in the same way, just the complex exponential is replaced by the much shorter ternary operator.
Try it online!
The output is a complex number
(PlusPrimes) + (MinusPrimes)i
. I hope it's not too much against the rules.Explanation: It's a function that takes one integer argument. We iterate over all integers from 5 to the argument (
(5..$_)
). For each of these, we evaluate.is-prime
(this is called on$_
, the argument of the mapped block), multiply it (if numified,True == 1, False == 0
) with a complex exponential that's made to be eitherexp(0) = 1
(for$_%6 = 1
) orexp(iπ/2) = i
(for$_%6 = 5
), and finally round it to the nearest integer. Summing them up with[+]
gives the result.Finally: it's really efficient, so I'm not sure if TIO won't time out before you get your output for higher numbers (for 1e5, it takes 26 sec on my machine, and TIO tends to be somewhat slower).
quelle
map
orgrep
can sometimes cost you a few characters. This saves 2 chars:{[+] map {.is-prime*exp(π*($_%6-1)i/8).round: 1},5..$_}
Actually, 21 bytes
Try it online!
Outputs the PlusPrimes first, followed by the MinusPrimes
Explanation:
quelle
Stacked, 37 bytes
Try it online!
Rather slow, tests for primality for each K < N. Works similar to my J answer.
quelle
MATLAB 2017a, 29 Bytes
Explanation:
primes(k)
gets all primes up to and including k.mod(primes(k),6)'
takes the modulus 6 of all primes and transposes it so the sum runs along the correct dimension.==[5,1]
sets all fives (minusPrimes) to 1 in the first column and all ones (plusPrimes) to 1 in the second column.sum()
sums each column.This outputs
[minusPrime, plusPrime]
quelle
Japt,
1816 bytes-2 bytes thanks to @Oliver
Try it online!
Outputs in the format
[PlusPrimes, MinusPrimes]
.quelle
[5,1]
to get the counts and you got there first.f
ilter and a string; I used the mapping function ofõ
and an array. Besides, I got the[5,1]
idea from another answer.5â
to get[1,5]
C#,
202179174 Bytes-23 Bytes thanks to Mr. Xcoder
-5 Bytes thanks to Cyoce
Function that returns an array of length 2,
[MinusPrimes, PlusPrimes]
Execute by callinga(n)
.Properly formatted code on Try It Online: Here
quelle
public int[]a(int n){int[]r=new int[2];for(int i=5;i<=n;i++)if(i%2*b(i)>0)if(i%6<5)r[1]++;else++r[0];return r;}public int b(int n){for(int i=3;i<=Math.Sqrt(n)+1;i+=2)if(n%i<1)return 0;return 1;}
public int[]a(int n){int[]r=new int[2];for(int i=5;i<=n;i++)if(i%2*b(i)>0)if(i%6<5)r[1]++;else++r[0];return r;}public int b(int n){for(int i=3;i-2<Math.Sqrt(n);i+=2)if(n%i<1)return 0;return 1;}
Haskell,
8169 bytesTry it online!
First solution was:
But I read w0lf's answer in Ruby...
quelle
Pyth, 15 bytes
Test Suite.
Pyth, 16 bytes
Test Suite.
How?
Explanation #1
Explanation #2
Alternatives:
quelle
Jelly,
12 1110 bytesThanks to @cairdcoinheringaahing for some tips in chat. Thanks to @Dennis for saving one byte in chat.
Try it online!
Jelly, 11 bytes
Try it online!
Jelly, 11 bytes
Try it online!
How does this work?
Explanation #1
Explanation #2
Explanation #3
quelle
Java 8,
141140138106101100969481 bytesReturns an integer-array with two values, in reversed order compared to the challenge description:
[plusPrime, minusPrime]
.Port of @Xynos' C# answer, after I golfed
394042 bytes.Huge help from @Nevay for another whopping -55 bytes.
Explanation:
Try it here. (Final test case of
4000000
is exceeding the 60 sec time limit slightly.)quelle
n->{int r[]={0,0},i=4,j,c;for(;i++<n;){for(j=c=1;j*j<i;)c=i%(j+=2)<1?0:c;if(i%2*c>0)r[i%6%5]++;}return r;}
n->{int r[]={0,0},i=4,j,c;for(;i++<n;r[i%6%5%2]-=-i%2*c>>-1)for(j=c=1;j*j<i;)c|=i%(j+=2)-1;return r;}
n->{int r[]={0,0},i=4,j,c;for(;i++<n;r[i%6%5%2]+=i&c)for(j=c=1;j*j++<i;)c&=-i%++j>>-1;return r;}
(-1 thanks to yourj++,++j
)n->{int r[]={0,0},i=4,j,c;for(;i++<n;r[i%6/4]+=i&c)for(j=c=1;j*j++<i;)c&=-i%++j>>-1;return r;}
([plusPrime, minusPrime]
).n->{int r[]={0,0},c;for(;n-->4;r[n%6/4]+=c)for(c=n;c>1;)c=c-1&~n%c>>-1;return r;}
JavaScript (ES6),
8382806866 bytesTurned out a fully recursive solution was much shorter than mapping an array!
Output order is
[-,+]
. Craps out with an overflow error somewhere around 3490.Try it
quelle
CJam, 19 bytes
Program that takes the input from STDIN, and outputs the two numbers separated by newline through STDOUT.
Try it online!
Explanation
quelle
R + numbers,
66605840 bytes-16 bytes thanks to Jarko Dubbeldam! I subsequently golfed another two bytes off.
Prints
PlusPrimes MinusPrimes
to stdout; reads from stdin.table
tabulates the count of each occurrence of the values in its input vector, in ascending order of value. Hence, since there are only two values, namely1
and5
(mod 6), this is exactly the function we need, along withnumbers::Primes
, which returns all primes between4
and the input.Try it online!
Base R,
9791898665 bytesa bunch of bytes saved by Jarko here, too
This is nearly identical to the above, except it calculates all the primes in base R rather than using a package, and it returns by function output rather than printing it out. You can see in the output that it returns a table with names
1
and5
, with the counts below.Try it online!
quelle
all(x%%2:x^.5>0)
, anything nonzero is already truthy, soall(x%%2:x^.5)
works too4
we can get rid of the>4
since we won't have2
in there anymore as a prime, so this golfs to 40 bytes instead.Pari/GP, 41 bytes
Try it online!
quelle
JavaScript (SpiderMonkey),
151,140, 131 bytesTry it online!
Thanks to shaggy for helping with a bug fix and golfing.
Explanation:
quelle
17,15
for 149 (Should be18,15
). You need to increase the size of your array by 1: TIO. Incidentally, this is just "vanilla" ES6, nothing specific to SpiderMonkey in it. Also, you can use Stack Snippets for JS, rather than TIO. And, you have a lot of spaces you can remove.