Honaker primes: Difference between revisions

m
Forth performance improvement
m (Forth performance improvement)
 
(41 intermediate revisions by 24 users not shown)
Line 1:
{{draft task}}
 
A Honaker prime is a prime whose digital sum is equal to the digital sum of its position in the sequence of primes.
Line 22:
 
 
 
=={{header|11l}}==
{{trans|Python}}
 
<syntaxhighlight lang="11l">
F primes_up_to_limit(Int limit)
[Int] r
I limit >= 2
r.append(2)
 
V isprime = [1B] * ((limit - 1) I/ 2)
V sieveend = Int(sqrt(limit))
L(i) 0 .< isprime.len
I isprime[i]
Int p = i * 2 + 3
r.append(p)
I i <= sieveend
L(j) ((p * p - 3) >> 1 .< isprime.len).step(p)
isprime[j] = 0B
R r
 
F digitsum(num)
‘ Digit sum of an integer (base 10) ’
R sum(String(num).map(c -> Int(c)))
 
F generate_honaker(limit = 5'000'000)
‘ Generate the sequence of Honaker primes with their sequence and primepi values ’
V honaker = enumerate(primes_up_to_limit(limit)).filter((i, p) -> digitsum(p) == digitsum(i + 1)).map((i, p) -> (i + 1, p))
R enumerate(honaker).map((hcount, pp) -> (hcount + 1, pp[0], pp[1]))
 
print(‘First 50 Honaker primes:’)
L(p) generate_honaker()
I p[0] < 51
print(f:‘{String(p):<16}’, end' I p[0] % 5 == 0 {"\n"} E ‘’)
E I p[0] == 10'000
print(f:"\nThe 10,000th Honaker prime is the {commatize(p[1])}th one, which is {commatize(p[2])}.")
L.break
</syntaxhighlight>
 
{{out}}
<pre>
First 50 Honaker primes:
(1, 32, 131) (2, 56, 263) (3, 88, 457) (4, 175, 1039) (5, 176, 1049)
(6, 182, 1091) (7, 212, 1301) (8, 218, 1361) (9, 227, 1433) (10, 248, 1571)
(11, 293, 1913) (12, 295, 1933) (13, 323, 2141) (14, 331, 2221) (15, 338, 2273)
(16, 362, 2441) (17, 377, 2591) (18, 386, 2663) (19, 394, 2707) (20, 397, 2719)
(21, 398, 2729) (22, 409, 2803) (23, 439, 3067) (24, 446, 3137) (25, 457, 3229)
(26, 481, 3433) (27, 499, 3559) (28, 508, 3631) (29, 563, 4091) (30, 571, 4153)
(31, 595, 4357) (32, 599, 4397) (33, 635, 4703) (34, 637, 4723) (35, 655, 4903)
(36, 671, 5009) (37, 728, 5507) (38, 751, 5701) (39, 752, 5711) (40, 755, 5741)
(41, 761, 5801) (42, 767, 5843) (43, 779, 5927) (44, 820, 6301) (45, 821, 6311)
(46, 826, 6343) (47, 827, 6353) (48, 847, 6553) (49, 848, 6563) (50, 857, 6653)
 
The 10,000th Honaker prime is the 286,069th one, which is 4,043,749.
</pre>
 
=={{header|ALGOL 68}}==
After experimenting on TIO.RUN, it seems that with ALGOL 68G, calculating the digit sums the "traditional" way is slightly faster than generating a table of digit sums. In the sample below, the digit sum is calculated by first converting the number to a string - this is faster in ALGOL 68G than using MOD and division. For other implementations of Algol 68, using MOD and division may be faster.
<syntaxhighlight lang="algol68">
BEGIN # find some Honaker primes: primes whose digit-sum equals the #
Line 95 ⟶ 150:
 
Honaker prime 10000 is prime 286069: 4043749
</pre>
 
=={{header|Arturo}}==
 
<syntaxhighlight lang="arturo">honaker?: function [n, pos]->
equal? sum digits n sum digits pos
 
idx: 0
found: 0
 
honakers: []
 
loop 2..∞ 'n [
if prime? n [
idx: idx + 1
 
if honaker? n idx [
found: found + 1
'honakers ++ @[@[found, idx, n]]
]
]
if found = 50 -> break
]
 
loop split.every: 5 honakers 'x ->
print map x 's -> pad as.code s 14</syntaxhighlight>
 
{{out}}
 
<pre> [1 32 131] [2 56 263] [3 88 457] [4 175 1039] [5 176 1049]
[6 182 1091] [7 212 1301] [8 218 1361] [9 227 1433] [10 248 1571]
[11 293 1913] [12 295 1933] [13 323 2141] [14 331 2221] [15 338 2273]
[16 362 2441] [17 377 2591] [18 386 2663] [19 394 2707] [20 397 2719]
[21 398 2729] [22 409 2803] [23 439 3067] [24 446 3137] [25 457 3229]
[26 481 3433] [27 499 3559] [28 508 3631] [29 563 4091] [30 571 4153]
[31 595 4357] [32 599 4397] [33 635 4703] [34 637 4723] [35 655 4903]
[36 671 5009] [37 728 5507] [38 751 5701] [39 752 5711] [40 755 5741]
[41 761 5801] [42 767 5843] [43 779 5927] [44 820 6301] [45 821 6311]
[46 826 6343] [47 827 6353] [48 847 6553] [49 848 6563] [50 857 6653]</pre>
 
=={{header|C}}==
{{trans|Wren}}
<syntaxhighlight lang="c">#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <locale.h>
 
#define LIMIT 5000000
 
typedef struct {
int x;
int y;
} pair;
 
int *primeSieve(int limit, int *length) {
int i, p, *primes;
int j, pc = 0;
limit++;
// True denotes composite, false denotes prime.
bool *c = calloc(limit, sizeof(bool)); // all false by default
c[0] = true;
c[1] = true;
for (i = 4; i < limit; i += 2) c[i] = true;
p = 3; // Start from 3.
while (true) {
int p2 = p * p;
if (p2 >= limit) break;
for (i = p2; i < limit; i += 2 * p) c[i] = true;
while (true) {
p += 2;
if (!c[p]) break;
}
}
for (i = 0; i < limit; ++i) {
if (!c[i]) ++pc;
}
primes = (int *)malloc(pc * sizeof(int));
for (i = 0, j = 0; i < limit; ++i) {
if (!c[i]) primes[j++] = i;
}
free(c);
*length = pc;
return primes;
}
 
int digitSum(int n) {
int sum = 0;
while (n > 0) {
sum += n % 10;
n /= 10;
}
return sum;
}
 
int main() {
int i, count, length, hc = 0;
int *primes = (int *)primeSieve(LIMIT, &length);
pair h[50], h10000;
for (i = 1, count = 0; count < 10000; ++i) {
if (digitSum(i) == digitSum(primes[i-1])) {
++count;
if (count <= 50) {
h[hc++] = (pair){i, primes[i-1]};
} else if (count == 10000) {
h10000.x = i;
h10000.y = primes[i-1];
}
}
}
setlocale(LC_NUMERIC, "");
printf("The first 50 Honaker primes (index, prime):\n");
for (i = 0; i < 50; ++i) {
printf("(%3d, %'5d) ", h[i].x, h[i].y);
if (!((i+1)%5)) printf("\n");
}
printf("\nand the 10,000th: (%'7d, %'9d)\n", h10000.x, h10000.y);
free(primes);
return 0;
}</syntaxhighlight>
 
{{out}}
<pre>
The first 50 Honaker primes (index, prime):
( 32, 131) ( 56, 263) ( 88, 457) (175, 1,039) (176, 1,049)
(182, 1,091) (212, 1,301) (218, 1,361) (227, 1,433) (248, 1,571)
(293, 1,913) (295, 1,933) (323, 2,141) (331, 2,221) (338, 2,273)
(362, 2,441) (377, 2,591) (386, 2,663) (394, 2,707) (397, 2,719)
(398, 2,729) (409, 2,803) (439, 3,067) (446, 3,137) (457, 3,229)
(481, 3,433) (499, 3,559) (508, 3,631) (563, 4,091) (571, 4,153)
(595, 4,357) (599, 4,397) (635, 4,703) (637, 4,723) (655, 4,903)
(671, 5,009) (728, 5,507) (751, 5,701) (752, 5,711) (755, 5,741)
(761, 5,801) (767, 5,843) (779, 5,927) (820, 6,301) (821, 6,311)
(826, 6,343) (827, 6,353) (847, 6,553) (848, 6,563) (857, 6,653)
 
