Gaussian primes: Difference between revisions
Content deleted Content added
Added FreeBASIC |
m Add a link to a media file. |
||
(4 intermediate revisions by 3 users not shown) | |||
Line 266:
* * | * *
|
</pre>
=={{header|C++}}==
<syntaxhighlight lang="c++">
#include <algorithm>
#include <complex>
#include <cstdint>
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>
std::string to_string(const std::complex<int32_t>& complex_number) {
std::string prefix = ( std::real(complex_number) < 0 ) ? "" : " ";
std::string real_part = prefix + std::to_string(std::real(complex_number));
std::string sign = ( std::imag(complex_number) < 0 ) ? " - " : " + ";
const uint32_t imag_part = ( sign == " - " ) ? -std::imag(complex_number) : std::imag(complex_number);
return real_part + sign + std::to_string(imag_part) + "i";
}
bool is_real_prime(const uint32_t& number) {
if ( number < 2 ) { return false; }
if ( number % 2 == 0 ) { return number == 2; }
if ( number % 3 == 0 ) { return number == 3; }
for ( uint32_t divisor = 5; divisor * divisor <= number; divisor += 2 ) {
if ( number % divisor == 0 ) { return false; }
}
return true;
}
bool is_gaussian_prime(const std::complex<int32_t> complex_number) {
int32_t r = std::real(complex_number); if ( r < 0 ) { r = -r; }
int32_t c = std::imag(complex_number); if ( c < 0 ) { c = -c; }
return is_real_prime(r * r + c * c)
|| ( c == 0 && is_real_prime(r) && ( r - 3 ) % 4 == 0 )
|| ( r == 0 && is_real_prime(c) && ( c - 3 ) % 4 == 0 );
}
int main() {
const int32_t limit = 10;
std::vector<std::complex<int32_t>> gaussian_primes = { };
for ( int32_t real = -limit; real <= limit; ++real ) {
for ( int32_t imag = -limit; imag <= limit; ++imag ) {
const std::complex<int32_t> candidate(real, imag);
if ( std::norm(candidate) < limit * limit && is_gaussian_prime(candidate) ) {
gaussian_primes.emplace_back(candidate);
}
}
}
std::sort(gaussian_primes.begin(), gaussian_primes.end(),
[](const auto& lhs, const auto& rhs) { return std::norm(lhs) < std::norm(rhs); });
std::cout << "Gaussian primes less than radius 10 from the origin:" << "\n";
for ( uint32_t i = 0; i < gaussian_primes.size(); ++i ) {
std::cout << std::setw(8) << to_string(gaussian_primes[i]) << ( i % 10 == 9 ? "\n" : " " );
}
}
</syntaxhighlight>
{{ out }}
<pre>
Gaussian primes less than radius 10 from the origin:
-1 - 1i 1 + 1i 1 - 1i -1 + 1i 2 + 1i 2 - 1i -1 - 2i -1 + 2i -2 + 1i -2 - 1i
1 - 2i 1 + 2i 0 + 3i 0 - 3i -3 + 0i 3 + 0i -3 - 2i -3 + 2i 2 + 3i -2 + 3i
-2 - 3i 3 - 2i 3 + 2i 2 - 3i -1 - 4i -4 - 1i -4 + 1i -1 + 4i 4 - 1i 4 + 1i
1 - 4i 1 + 4i -2 + 5i 2 - 5i 2 + 5i 5 + 2i 5 - 2i -2 - 5i -5 - 2i -5 + 2i
6 + 1i 1 - 6i 1 + 6i -6 - 1i -6 + 1i -1 + 6i 6 - 1i -1 - 6i 4 - 5i -5 - 4i
-4 + 5i -4 - 5i -5 + 4i 4 + 5i 5 - 4i 5 + 4i 7 + 0i 0 - 7i -7 + 0i 0 + 7i
2 + 7i 7 + 2i -2 + 7i -2 - 7i 2 - 7i 7 - 2i -7 - 2i -7 + 2i 5 + 6i 5 - 6i
-6 - 5i -6 + 5i 6 + 5i -5 - 6i -5 + 6i 6 - 5i 3 + 8i 8 + 3i 3 - 8i 8 - 3i
-3 - 8i -8 + 3i -8 - 3i -3 + 8i 5 + 8i 8 - 5i 5 - 8i 8 + 5i -5 + 8i -5 - 8i
-8 + 5i -8 - 5i 9 - 4i -9 - 4i 9 + 4i 4 + 9i 4 - 9i -4 + 9i -4 - 9i -9 + 4i
</pre>
Line 432 ⟶ 506:
3j_8 3j8 8j_3 8j3 _8j_5 _8j5 _5j_8 _5j8 5j_8 5j8
8j_5 8j5 _9j_4 _9j4 _4j_9 _4j9 4j_9 4j9 9j_4 9j4</syntaxhighlight>
=={{header|Java}}==
<syntaxhighlight lang="java">
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public final class GaussianPrimes {
public static void main(String[] args) throws IOException {
List<Complex> gaussianPrimes = createGaussianPrimes(10);
System.out.println("Gaussian primes less than radius 10 from the origin:");
for ( int i = 0; i < gaussianPrimes.size(); i++ ) {
System.out.print(String.format("%s%s", gaussianPrimes.get(i), ( i % 10 == 9 ? "\n" : " " )));
}
String text = svgFileText(createGaussianPrimes(50), 1000);
Files.write(Paths.get("GaussianPrimesJava.svg"), text.getBytes());
}
private static List<Complex> createGaussianPrimes(int limit) {
List<Complex> result = new ArrayList<Complex>();
for ( int real = -limit; real <= limit; real++ ) {
for ( int imag = -limit; imag <= limit; imag++ ) {
Complex number = new Complex(real, imag);
if ( number.norm() < limit * limit && number.isGaussianPrime() ) {
result.addLast(number);
}
}
}
Collections.sort(result, Comparator.comparing(Complex::norm)
.thenComparing(Complex::real)
.thenComparing(Complex::imag));
return result;
}
private static String svgFileText(List<Complex> numbers, int size) {
StringBuilder text = new StringBuilder();
text.append("<svg xmlns='http://www.w3.org/2000/svg'");
text.append(" width='" + size + "' height='" + size + "'>\n");
text.append("<rect style='width:100%; height:100%; fill:cyan' />\n");
text.append("<line x1='0' y1='500' x2='1000' y2='500' style='stroke:black; stroke-width:2' />");
text.append("<line x1='500' y1='0' x2='500' y2='1000' style='stroke:black; stroke-width:2' />");
for ( Complex number : numbers ) {
text.append("<circle cx='" + ( 500 + 8 * number.real() ) + "'");
text.append(" cy='" + ( 500 + 8 * number.imag() ) + "'");
text.append(" r='4' stroke='black' stroke-width='2' fill='yellow' />");
}
text.append("</svg>");
return text.toString();
}
}
final class Complex {
public Complex(int aReal, int aImag) {
real = aReal;
imag = aImag;
}
public int norm() {
return real * real + imag * imag;
}
public boolean isGaussianPrime() {
final int r = Math.abs(real);
final int c = Math.abs(imag);
return isRealPrime(r * r + c * c)
|| c == 0 && isRealPrime(r) && ( r - 3 ) % 4 == 0
|| r == 0 && isRealPrime(c) && ( c - 3 ) % 4 == 0;
}
@Override
public String toString() {
String prefix = ( real < 0 ) ? "" : " ";
String realPart = prefix + real;
String sign = ( imag < 0 ) ? " - " : " + ";
return realPart + sign + Math.abs(imag) + "i";
}
public int real() {
return real;
}
public int imag() {
return imag;
}
private boolean isRealPrime(int number) {
if ( number < 2 ) { return false; }
if ( number % 2 == 0 ) { return number == 2; }
if ( number % 3 == 0 ) { return number == 3; }
for ( int divisor = 5; divisor * divisor <= number; divisor += 2 ) {
if ( number % divisor == 0 ) { return false; }
}
return true;
}
private final int real;
private final int imag;
}
</syntaxhighlight>
{{ out }}
<pre>
Gaussian primes less than radius 10 from the origin:
-1 - 1i -1 + 1i 1 - 1i 1 + 1i -2 - 1i -2 + 1i -1 - 2i -1 + 2i 1 - 2i 1 + 2i
2 - 1i 2 + 1i -3 + 0i 0 - 3i 0 + 3i 3 + 0i -3 - 2i -3 + 2i -2 - 3i -2 + 3i
2 - 3i 2 + 3i 3 - 2i 3 + 2i -4 - 1i -4 + 1i -1 - 4i -1 + 4i 1 - 4i 1 + 4i
4 - 1i 4 + 1i -5 - 2i -5 + 2i -2 - 5i -2 + 5i 2 - 5i 2 + 5i 5 - 2i 5 + 2i
-6 - 1i -6 + 1i -1 - 6i -1 + 6i 1 - 6i 1 + 6i 6 - 1i 6 + 1i -5 - 4i -5 + 4i
-4 - 5i -4 + 5i 4 - 5i 4 + 5i 5 - 4i 5 + 4i -7 + 0i 0 - 7i 0 + 7i 7 + 0i
-7 - 2i -7 + 2i -2 - 7i -2 + 7i 2 - 7i 2 + 7i 7 - 2i 7 + 2i -6 - 5i -6 + 5i
-5 - 6i -5 + 6i 5 - 6i 5 + 6i 6 - 5i 6 + 5i -8 - 3i -8 + 3i -3 - 8i -3 + 8i
3 - 8i 3 + 8i 8 - 3i 8 + 3i -8 - 5i -8 + 5i -5 - 8i -5 + 8i 5 - 8i 5 + 8i
8 - 5i 8 + 5i -9 - 4i -9 + 4i -4 - 9i -4 + 9i 4 - 9i 4 + 9i 9 - 4i 9 + 4i
</pre>
[[Media:GaussianPrimesJava.svg]]
=={{header|jq}}==
Line 1,278 ⟶ 1,477:
Off-site SVG image: [https://raw.githubusercontent.com/thundergnat/rc/master/img/gaussian-primes-raku.svg gaussian-primes-raku.svg]
=={{header|RPL}}==
« → radius
« { }
1 radius '''FOR''' a
0 a '''FOR''' b
'''IF''' a SQ b SQ + radius SQ ≤ '''THEN'''
{ }
'''IF''' b '''THEN'''
'''IF''' a SQ b SQ + ISPRIME? '''THEN'''
a b R→C DUP CONJ OVER NEG DUP CONJ 4 →LIST +
'''END'''
'''ELSE'''
'''IF''' a ISPRIME? a 4 MOD 3 == AND '''THEN'''
a DUP NEG 2 →LIST +
'''END'''
'''END'''
'''IF''' DUP SIZE '''THEN '''
'''IF''' a b ≠ '''THEN''' DUP (0,1) * + '''END'''
+
'''ELSE''' DROP '''END'''
'''END'''
'''NEXT NEXT '''
» » '<span style="color:blue">GPRIMES</span>' STO
« 10 <span style="color:blue">GPRIMES</span>
(-50 -50) PMIN (50 50) PMAX ERASE
50 <span style="color:blue">GPRIMES</span> 1 « '''IF''' DUP IM NOT '''THEN''' 0 R→C '''END''' PIXON » DOLIST
{ } PVIEW
» '<span style="color:blue">TASK</span>' STO
[[File:Gaussian primes.png|thumb|alt=Gaussian primes within radius 50|HP-48G emulator screenshot]]
{{out}}
<pre>
1: { (1.,1.) (1.,-1.) (-1.,-1.) (-1.,1.) (2.,1.) (2.,-1.) (-2.,-1.) (-2.,1.) (-1.,2.) (1.,2.) (1.,-2.) (-1.,-2.) 3 -3 (0.,3.) (0.,-3.) (3.,2.) (3.,-2.) (-3.,-2.) (-3.,2.) (-2.,3.) (2.,3.) (2.,-3.) (-2.,-3.) (4.,1.) (4.,-1.) (-4.,-1.) (-4.,1.) (-1.,4.) (1.,4.) (1.,-4.) (-1.,-4.) (5.,2.) (5.,-2.) (-5.,-2.) (-5.,2.) (-2.,5.) (2.,5.) (2.,-5.) (-2.,-5.) (5.,4.) (5.,-4.) (-5.,-4.) (-5.,4.) (-4.,5.) (4.,5.) (4.,-5.) (-4.,-5.) (6.,1.) (6.,-1.) (-6.,-1.) (-6.,1.) (-1.,6.) (1.,6.) (1.,-6.) (-1.,-6.) (6.,5.) (6.,-5.) (-6.,-5.) (-6.,5.) (-5.,6.) (5.,6.) (5.,-6.) (-5.,-6.) 7 -7 (0.,7.) (0.,-7.) (7.,2.) (7.,-2.) (-7.,-2.) (-7.,2.) (-2.,7.) (2.,7.) (2.,-7.) (-2.,-7.) (8.,3.) (8.,-3.) (-8.,-3.) (-8.,3.) (-3.,8.) (3.,8.) (3.,-8.) (-3.,-8.) (8.,5.) (8.,-5.) (-8.,-5.) (-8.,5.) (-5.,8.) (5.,8.) (5.,-8.) (-5.,-8.) (9.,4.) (9.,-4.) (-9.,-4.) (-9.,4.) (-4.,9.) (4.,9.) (4.,-9.) (-4.,-9.) }
</pre>
=={{header|Sidef}}==
{{trans|Perl}}
<syntaxhighlight lang="ruby">func gaussianprimes(size) {
var plot = (2*size + 1 -> of { 2*size + 1 -> of(' ') })
var primes = []
for A in (-size .. size) {
var limit = isqrt(size**2 - A**2)
for B in (-limit .. limit) {
var n = Gauss(A, B)
if (n.is_prime) {
primes << n.to_n
plot[B + size + 1][A + size + 1] = 'X'
}
}
}
return (plot, primes)
}
with(10) {|n|
var(_plot, primes) = gaussianprimes(n)
say "Primes within #{n}:"
primes.slices(10).each { .map{'%7s'%_}.join(", ").say }
}
with (50) {|n|
var (plot) = gaussianprimes(n)
say "\nPlot within #{n}"
plot.each { .join('').say }
}</syntaxhighlight>
{{out}}
<pre>
Primes within 10:
-9 - 4i, -9 + 4i, -8 - 5i, -8 - 3i, -8 + 3i, -8 + 5i, -7 - 2i, -7, -7 + 2i, -6 - 5i
-6 - 1i, -6 + 1i, -6 + 5i, -5 - 8i, -5 - 6i, -5 - 4i, -5 - 2i, -5 + 2i, -5 + 4i, -5 + 6i
-5 + 8i, -4 - 9i, -4 - 5i, -4 - 1i, -4 + 1i, -4 + 5i, -4 + 9i, -3 - 8i, -3 - 2i, -3
-3 + 2i, -3 + 8i, -2 - 7i, -2 - 5i, -2 - 3i, -2 - 1i, -2 + 1i, -2 + 3i, -2 + 5i, -2 + 7i
-1 - 6i, -1 - 4i, -1 - 2i, -1 - 1i, -1 + 1i, -1 + 2i, -1 + 4i, -1 + 6i, -7i, -3i
3i, 7i, 1 - 6i, 1 - 4i, 1 - 2i, 1 - 1i, 1 + 1i, 1 + 2i, 1 + 4i, 1 + 6i
2 - 7i, 2 - 5i, 2 - 3i, 2 - 1i, 2 + 1i, 2 + 3i, 2 + 5i, 2 + 7i, 3 - 8i, 3 - 2i
3, 3 + 2i, 3 + 8i, 4 - 9i, 4 - 5i, 4 - 1i, 4 + 1i, 4 + 5i, 4 + 9i, 5 - 8i
5 - 6i, 5 - 4i, 5 - 2i, 5 + 2i, 5 + 4i, 5 + 6i, 5 + 8i, 6 - 5i, 6 - 1i, 6 + 1i
6 + 5i, 7 - 2i, 7, 7 + 2i, 8 - 5i, 8 - 3i, 8 + 3i, 8 + 5i, 9 - 4i, 9 + 4i
</pre>
The plot up to 50 is identical to the one produced by the Perl entry.
=={{header|Wren}}==
|