How to resolve the algorithm Emirp primes step by step in the Stata programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Emirp primes step by step in the Stata programming language

Table of Contents

Problem Statement

An   emirp   (prime spelled backwards)   are primes that when reversed   (in their decimal representation)   are a different prime. (This rules out palindromic primes.)

In each list, the numbers should be in order. Invoke the (same) program once per task requirement, this will show what limit is used as the upper bound for calculating surplus (regular) primes. The specific method of how to determine if a range or if specific values are to be shown will be left to the programmer.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Emirp primes step by step in the Stata programming language

Source code in the stata programming language

emirp 1000
list in 1/20, noobs noh

  +-----+
  |  13 |
  |  17 |
  |  31 |
  |  37 |
  |  71 |
  |-----|
  |  73 |
  |  79 |
  |  97 |
  | 107 |
  | 113 |
  |-----|
  | 149 |
  | 157 |
  | 167 |
  | 179 |
  | 199 |
  |-----|
  | 311 |
  | 337 |
  | 347 |
  | 359 |
  | 389 |
  +-----+

emirp 10000
list if 7700<p & p<8000, noobs noh

  +------+
  | 7717 |
  | 7757 |
  | 7817 |
  | 7841 |
  | 7867 |
  |------|
  | 7879 |
  | 7901 |
  | 7927 |
  | 7949 |
  | 7951 |
  |------|
  | 7963 |
  +------+

emirp 1000000
list if _n==10000, noobs noh

  +--------+
  | 948349 |
  +--------+


program emirp
	args n
	qui clear
	qui mata: build(`n')
	qui save temp, replace
	qui replace p=real(strreverse(strofreal(p)))
	qui merge 1:1 p using temp, keep(3) nogen
	qui drop if real(strreverse(strofreal(p)))==p
end

mata
real colvector sieve(real scalar n) {
	real colvector a
	real scalar i,j
	if (n<2) return(J(0,1,.))
	a=J(n,1,1)
	a[1]=0
	for (i=1; i<=n; i++) {
		if (a[i]) {
			j=i*i
			if (j>n) return(select(1::n,a))
			for (; j<=n; j=j+i) a[j]=0
		}
	}
}

function build(n) {
	a=sieve(n)
	st_addobs(rows(a))
	st_addvar("long","p")
	st_store(.,1,a)
}
end


  

You may also check:How to resolve the algorithm Number reversal game step by step in the Fortran programming language
You may also check:How to resolve the algorithm Logical operations step by step in the gecho programming language
You may also check:How to resolve the algorithm Sorting algorithms/Quicksort step by step in the Joy programming language
You may also check:How to resolve the algorithm Date format step by step in the LiveCode programming language
You may also check:How to resolve the algorithm Knuth's algorithm S step by step in the OCaml programming language