and the 10,000th: (286,069, 4,043,749)
</pre>
 
=={{header|C#}}==
{{trans|Java}}
<syntaxhighlight lang="C#">
using System;
using System.Collections.Generic;
using System.Linq;
 
public class HonakerPrimes
{
private static List<int> primes = new List<int>();
private static int honakerIndex = 0;
private static int primeIndex = 0;
 
public static void Main(string[] args)
{
SievePrimes(5_000_000);
 
Console.WriteLine("The first 50 Honaker primes (honaker index: prime index, prime):");
for (int i = 1; i <= 50; i++)
{
Console.Write($"{NextHonakerPrime()}{(i % 5 == 0 ? "\n" : " ")}");
}
for (int i = 51; i < 10_000; i++)
{
NextHonakerPrime();
}
Console.WriteLine();
Console.WriteLine($"The 10,000th Honaker prime is: {NextHonakerPrime()}");
}
 
private static HonakerPrime NextHonakerPrime()
{
honakerIndex++;
primeIndex++;
while (DigitalSum(primeIndex) != DigitalSum(primes[primeIndex - 1]))
{
primeIndex++;
}
return new HonakerPrime(honakerIndex, primeIndex, primes[primeIndex - 1]);
}
 
private static int DigitalSum(int number)
{
return number.ToString().Select(c => c - '0').Sum();
}
 
private static void SievePrimes(int limit)
{
primes.Add(2);
var halfLimit = (limit + 1) / 2;
bool[] composite = new bool[halfLimit];
for (int i = 1, p = 3; i < halfLimit; p += 2, i++)
{
if (!composite[i])
{
primes.Add(p);
for (int a = i + p; a < halfLimit; a += p)
{
composite[a] = true;
}
}
}
}
 
private class HonakerPrime
{
public int HonakerIndex { get; }
public int PrimeIndex { get; }
public int Prime { get; }
 
public HonakerPrime(int honakerIndex, int primeIndex, int prime)
{
HonakerIndex = honakerIndex;
PrimeIndex = primeIndex;
Prime = prime;
}
 
public override string ToString() => $"({HonakerIndex}: {PrimeIndex}, {Prime})";
}
}
</syntaxhighlight>
{{out}}
<pre>
The first 50 Honaker primes (honaker index: prime index, prime):
(1: 32, 131) (2: 56, 263) (3: 88, 457) (4: 175, 1039) (5: 176, 1049)
(6: 182, 1091) (7: 212, 1301) (8: 218, 1361) (9: 227, 1433) (10: 248, 1571)
(11: 293, 1913) (12: 295, 1933) (13: 323, 2141) (14: 331, 2221) (15: 338, 2273)
(16: 362, 2441) (17: 377, 2591) (18: 386, 2663) (19: 394, 2707) (20: 397, 2719)
(21: 398, 2729) (22: 409, 2803) (23: 439, 3067) (24: 446, 3137) (25: 457, 3229)
(26: 481, 3433) (27: 499, 3559) (28: 508, 3631) (29: 563, 4091) (30: 571, 4153)
(31: 595, 4357) (32: 599, 4397) (33: 635, 4703) (34: 637, 4723) (35: 655, 4903)
(36: 671, 5009) (37: 728, 5507) (38: 751, 5701) (39: 752, 5711) (40: 755, 5741)
(41: 761, 5801) (42: 767, 5843) (43: 779, 5927) (44: 820, 6301) (45: 821, 6311)
(46: 826, 6343) (47: 827, 6353) (48: 847, 6553) (49: 848, 6563) (50: 857, 6653)
 
The 10,000th Honaker prime is: (10000: 286069, 4043749)
 
</pre>
 
Line 165 ⟶ 454:
Ten thousandth: (286069, 4043749)
</pre>
 
===Without external libraries===
<syntaxhighlight lang="c++">
#include <cstdint>
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>
 
uint32_t honaker_index = 0;
uint32_t prime_index = 0;
std::vector<uint32_t> primes;
 
struct HonakerPrime {
uint32_t honaker_index, prime_index, prime;
 
std::string to_string() {
return "(" + std::to_string(honaker_index) + ": "
+ std::to_string(prime_index) + ", "
+ std::to_string(prime) + ")";
}
};
 
void sieve_primes(const uint32_t& limit) {
primes.emplace_back(2);
const uint32_t half_limit = ( limit + 1 ) / 2;
std::vector<bool> composite(half_limit);
for ( uint32_t i = 1, p = 3; i < half_limit; p += 2, ++i ) {
if ( ! composite[i] ) {
primes.emplace_back(p);
for ( uint32_t a = i + p; a < half_limit; a += p ) {
composite[a] = true;
}
}
}
}
 
uint32_t digital_sum(uint32_t number) {
uint32_t sum = 0;
while ( number > 0 ) {
sum += number % 10;
number /= 10;
}
return sum;
}
 
HonakerPrime nextHonakerPrime() {
honaker_index++;
prime_index++;
while ( digital_sum(prime_index) != digital_sum(primes[prime_index - 1]) ) {
prime_index++;
}
return HonakerPrime(honaker_index, prime_index, primes[prime_index - 1]);
}
 
int main() {
sieve_primes(5'000'000);
 
std::cout << "The first 50 Honaker primes (honaker index: prime index, prime):" << std::endl;
for ( uint32_t i = 1; i <= 50; ++i ) {
std::cout << std::setw(17) << nextHonakerPrime().to_string() << ( i % 5 == 0 ? "\n" : " " );
}
for ( uint32_t i = 51; i < 10'000; ++i ) {
nextHonakerPrime();
}
std::cout << "\n" << "The 10,000th Honaker prime is: " + nextHonakerPrime().to_string() << std::endl;
}
</syntaxhighlight>
{{ out }}
<pre>
The first 50 Honaker primes (honaker index: prime index, prime):
(1: 32, 131) (2: 56, 263) (3: 88, 457) (4: 175, 1039) (5: 176, 1049)
(6: 182, 1091) (7: 212, 1301) (8: 218, 1361) (9: 227, 1433) (10: 248, 1571)
(11: 293, 1913) (12: 295, 1933) (13: 323, 2141) (14: 331, 2221) (15: 338, 2273)
(16: 362, 2441) (17: 377, 2591) (18: 386, 2663) (19: 394, 2707) (20: 397, 2719)
(21: 398, 2729) (22: 409, 2803) (23: 439, 3067) (24: 446, 3137) (25: 457, 3229)
(26: 481, 3433) (27: 499, 3559) (28: 508, 3631) (29: 563, 4091) (30: 571, 4153)
(31: 595, 4357) (32: 599, 4397) (33: 635, 4703) (34: 637, 4723) (35: 655, 4903)
(36: 671, 5009) (37: 728, 5507) (38: 751, 5701) (39: 752, 5711) (40: 755, 5741)
(41: 761, 5801) (42: 767, 5843) (43: 779, 5927) (44: 820, 6301) (45: 821, 6311)
(46: 826, 6343) (47: 827, 6353) (48: 847, 6553) (49: 848, 6563) (50: 857, 6653)
 
The 10,000th Honaker prime is: (10000: 286069, 4043749)
</pre>
 
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|Classes,SysUtils,StdCtrls}}
Modular version of the algorithm, breaks the problem down into simple pieces, which is a standard technique for solving problems. One of the advantages is that the modules can be used in different combination to solve different aspects of the problem. In this case, it is to find the first 50 and then 10,000th Honaker.
 
<syntaxhighlight lang="Delphi">
function IsPrime(N: integer): boolean;
{Optimised prime test - about 40% faster than the naive approach}
var I,Stop: integer;
begin
if (N = 2) or (N=3) then Result:=true
else if (n <= 1) or ((n mod 2) = 0) or ((n mod 3) = 0) then Result:= false
else
begin
I:=5;
Stop:=Trunc(sqrt(N));
Result:=False;
while I<=Stop do
begin
if ((N mod I) = 0) or ((N mod (i + 2)) = 0) then exit;
Inc(I,6);
end;
Result:=True;
end;
end;
 
 
 
