Honaker primes: Difference between revisions

m
Forth performance improvement
m (Forth performance improvement)
 
(24 intermediate revisions by 14 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 189:
[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>
 
=={{header|C++}}==
Line 258 ⟶ 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 278 ⟶ 724:
(286069, 4043749)
</pre>
 
=={{header|Factor}}==
{{works with|Factor|0.99 2022-04-03}}
Line 303 ⟶ 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 375 ⟶ 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 431 ⟶ 1,075:
<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 sums 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 438 ⟶ 1,092:
761j5801 767j5843 779j5927 820j6301 821j6311 826j6343 827j6353 847j6553 848j6563 857j6653</syntaxhighlight>
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 469 ⟶ 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}}==
Line 793 ⟶ 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'
Line 866 ⟶ 1,925:
(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 891 ⟶ 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 900 ⟶ 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)
1,777

edits