function GetNextPrime(var Start: integer): integer;
{Get the next prime number after Start}
{Start is passed by "reference," so the
{original variable is incremented}
begin
repeat Inc(Start)
until IsPrime(Start);
Result:=Start;
end;
 
 
function SumDigits(N: integer): integer;
{Sum the integers in a number}
var T: integer;
begin
Result:=0;
repeat
begin
T:=N mod 10;
N:=N div 10;
Result:=Result+T;
end
until N<1;
end;
 
 
function IsHonaker(I,N: integer): boolean;
{A Honaker prime is one where the sums of digits}
{of the prime and its position are equal}
begin
Result:=SumDigits(I) = SumDigits(N);
end;
 
procedure ShowHonakerPrimes(Memo: TMemo);
{Test Honaker primes}
var I, N,Cnt: integer;
var S: string;
begin
N:=0; Cnt:=0; S:='';
{Test all numbers to see if they are prime}
for I:=1 to High(integer) do
begin
N:=GetNextPrime(N);
{Test the number if it Honaker}
if IsHonaker(I,N) then
begin
{Display if Honaker}
Inc(Cnt);
S:=S+Format('(%2d%5d%5d) ',[Cnt,I,N]);
if (Cnt mod 3)=0 then S:=S+#$0D#$0A;
if Cnt>=50 then break;
end;
end;
Memo.Lines.Add('First 50 Honaker Primes');
Memo.Lines.Add(S);
Memo.Lines.Add('');
 
{Find the 10,000th Honaker}
Memo.Lines.Add('The 10,000th Honaker Primes');
N:=0; Cnt:=0;
for I:=1 to High(integer) do
begin
N:=GetNextPrime(N);
if IsHonaker(I,N) then
begin
Inc(Cnt);
if Cnt=10000 then
begin
Memo.Lines.Add(Format('(%5d %5d %5d) ',[Cnt,I,N]));
break;
end;
end;
end;
end;
 
 
</syntaxhighlight>
{{out}}
<pre>
First 50 Honaker Primes
( 1 32 131) ( 2 56 263) ( 3 88 457)
( 4 175 1039) ( 5 176 1049) ( 6 182 1091)
( 7 212 1301) ( 8 218 1361) ( 9 227 1433)
(10 248 1571) (11 293 1913) (12 295 1933)
(13 323 2141) (14 331 2221) (15 338 2273)
(16 362 2441) (17 377 2591) (18 386 2663)
(19 394 2707) (20 397 2719) (21 398 2729)
(22 409 2803) (23 439 3067) (24 446 3137)
(25 457 3229) (26 481 3433) (27 499 3559)
(28 508 3631) (29 563 4091) (30 571 4153)
(31 595 4357) (32 599 4397) (33 635 4703)
(34 637 4723) (35 655 4903) (36 671 5009)
(37 728 5507) (38 751 5701) (39 752 5711)
(40 755 5741) (41 761 5801) (42 767 5843)
(43 779 5927) (44 820 6301) (45 821 6311)
(46 826 6343) (47 827 6353) (48 847 6553)
(49 848 6563) (50 857 6653)
 
The 10,000th Honaker Primes
(10000 286069 4043749)
</pre>
 
 
=={{header|EasyLang}}==
<syntaxhighlight>
fastfunc nextprim num .
repeat
i = 2
while i <= sqrt num and num mod i <> 0
i += 1
.
until num mod i <> 0
num += 1
.
return num
.
func digsum n .
while n > 0
sum += n mod 10
n = n div 10
.
return sum
.
proc show . .
i = 1
pri = 2
while count < 50
if digsum i = digsum pri
write "(" & i & " " & pri & ") "
count += 1
.
i += 1
pri = nextprim (pri + 1)
.
.
show
</syntaxhighlight>
 
=={{header|F_Sharp|F#}}==
Line 185 ⟶ 724:
(286069, 4043749)
</pre>
 
=={{header|Factor}}==
{{works with|Factor|0.99 2022-04-03}}
Line 210 ⟶ 750:
{ 761 5801 } { 767 5843 } { 779 5927 } { 820 6301 } { 821 6311 }
{ 826 6343 } { 827 6353 } { 847 6553 } { 848 6563 } { 857 6653 }
</pre>
 
=={{header|Forth}}==
{{works with|Gforth}}
<syntaxhighlight lang="forth">5000000 constant limit
create sieve limit allot
 
: prime? ( n -- ? ) sieve + c@ 0= ;
: notprime! ( n -- ) sieve + 1 swap c! ;
 
: prime_sieve
sieve limit erase
3
begin
dup dup * limit <
while
dup prime? if
limit over dup * do
i notprime!
dup 2* +loop
then
2 +
repeat
drop ;
 
: digit_sum ( u -- u )
dup 10 < if exit then
10 /mod recurse + ;
 
: next_odd_prime ( u -- u )
begin
2 + dup prime?
until ;
 
: next_honaker_prime ( u u -- u u )
begin
swap next_odd_prime swap 1+
2dup digit_sum swap digit_sum =
until ;
 
: print_pair ( u u -- )
." (" 3 .r ." , " 4 .r ." )" ;
 
: main
prime_sieve
." First 50 Honaker primes (index, prime):" cr
3 2 0 \ prime prime-index honaker-index
begin
dup 50 <
while
-rot next_honaker_prime
2dup print_pair rot 1+
dup 5 mod 0= if cr else space then
repeat
begin
dup 10000 <
while
-rot next_honaker_prime rot 1+
repeat
drop
cr ." Ten thousandth: " print_pair ;
 
main cr bye</syntaxhighlight>
 
{{out}}
<pre>
First 50 Honaker primes (index, prime):
( 32, 131) ( 56, 263) ( 88, 457) (175, 1039) (176, 1049)
(182, 1091) (212, 1301) (218, 1361) (227, 1433) (248, 1571)
(293, 1913) (295, 1933) (323, 2141) (331, 2221) (338, 2273)
(362, 2441) (377, 2591) (386, 2663) (394, 2707) (397, 2719)
(398, 2729) (409, 2803) (439, 3067) (446, 3137) (457, 3229)
(481, 3433) (499, 3559) (508, 3631) (563, 4091) (571, 4153)
(595, 4357) (599, 4397) (635, 4703) (637, 4723) (655, 4903)
(671, 5009) (728, 5507) (751, 5701) (752, 5711) (755, 5741)
(761, 5801) (767, 5843) (779, 5927) (820, 6301) (821, 6311)
(826, 6343) (827, 6353) (847, 6553) (848, 6563) (857, 6653)
 
Ten thousandth: (286069, 4043749)
</pre>
 
Line 282 ⟶ 901:
 
10000th honaker prime is at 286069 and is 4043749</pre>
 
=={{header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">
local fn dig_sum( n as NSUInteger )
NSUInteger sum = 0
while ( n > 0 )
sum += n mod 10
n /= 10
wend
end fn = sum
 
void local fn CalculaterHonakerPrimes
NSUInteger x, y, rank = 1, place = 0
for x = 3 to _limit step 2
if ( prime(x) == 0 )
for y = x * x to _limit step x + x
prime(y) = 1
next
end if
next
printf @"The first %lu Honaker Primes ranked as \"Index: ([position], [value])\" are:\n", 50
for x = 3 to _limit step 2
if ( prime(x) == 0 )
rank++
if ( (rank mod 9) == ( x mod 9 ) )
if ( fn dig_sum(rank) == fn dig_sum(x) )
place++
if ( place <= 50 )
printf @"%4lu: (%3lu, %4lu) \b", place, rank, x
if ( place mod 5 == 0 ) then print
end if
if ( place == 10000 ) then printf @"\n The 10000th Honaker Prime is:\n %lu: (%4lu, %5lu)", place, rank, x
end if
end if
end if
next
end fn
 
window 1, @"Honakeer Primes", ( 0, 0, 780, 380 )
 
fn CalculaterHonakerPrimes
 
HandleEvents
</syntaxhighlight>
{{output}}
<pre>
The first 50 Honaker Primes ranked as "Index: ([position], [value])" are:
 
1: ( 32, 131) 2: ( 56, 263) 3: ( 88, 457) 4: (175, 1039) 5: (176, 1049)
6: (182, 1091) 7: (212, 1301) 8: (218, 1361) 9: (227, 1433) 10: (248, 1571)
11: (293, 1913) 12: (295, 1933) 13: (323, 2141) 14: (331, 2221) 15: (338, 2273)
16: (362, 2441) 17: (377, 2591) 18: (386, 2663) 19: (394, 2707) 20: (397, 2719)
21: (398, 2729) 22: (409, 2803) 23: (439, 3067) 24: (446, 3137) 25: (457, 3229)
26: (481, 3433) 27: (499, 3559) 28: (508, 3631) 29: (563, 4091) 30: (571, 4153)
31: (595, 4357) 32: (599, 4397) 33: (635, 4703) 34: (637, 4723) 35: (655, 4903)
36: (671, 5009) 37: (728, 5507) 38: (751, 5701) 39: (752, 5711) 40: (755, 5741)
41: (761, 5801) 42: (767, 5843) 43: (779, 5927) 44: (820, 6301) 45: (821, 6311)
46: (826, 6343) 47: (827, 6353) 48: (847, 6553) 49: (848, 6563) 50: (857, 6653)
 
The 10000th Honaker Prime is:
10000: (286069, 4043749)
</pre>
 
 
=={{header|Go}}==
{{trans|Wren}}
{{libheader|Go-rcu}}
<syntaxhighlight lang="go">package main
 
import (
"fmt"
"rcu"
)
 
func main() {
primes := rcu.Primes(5_000_000)
var h [][2]int
var h10000 [2]int
for i, count := 1, 0; count < 10000; i++ {
if rcu.DigitSum(i, 10) == rcu.DigitSum(primes[i-1], 10) {
count++
if count <= 50 {
h = append(h, [2]int{i, primes[i-1]})
} else if count == 10000 {
h10000 = [2]int{i, primes[i-1]}
}
}
}
fmt.Println("The first 50 Honaker primes (index, prime):\n")
for i := 0; i < 50; i++ {
fmt.Printf("(%3d, %5s) ", h[i][0], rcu.Commatize(h[i][1]))
if (i+1)%5 == 0 {
fmt.Println()
}
}
fmt.Printf("\nand the 10,000th: (%7s, %9s)\n", rcu.Commatize(h10000[0]), rcu.Commatize(h10000[1]))
}</syntaxhighlight>
 
{{out}}
<pre>
The first 50 Honaker primes (index, prime):
 
( 32, 131) ( 56, 263) ( 88, 457) (175, 1,039) (176, 1,049)
(182, 1,091) (212, 1,301) (218, 1,361) (227, 1,433) (248, 1,571)
(293, 1,913) (295, 1,933) (323, 2,141) (331, 2,221) (338, 2,273)
(362, 2,441) (377, 2,591) (386, 2,663) (394, 2,707) (397, 2,719)
(398, 2,729) (409, 2,803) (439, 3,067) (446, 3,137) (457, 3,229)
(481, 3,433) (499, 3,559) (508, 3,631) (563, 4,091) (571, 4,153)
(595, 4,357) (599, 4,397) (635, 4,703) (637, 4,723) (655, 4,903)
(671, 5,009) (728, 5,507) (751, 5,701) (752, 5,711) (755, 5,741)
(761, 5,801) (767, 5,843) (779, 5,927) (820, 6,301) (821, 6,311)
(826, 6,343) (827, 6,353) (847, 6,553) (848, 6,563) (857, 6,653)
 
and the 10,000th: (286,069, 4,043,749)
</pre>
 
=={{header|Haskell}}==
Line 299 ⟶ 1,036:
--------------------------- TEST -------------------------
main :: IO ()
main = do
putStrLn "First Fifty:\n"
>> mapM_
mapM_ (putStrLn . unwords) $
(putStrLn . unwords)
chunksOf 5 $
take 50 (show <$> honakers)chunksOf
5
(take 50 (show <$> honakers))
putStrLn "\n10Kth:\n"
)
print $ honakers !! pred 10000
>> putStrLn "\n10Kth:\n"
>> print (honakers !! pred 10000)
 
------------------------- GENERIC ------------------------
sumDigits :: Integer -> Integer
sumDigits = foldr ((+) . read . returnpure) 0 . show
 
both :: (a -> b) -> (a, a) -> (b, b)
Line 335 ⟶ 1,074:
Implementation:
<syntaxhighlight lang=J>honk=: >: =&(+/@(10&#.inv))"0 p:</syntaxhighlight>
This tests a sequence of prime indices to determine whether the corresponding prime is a honaker prime. Here, <code>>:</code> adds 1 (since J indices start with 0 and Honaker prime indices start with 1). Also, <code>p:</code> yields the prime for an index, and <code>+/@(10&#.inv)</code> computes a digital sum of a number (but not a sequence, so we use <code>"0</code> to map it onto sequences). So, <code>=&(+/@(10&#.inv))"0</code> identifies members of a pair of sequences (one on the left, the other on the right) whose digital primessums match.
 
In other words, these are equivalent:
<syntaxhighlight lang=J> (>: =&(+/@(10&#.inv))"0 p:) 30 31 32
31 32 33 (=&(+/@(10&#.inv))"0) 127 131 137
((+/3 1),(+/3 2),(+/3 3)) = ((+/1 2 7),(+/1 3 1),(+/1 3 7))
((3+1),(3+2),(3+3)) = ((1+2+7),(1+3+1),(1+3+7))
4 5 6 = 10 5 11
0 1 0
</syntaxhighlight>
 
Task example:<syntaxhighlight lang=J> (>: j. p:) 5 10$I.honk i.1e3
32j131 56j263 88j457 175j1039 176j1049 182j1091 212j1301 218j1361 227j1433 248j1571
Line 344 ⟶ 1,093:
Here, we test the first thousand primes to see which are prime indices of Honaker primes. Then <code>I.</code>converts the test results back to index form, and <code>5 10$I.</code> organizes those indices in 5 rows of 10 columns (discarding any extra). Finally, we use complex number notation to form pairs of the corresponding honaker index and prime.
 
=={{header|Java}}==
<syntaxhighlight lang="java">
import java.util.ArrayList;
import java.util.List;
 
public final class HonakerPrimes {
 
public static void main(String[] args) {
sievePrimes(5_000_000);
System.out.println("The first 50 Honaker primes (honaker index: prime index, prime):");
for ( int i = 1; i <= 50; i++ ) {
System.out.print(String.format("%17s%s", nextHonakerPrime(), ( i % 5 == 0 ? "\n" : " " ) ));
}
for ( int i = 51; i < 10_000; i++ ) {
nextHonakerPrime();
}
System.out.println();
System.out.println("The 10,000th Honaker prime is: " + nextHonakerPrime());
}
private static HonakerPrime nextHonakerPrime() {
honakerIndex += 1;
primeIndex += 1;
while ( digitalSum(primeIndex) != digitalSum(primes.get(primeIndex - 1)) ) {
primeIndex += 1;
}
return new HonakerPrime(honakerIndex, primeIndex, primes.get(primeIndex - 1));
}
private static int digitalSum(int number) {
return String.valueOf(number).chars().map( i -> i - (int) '0' ).sum();
}
 
private static void sievePrimes(int limit) {
primes.add(2);
final int halfLimit = ( limit + 1 ) / 2;
boolean[] composite = new boolean[halfLimit];
for ( int i = 1, p = 3; i < halfLimit; p += 2, i++ ) {
if ( ! composite[i] ) {
primes.add(p);
for ( int a = i + p; a < halfLimit; a += p ) {
composite[a] = true;
}
}
}
}
private static int honakerIndex = 0;
private static int primeIndex = 0;
private static List<Integer> primes = new ArrayList<Integer>();
private static record HonakerPrime(int honakerIndex, int primeIndex, int prime) {
public String toString() {
return "(" + honakerIndex + ": " + primeIndex + ", " + prime + ")";
}
}
 
}
</syntaxhighlight>
{{ out }}
<pre>
The first 50 Honaker primes (honaker index: prime index, prime):
(1: 32, 131) (2: 56, 263) (3: 88, 457) (4: 175, 1039) (5: 176, 1049)
(6: 182, 1091) (7: 212, 1301) (8: 218, 1361) (9: 227, 1433) (10: 248, 1571)
(11: 293, 1913) (12: 295, 1933) (13: 323, 2141) (14: 331, 2221) (15: 338, 2273)
(16: 362, 2441) (17: 377, 2591) (18: 386, 2663) (19: 394, 2707) (20: 397, 2719)
(21: 398, 2729) (22: 409, 2803) (23: 439, 3067) (24: 446, 3137) (25: 457, 3229)
(26: 481, 3433) (27: 499, 3559) (28: 508, 3631) (29: 563, 4091) (30: 571, 4153)
(31: 595, 4357) (32: 599, 4397) (33: 635, 4703) (34: 637, 4723) (35: 655, 4903)
(36: 671, 5009) (37: 728, 5507) (38: 751, 5701) (39: 752, 5711) (40: 755, 5741)
(41: 761, 5801) (42: 767, 5843) (43: 779, 5927) (44: 820, 6301) (45: 821, 6311)
(46: 826, 6343) (47: 827, 6353) (48: 847, 6553) (49: 848, 6563) (50: 857, 6653)
 
The 10,000th Honaker prime is: (10000: 286069, 4043749)
</pre>
 
=={{header|jq}}==
{{works with|jq}}
'''Adapted from [[#Wren|Wren]]'''
 
As with several other entries, some care must be used in choosing the
size of the prime sieve or basket.
 
'''Generic utilities'''
<syntaxhighlight lang=jq>
def digitSum: tostring | explode | map(.-48) | add;
 
def lpad($len): tostring | ($len - length) as $l | (" " * $l)[:$l] + .;
 
# Input: a positive integer
# Output: an array, $a, of length .+1 such that
# $a[$i] is $i if $i is prime, and false otherwise.
def primeSieve:
# erase(i) sets .[i*j] to false for integral j > 1
def erase($i):
if .[$i] then
reduce (range(2*$i; length; $i)) as $j (.; .[$j] = false)
else .
end;
(. + 1) as $n
| (($n|sqrt) / 2) as $s
| [null, null, range(2; $n)]
| reduce (2, 1 + (2 * range(1; $s))) as $i (.; erase($i)) ;
</syntaxhighlight>
'''The Task'''
<syntaxhighlight lang=jq>
# Output: .h gives details for the first $n Honaker primes, and
# .hmax gives details for the $max-th
def honakers($sieveLength; $n; $max):
($sieveLength|primeSieve|map(select(.))) as $primes
| { i: 1,
count: 0,
h: [],
hmax: null}
| until(.done or .i > $sieveLength;
if (.i|digitSum) == ($primes[.i-1] | digitSum)
then .count += 1
| if .count <= 50
then .h += [[.i, $primes[.i-1]]]
elif .count == $max
then .hmax = [.i, $primes[.i-1]]
| .done = true
else .
end
else .
end
| .i += 1 );
 
5e6 as $enough
| "The first 50 Honaker primes [index, prime]:",
(honakers($enough; 50; 10000)
| (.h | map( "[\(.[0]|lpad(3)), \(.[1]|lpad(4))]") | _nwise(5) | join(" ")),
"\nand the 10,000th:",
.hmax )
</syntaxhighlight>
{{output}}
<pre>
The first 50 Honaker primes [index, prime]:
[ 32, 131] [ 56, 263] [ 88, 457] [175, 1039] [176, 1049]
[182, 1091] [212, 1301] [218, 1361] [227, 1433] [248, 1571]
[293, 1913] [295, 1933] [323, 2141] [331, 2221] [338, 2273]
[362, 2441] [377, 2591] [386, 2663] [394, 2707] [397, 2719]
[398, 2729] [409, 2803] [439, 3067] [446, 3137] [457, 3229]
[481, 3433] [499, 3559] [508, 3631] [563, 4091] [571, 4153]
[595, 4357] [599, 4397] [635, 4703] [637, 4723] [655, 4903]
[671, 5009] [728, 5507] [751, 5701] [752, 5711] [755, 5741]
[761, 5801] [767, 5843] [779, 5927] [820, 6301] [821, 6311]
[826, 6343] [827, 6353] [847, 6553] [848, 6563] [857, 6653]
 
and the 10,000th:
[286069,4043749]
</pre>
 
=={{header|Julia}}==
Line 352 ⟶ 1,256:
 
""" Get the sequence of Honaker primes as tuples with their primepi values first in tuple"""
honaker(N, lim) = [(i, p) for (i, p) in enumerate(primes(lim)) if sum(digits(p)) == sum(digits(i))]
 
println("First 50 Honaker primes:")
const a = honaker(10_000, 5_000_000)
foreach(p -> print(rpad(p[2], 12), p[1] % 5 == 0 ? "\n" : ""), enumerate(a[1:50]))
println("\n$(format(a[10000][2], commas = true)) is the ",
Line 375 ⟶ 1,279:
4,043,749 is the 286,069th prime and the 10,000th Honaker prime.
</pre>
 
=={{header|Nim}}==
<syntaxhighlight lang="Nim">import std/[bitops, math, strformat, strutils]
 
type Sieve = object
data: seq[byte]
 
func `[]`(sieve: Sieve; idx: Positive): bool =
## Return value of element at index "idx".
let idx = idx shr 1
let iByte = idx shr 3
let iBit = idx and 7
result = sieve.data[iByte].testBit(iBit)
 
func `[]=`(sieve: var Sieve; idx: Positive; val: bool) =
## Set value of element at index "idx".
let idx = idx shr 1
let iByte = idx shr 3
let iBit = idx and 7
if val: sieve.data[iByte].setBit(iBit)
else: sieve.data[iByte].clearBit(iBit)
 
func newSieve(lim: Positive): Sieve =
## Create a sieve with given maximal index.
result.data = newSeq[byte]((lim + 16) shr 4)
 
func initPrimes(lim: Positive): seq[Natural] =
## Initialize the list of primes from 2 to "lim".
var composite = newSieve(lim)
composite[1] = true
for n in countup(3, sqrt(lim.toFloat).int, 2):
if not composite[n]:
for k in countup(n * n, lim, 2 * n):
composite[k] = true
result.add 2
for n in countup(3, lim, 2):
if not composite[n]:
result.add n
 
let primes = initPrimes(5_000_000)
 
func digitalSum(n: Natural): int =
## Return the digital sum of "n".
var n = n
while n != 0:
result += n mod 10
n = n div 10
 
iterator honakerPrimes(primes: seq[Natural]): tuple[pos, val: int] =
## Yield the position and value of Honaker primes from the given list of primes.
for i, n in primes:
if digitalSum(i + 1) == digitalSum(n):
yield (i + 1, n)
 
echo "List of positions and values of first 50 Honeker primes:"
var count = 0
for (pos, val) in honakerPrimes(primes):
inc count
if count <= 50:
stdout.write &"({pos:>3}, {val:>4})"
stdout.write if count mod 5 == 0: '\n' else: ' '
elif count == 10_000:
echo &"\nThe 10_000th Honeker prime number is {insertSep($val)} at position {insertSep($pos)}."
break
</syntaxhighlight>
 
{{out}}
<pre>List of positions and values of first 50 Honeker primes:
( 32, 131) ( 56, 263) ( 88, 457) (175, 1039) (176, 1049)
(182, 1091) (212, 1301) (218, 1361) (227, 1433) (248, 1571)
(293, 1913) (295, 1933) (323, 2141) (331, 2221) (338, 2273)
(362, 2441) (377, 2591) (386, 2663) (394, 2707) (397, 2719)
(398, 2729) (409, 2803) (439, 3067) (446, 3137) (457, 3229)
(481, 3433) (499, 3559) (508, 3631) (563, 4091) (571, 4153)
(595, 4357) (599, 4397) (635, 4703) (637, 4723) (655, 4903)
(671, 5009) (728, 5507) (751, 5701) (752, 5711) (755, 5741)
(761, 5801) (767, 5843) (779, 5927) (820, 6301) (821, 6311)
(826, 6343) (827, 6353) (847, 6553) (848, 6563) (857, 6653)
 
The 10_000th Honeker prime number is 4_043_749 at position 286_069.
</pre>
=={{header|Pascal}}==
==={{header|Free Pascal}}===
uses [[Extensible_prime_generator#Pascal|primsieve]] <br>
checking "numbersaplenty.com/set/Honaker_prime" for 30000101111.
<syntaxhighlight lang="pascal">
{$IFDEF FPC}{$MODE DELPHI}{$OPTIMIZATION ON,ALL}{$ENDIF}
{$IFDEF WINDOWS} {$APPTYPE CONSOLE}{$ENDIF}
uses
primsieve;
function SumOfDecDigits(n:UInt64): Uint32; forward;
const
DgtMod = 10000;
var
{$ALIGN 32}
SumDigits : array[0..DgtMod-1] of byte;
procedure Init;
var
i,
a,b,c,d : NativeUint;
Begin
i := DgtMod-1;
For a := 9 downto 0 do
For b := 9 downto 0 do
For c := 9 downto 0 do
For d := 9 downto 0 do
Begin
SumDigits[i] := a+b+c+d;
dec(i);
end;
end;
 
procedure OutSpecial(idxH,idxP,p,CntDecDgt:Uint64);
Begin
write('(',idxH:9,idxP:11,p:13);
writeln(' Digitsum :',SumOfDecDigits(p):3,' < ',CntDecDgt:3,' Count of digits )');
end;
 
procedure OutHonaker(idxH,idxP,p:Uint64);
begin
writeln('(',idxH:9,idxP:11,p:13,')');
end;
 
function SumOfDecDigits(n:UInt64): Uint32;
var
tmp: Uint64;
digit: Uint32;
Begin
result := 0;
repeat
tmp := n div DgtMod;
digit := n-tmp*DgtMod;
n := tmp;
result +=SumDigits[digit];
until n=0;
end;
 
var
idxP,p,DecDgtLimit : Uint64;
idxH,lmt,SumDgtPrime,CntDecDgt : UInt32;
Begin
init;
 
idxP := 0;
idxH := 0;
CntDecDgt := 1;
DecDgtLimit := 10;
Writeln(' First 50 Honaker primes ');
repeat
p := NextPrime;
inc(idxP);
SumDgtPrime := SumOfDecDigits(idxP);
If SumOfDecDigits(idxP) = SumOfDecDigits(p) then
begin
inc(IdxH);
if idxH<= 50 then
Begin
write('(',idxH:3,idxP:4,p:5,')');
if Idxh mod 5=0 then writeln;
end;
end;
until idxH= 50;
 
lmt := 100;
CntDecDgt := 1;
DecDgtLimit := 10;
while DecDgtLimit < p do
Begin
CntDecDgt += 1;
DecDgtLimit *= 10;
end;
Writeln;
Writeln(' n.th PrimeIdx Prime');
repeat
p := NextPrime;
inc(idxP);
IF p > DecDgtLimit then
Begin
CntDecDgt += 1;
DecDgtLimit *= 10;
end;
SumDgtPrime := SumOfDecDigits(idxP);
If SumOfDecDigits(idxP) = SumOfDecDigits(p) then
begin
inc(IdxH);
while p > DecDgtLimit do
Begin
CntDecDgt += 1;
DecDgtLimit *= 10;
end;
if SumDgtPrime < CntDecDgt then
OutSpecial(idxH,idxP,p,CntDecDgt);
if idxH = lmt then
Begin
OutHonaker(idxH,idxP,p);
lmt *= 10;
end;
end;
until lmt> 100*1000*1000;
end.</syntaxhighlight>
{{out}}
<pre>
First 50 Honaker primes
( 1 32 131)( 2 56 263)( 3 88 457)( 4 175 1039)( 5 176 1049)
( 6 182 1091)( 7 212 1301)( 8 218 1361)( 9 227 1433)( 10 248 1571)
( 11 293 1913)( 12 295 1933)( 13 323 2141)( 14 331 2221)( 15 338 2273)
( 16 362 2441)( 17 377 2591)( 18 386 2663)( 19 394 2707)( 20 397 2719)
( 21 398 2729)( 22 409 2803)( 23 439 3067)( 24 446 3137)( 25 457 3229)
( 26 481 3433)( 27 499 3559)( 28 508 3631)( 29 563 4091)( 30 571 4153)
( 31 595 4357)( 32 599 4397)( 33 635 4703)( 34 637 4723)( 35 655 4903)
( 36 671 5009)( 37 728 5507)( 38 751 5701)( 39 752 5711)( 40 755 5741)
( 41 761 5801)( 42 767 5843)( 43 779 5927)( 44 820 6301)( 45 821 6311)
( 46 826 6343)( 47 827 6353)( 48 847 6553)( 49 848 6563)( 50 857 6653)
 
n.th PrimeIdx Prime
( 100 1855 15913)
( 1000 24706 283303)
( 10000 286069 4043749)
( 100000 3066943 51168613)
( 1000000 32836375 630589303)
( 10000000 354922738 7707009643)
( 36181814 1300010120 30000101111 Digitsum : 8 < 11 Count of digits )
(100000000 3784461563 91565150519)
 
real 1m43.381s user 1m43.253s sys 0m0.000s (4,4 GHz Ryzen 5600 G) </pre>
 
=={{header|Perl}}==
{{libheader|ntheory}}
<syntaxhighlight lang="perl" line>use v5.36;
use ntheory 'nth_prime';
use List::Util <max sum>;
 
sub table ($c, @V) { my $t = $c * (my $w = 2 + max map { length } @V); ( sprintf( ('%'.$w.'s')x@V, @V) ) =~ s/.{1,$t}\K/\n/gr }
sub comma { scalar reverse join ',', unpack '(A3)*', reverse shift }
 
my($n,@honaker);
 
while () {
my $p = nth_prime(++$n);
push @honaker, [$n, $p] if (sum split '', $p) == sum split '', $n;
last if 10_000 == @honaker;
}
 
push @res, "First 50 Honaker primes (index, prime):";
push @res, table 5, map { sprintf "(%3d, %4d)", @$_ } @honaker[0..49];
 
push @res, "Ten thousandth: " . sprintf "(%s, %s)", map { comma $_ } @{$honaker[9999]};
</syntaxhighlight>
{{out}}
<pre>First 50 Honaker primes (index, prime):
( 32, 131) ( 56, 263) ( 88, 457) (175, 1039) (176, 1049)
(182, 1091) (212, 1301) (218, 1361) (227, 1433) (248, 1571)
(293, 1913) (295, 1933) (323, 2141) (331, 2221) (338, 2273)
(362, 2441) (377, 2591) (386, 2663) (394, 2707) (397, 2719)
(398, 2729) (409, 2803) (439, 3067) (446, 3137) (457, 3229)
(481, 3433) (499, 3559) (508, 3631) (563, 4091) (571, 4153)
(595, 4357) (599, 4397) (635, 4703) (637, 4723) (655, 4903)
(671, 5009) (728, 5507) (751, 5701) (752, 5711) (755, 5741)
(761, 5801) (767, 5843) (779, 5927) (820, 6301) (821, 6311)
(826, 6343) (827, 6353) (847, 6553) (848, 6563) (857, 6653)
 
Ten thousandth: (286,069, 4,043,749)</pre>
 
=={{header|Phix}}==
Line 456 ⟶ 1,622:
The 10,000th Honaker prime is the 286,069th one, which is 4,043,749.</pre>
 
Or, defining an infinite series of Honaker primes, writing our own primes function,
 
and displaying with flexible column widths, for variable sample sizes:
Or, a variant without sympy:
<syntaxhighlight lang="python">'''Honaker primes'''
 
from itertools import chain, count, islice
from functools import reduce
 
# honakers :: [Int]
 
def honakers():
'''Infinite stream of Honaker termsprimes,
tupled with their 1-based indices.
in the series of prime integers.
'''
def fp(ip):
return [digitSum(ip[0]) if== digitSum(ip[1])
digitSum(ip[0]) == digitSum(ip[1])
) else []
 
return chain.from_iterablefilter(
map(fp, zipenumerate(countprimes(1), primes())1)
)
 
Line 487 ⟶ 1,653:
0
)
 
 
# ------------------------- TEST -------------------------
# main :: IO ()
def main():
'''First 50 Honaker primes, and ten thousandth.'''
'''Test'''
 
print("First 50:\n")
print ("First 50 (prime index, Honaker) pairs:")
for xs in (
chunksOf(5)print(
listtable(5)([
str(n) for n islice(in
islice(honakers(), 50)
50])
)
)
)
):
print(" ".join(repr(x) for x in xs))
 
print("\n10Kth:\n")
Line 541 ⟶ 1,702:
else:
seen[
until(lambda x: x not in seen)(
lambda x: x +not 2in * pseen,
)(q lambda x: x + 2 * p),
q + 2 * p
)
] = p
 
 
# table :: Int -> [String] -> String
def table(n):
'''A list of strings formatted as
right-justified rows of n columns.
'''
def go(xs):
w = len(max(xs, key=len))
return '\n'.join(
' '.join(row) for row in chunksOf(n)([
s.rjust(w, ' ') for s in xs
])
)
return go
 
 
# until :: (a -> Bool) -> (a -> a) -> a -> a
def until(p, f, x):
'''The result of repeatedly applying f until p holds.
The initial seed value is x.
'''
defv go(f):= x
while not def gp(xv):
v = xf(v)
return v
while not p(v):
v = f(v)
return v
return g
return go
 
 
# MAIN ---
if __name__ == '__main__':
main()</syntaxhighlight>
</syntaxhighlight>
{{Out}}
<pre>First 50 (prime index, Honaker) pairs:
(32, 131) (56, 263) (88, 457) (175, 1039) (176, 1049)
 
(32, 131) (56, 263) (88, 457) (175, 1039) (176, 1049)
(182, 1091) (212, 1301) (218, 1361) (227, 1433) (248, 1571)
(293, 1913) (295, 1933) (323, 2141) (331, 2221) (338, 2273)
Line 582 ⟶ 1,756:
 
(286069, 4043749)</pre>
 
=={{header|Quackery}}==
 
<code>isprime</code> is defined at [[Primality by trial division#Quackery]].
 
<syntaxhighlight lang="Quackery"> [ 0 swap
[ 10 /mod
rot + swap
dup 0 = until ]
drop ] is digitsum ( n --> n )
 
[ digitsum swap digitsum = ] is ds= ( n n --> b )
 
[ 0 temp put
[] 0
[ 1+
dup isprime if
[ 1 temp tally
dup temp share
ds= if
[ dup dip
[ temp share
swap join
nested join ] ] ]
dip [ 2dup size = ]
swap until ]
drop nip temp release ] is honakers ( n --> [ )
 
50 honakers
witheach
[ echo
i^ 5 mod 4 =
if cr else sp ]
cr cr
10000 honakers -1 peek echo</syntaxhighlight>
 
{{out}}
 
<pre>[ 32 131 ][ 56 263 ][ 88 457 ][ 175 1039 ][ 176 1049 ]
[ 182 1091 ][ 212 1301 ][ 218 1361 ][ 227 1433 ][ 248 1571 ]
[ 293 1913 ][ 295 1933 ][ 323 2141 ][ 331 2221 ][ 338 2273 ]
[ 362 2441 ][ 377 2591 ][ 386 2663 ][ 394 2707 ][ 397 2719 ]
[ 398 2729 ][ 409 2803 ][ 439 3067 ][ 446 3137 ][ 457 3229 ]
[ 481 3433 ][ 499 3559 ][ 508 3631 ][ 563 4091 ][ 571 4153 ]
[ 595 4357 ][ 599 4397 ][ 635 4703 ][ 637 4723 ][ 655 4903 ]
[ 671 5009 ][ 728 5507 ][ 751 5701 ][ 752 5711 ][ 755 5741 ]
[ 761 5801 ][ 767 5843 ][ 779 5927 ][ 820 6301 ][ 821 6311 ]
[ 826 6343 ][ 827 6353 ][ 847 6553 ][ 848 6563 ][ 857 6653 ]
 
[ 286069 4043749 ]</pre>
 
=={{header|Raku}}==
Line 604 ⟶ 1,828:
 
Ten thousandth: (286069, 4043749)</pre>
=={{header|RPL}}==
{{works with|HP|49}}
≪ →STR → digits
≪ 0
1 digits SIZE '''FOR''' j
digits j DUP SUB STR→ + '''NEXT'''
≫ ≫ '<span style="color:blue">DIGSUM</span>' STO
≪ 1 { } → max primepos result
≪ 2
1 max '''FOR''' j
'''DO''' NEXTPRIME
'''UNTIL''' 'primepos' INCR <span style="color:blue">DIGSUM</span> OVER <span style="color:blue">DIGSUM</span> == '''END'''
'result' j primepos 4 PICK →V3 STO+
'''NEXT''' result
≫ ≫ '<span style="color:blue">HONAKER</span>' STO
 
50 <span style="color:blue">HONAKER</span>
 
{{out}}
<pre>
1: {[1. 32. 131.] [2. 56. 263.] [3. 88. 457.] [4. 175. 1039.] [5. 176. 1049.] [6. 182. 1091.] [7. 212. 1301.] [8. 218. 1361.] [9. 227. 1433.] [10. 248. 1571.] [11. 293. 1913.] [12. 295. 1933.] [13. 323. 2141.] [14. 331. 2221.] [15. 338. 2273.] [16. 362. 2441.] [17. 377. 2591.] [18. 386. 2663.] [19. 394. 2707.] [20. 397. 2719.] [21. 398. 2729.] [22. 409. 2803.] [23. 439. 3067.] [24. 446. 3137.] [25. 457. 3229.] [26. 481. 3433.] [27. 499. 3559.] [28. 508. 3631.] [29. 563. 4091.] [30. 571. 4153.] [31. 595. 4357.] [32. 599. 4397.] [33. 635. 4703.] [34. 637. 4723.] [35. 655. 4903.] [36. 671. 5009.] [37. 728. 5507.] [38. 751. 5701.] [39. 752. 5711.] [40. 755. 5741.] [41. 761. 5801.] [42. 767. 5843.] [43. 779. 5927.] [44. 820. 6301.] [45. 821. 6311.] [46. 826. 6343.] [47. 827. 6353.] [48. 847. 6553.] [49. 848. 6563.] [50. 857. 6653.]}
</pre>
 
=={{header|Ruby}}==
<syntaxhighlight lang="ruby" line>require 'prime'
 
honakers = Prime.each.with_index(1).lazy.select{|pr, i| pr.digits.sum == i.digits.sum}
ar = honakers.take(10_000).to_a
puts "The first 50 Honaker primes and their position:"
ar.first(50).each_slice(5){|slice| puts "%15s"*slice.size % slice}
 
puts "\nHonaker prime 10000 is %d on position %d." % ar.last</syntaxhighlight>
{{out}}
<pre>The first 50 Honaker primes:
[131, 32] [263, 56] [457, 88] [1039, 175] [1049, 176]
[1091, 182] [1301, 212] [1361, 218] [1433, 227] [1571, 248]
[1913, 293] [1933, 295] [2141, 323] [2221, 331] [2273, 338]
[2441, 362] [2591, 377] [2663, 386] [2707, 394] [2719, 397]
[2729, 398] [2803, 409] [3067, 439] [3137, 446] [3229, 457]
[3433, 481] [3559, 499] [3631, 508] [4091, 563] [4153, 571]
[4357, 595] [4397, 599] [4703, 635] [4723, 637] [4903, 655]
[5009, 671] [5507, 728] [5701, 751] [5711, 752] [5741, 755]
[5801, 761] [5843, 767] [5927, 779] [6301, 820] [6311, 821]
[6343, 826] [6353, 827] [6553, 847] [6563, 848] [6653, 857]
 
Honaker prime 10000 is 4043749 on position 286069.
</pre>
 
=={{header|Rust}}==
<syntaxhighlight lang="rust">
//includes primal = "0.2" in dependencies
 
fn digit_sum( mut number: usize) -> usize {
let mut sum : usize = 0 ;
while number != 0 {
sum += number % 10 ;
number /= 10 ;
}
sum
}
 
fn main() {
let mut count : i32 = 0 ;
let mut pos : i32 = 1 ;
println!("The first 50 Honaker primes:") ;
primal::Primes::all( ).enumerate( ).map( |( i , w )| (i + 1 , w) ).
filter( |(i , w)| digit_sum( *i ) == digit_sum( *w ) ).take( 50 ).
for_each( |(i , w )| {
count += 1 ;
print!("(p:{} ,ind:{} ,val:{}) " , pos , i, w ) ;
pos += 1 ;
if count % 3 == 0 {
println!( ) ;
}
}) ;
println!( ) ;
}</syntaxhighlight>
{{out}}
<pre>
The first 50 Honaker primes:
(p:1 ,ind:32 ,val:131) (p:2 ,ind:56 ,val:263) (p:3 ,ind:88 ,val:457)
(p:4 ,ind:175 ,val:1039) (p:5 ,ind:176 ,val:1049) (p:6 ,ind:182 ,val:1091)
(p:7 ,ind:212 ,val:1301) (p:8 ,ind:218 ,val:1361) (p:9 ,ind:227 ,val:1433)
(p:10 ,ind:248 ,val:1571) (p:11 ,ind:293 ,val:1913) (p:12 ,ind:295 ,val:1933)
(p:13 ,ind:323 ,val:2141) (p:14 ,ind:331 ,val:2221) (p:15 ,ind:338 ,val:2273)
(p:16 ,ind:362 ,val:2441) (p:17 ,ind:377 ,val:2591) (p:18 ,ind:386 ,val:2663)
(p:19 ,ind:394 ,val:2707) (p:20 ,ind:397 ,val:2719) (p:21 ,ind:398 ,val:2729)
(p:22 ,ind:409 ,val:2803) (p:23 ,ind:439 ,val:3067) (p:24 ,ind:446 ,val:3137)
(p:25 ,ind:457 ,val:3229) (p:26 ,ind:481 ,val:3433) (p:27 ,ind:499 ,val:3559)
(p:28 ,ind:508 ,val:3631) (p:29 ,ind:563 ,val:4091) (p:30 ,ind:571 ,val:4153)
(p:31 ,ind:595 ,val:4357) (p:32 ,ind:599 ,val:4397) (p:33 ,ind:635 ,val:4703)
(p:34 ,ind:637 ,val:4723) (p:35 ,ind:655 ,val:4903) (p:36 ,ind:671 ,val:5009)
(p:37 ,ind:728 ,val:5507) (p:38 ,ind:751 ,val:5701) (p:39 ,ind:752 ,val:5711)
(p:40 ,ind:755 ,val:5741) (p:41 ,ind:761 ,val:5801) (p:42 ,ind:767 ,val:5843)
(p:43 ,ind:779 ,val:5927) (p:44 ,ind:820 ,val:6301) (p:45 ,ind:821 ,val:6311)
(p:46 ,ind:826 ,val:6343) (p:47 ,ind:827 ,val:6353) (p:48 ,ind:847 ,val:6553)
(p:49 ,ind:848 ,val:6563) (p:50 ,ind:857 ,val:6653)</pre>
 
 
=={{header|Scala}}==
{{trans|Java}}
<syntaxhighlight lang="Scala">
import scala.collection.mutable.ListBuffer
 
object HonakerPrimes {
def main(args: Array[String]): Unit = {
sievePrimes(5000000)
 
println("The first 50 Honaker primes (honaker index: prime index, prime):")
for (i <- 1 to 50) {
print(f"${nextHonakerPrime()}%17s${if (i % 5 == 0) "\n" else " "}")
}
for (i <- 51 until 10000) {
nextHonakerPrime()
}
println()
println(s"The 10,000th Honaker prime is: ${nextHonakerPrime()}")
}
 
private def nextHonakerPrime(): HonakerPrime = {
honakerIndex += 1
primeIndex += 1
while (digitalSum(primeIndex) != digitalSum(primes(primeIndex - 1))) {
primeIndex += 1
}
HonakerPrime(honakerIndex, primeIndex, primes(primeIndex - 1))
}
 
private def digitalSum(number: Int): Int = {
number.toString.map(_.asDigit).sum
}
 
private def sievePrimes(limit: Int): Unit = {
primes += 2
val halfLimit = (limit + 1) / 2
val composite = Array.fill(halfLimit)(false)
var i = 1
var p = 3
while (i < halfLimit) {
if (!composite(i)) {
primes += p
var a = i + p
while (a < halfLimit) {
composite(a) = true
a += p
}
}
i += 1
p += 2
}
}
 
private var honakerIndex = 0
private var primeIndex = 0
private val primes = ListBuffer[Int]()
 
case class HonakerPrime(honakerIndex: Int, primeIndex: Int, prime: Int) {
override def toString: String = s"($honakerIndex: $primeIndex, $prime)"
}
}
</syntaxhighlight>
{{out}}
<pre>
The first 50 Honaker primes (honaker index: prime index, prime):
(1: 32, 131) (2: 56, 263) (3: 88, 457) (4: 175, 1039) (5: 176, 1049)
(6: 182, 1091) (7: 212, 1301) (8: 218, 1361) (9: 227, 1433) (10: 248, 1571)
(11: 293, 1913) (12: 295, 1933) (13: 323, 2141) (14: 331, 2221) (15: 338, 2273)
(16: 362, 2441) (17: 377, 2591) (18: 386, 2663) (19: 394, 2707) (20: 397, 2719)
(21: 398, 2729) (22: 409, 2803) (23: 439, 3067) (24: 446, 3137) (25: 457, 3229)
(26: 481, 3433) (27: 499, 3559) (28: 508, 3631) (29: 563, 4091) (30: 571, 4153)
(31: 595, 4357) (32: 599, 4397) (33: 635, 4703) (34: 637, 4723) (35: 655, 4903)
(36: 671, 5009) (37: 728, 5507) (38: 751, 5701) (39: 752, 5711) (40: 755, 5741)
(41: 761, 5801) (42: 767, 5843) (43: 779, 5927) (44: 820, 6301) (45: 821, 6311)
(46: 826, 6343) (47: 827, 6353) (48: 847, 6553) (49: 848, 6563) (50: 857, 6653)
 
The 10,000th Honaker prime is: (10000: 286069, 4043749)
 
</pre>
 
 
=={{header|Sidef}}==
<syntaxhighlight lang="ruby">func is_honaker_prime (n) {
n.is_prime && (n.sumdigits == n.prime_count.sumdigits)
}
 
say "The first 50 Honaker primes and their position:"
is_honaker_prime.first(50).each_slice(5, {|*slice|
say ("%15s"*slice.len % slice.map{ [_, .prime_count] }...)
})
 
printf("\nHonaker prime 10000 is %s on position %s.\n",
with (is_honaker_prime.nth(1e4)) {|k| (k, k.prime_count) })</syntaxhighlight>
{{out}}
<pre>The first 50 Honaker primes and their position:
[131, 32] [263, 56] [457, 88] [1039, 175] [1049, 176]
[1091, 182] [1301, 212] [1361, 218] [1433, 227] [1571, 248]
[1913, 293] [1933, 295] [2141, 323] [2221, 331] [2273, 338]
[2441, 362] [2591, 377] [2663, 386] [2707, 394] [2719, 397]
[2729, 398] [2803, 409] [3067, 439] [3137, 446] [3229, 457]
[3433, 481] [3559, 499] [3631, 508] [4091, 563] [4153, 571]
[4357, 595] [4397, 599] [4703, 635] [4723, 637] [4903, 655]
[5009, 671] [5507, 728] [5701, 751] [5711, 752] [5741, 755]
[5801, 761] [5843, 767] [5927, 779] [6301, 820] [6311, 821]
[6343, 826] [6353, 827] [6553, 847] [6563, 848] [6653, 857]
 
Honaker prime 10000 is 4043749 on position 286069.</pre>
 
=={{header|Wren}}==
{{libheader|Wren-math}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "./math" for Int
import "./fmt" for Fmt
 
Line 628 ⟶ 2,059:
i = i + 1
}
System.print("The first 50 HoneckerHonaker primes (index, prime):")
for (i in 0..49) {
Fmt.write("($3d, $,5d) ", h[i][0], h[i][1])
Line 637 ⟶ 2,068:
{{out}}
<pre>
The first 50 HoneckerHonaker primes (index, prime):
( 32, 131) ( 56, 263) ( 88, 457) (175, 1,039) (176, 1,049)
(182, 1,091) (212, 1,301) (218, 1,361) (227, 1,433) (248, 1,571)
Line 650 ⟶ 2,081:
 
and the 10,000th: (286,069, 4,043,749)
</pre>
 
=={{header|XPL0}}==
<syntaxhighlight lang "XPL0">
func IsPrime(N); \Return 'true' if N is prime
int N, I;
[if N <= 2 then return N = 2;
if (N&1) = 0 then \even >2\ return false;
for I:= 3 to sqrt(N) do
[if rem(N/I) = 0 then return false;
I:= I+1;
];
return true;
];
 
func DigSum(N); \Return sum of digits in N
int N, S;
[S:= 0;
while N do
[N:= N/10;
S:= S + rem(0);
];
return S;
];
 
int N, C, H;
[Format(5, 0);
N:= 3; C:= 1; H:= 0;
loop [if IsPrime(N) then
[C:= C+1;
if DigSum(N) = DigSum(C) then
[H:= H+1;
if H<=50 or H=10000 then
[RlOut(0, float(C));
Text(0, ": ");
RlOut(0, float(N));
if rem(H/5) = 0 then CrLf(0) else Text(0, " ");
if H = 10000 then quit;
];
];
];
N:= N+2;
];
]</syntaxhighlight>
{{out}}
<pre>
32: 131 56: 263 88: 457 175: 1039 176: 1049
182: 1091 212: 1301 218: 1361 227: 1433 248: 1571
293: 1913 295: 1933 323: 2141 331: 2221 338: 2273
362: 2441 377: 2591 386: 2663 394: 2707 397: 2719
398: 2729 409: 2803 439: 3067 446: 3137 457: 3229
481: 3433 499: 3559 508: 3631 563: 4091 571: 4153
595: 4357 599: 4397 635: 4703 637: 4723 655: 4903
671: 5009 728: 5507 751: 5701 752: 5711 755: 5741
761: 5801 767: 5843 779: 5927 820: 6301 821: 6311
826: 6343 827: 6353 847: 6553 848: 6563 857: 6653
286069: 4043749
</pre>
 
1,777

edits