How to resolve the algorithm Circular primes step by step in the jq programming language

Published on 12 May 2024 09:40 PM
#Jq

How to resolve the algorithm Circular primes step by step in the jq programming language

Table of Contents

Problem Statement

A circular prime is a prime number with the property that the number generated at each intermediate step when cyclically permuting its (base 10) digits will also be prime. For example: 1193 is a circular prime, since 1931, 9311 and 3119 are all also prime. Note that a number which is a cyclic permutation of a smaller circular prime is not considered to be itself a circular prime. So 13 is a circular prime, but 31 is not.

A repunit (denoted by R) is a number whose base 10 representation contains only the digit 1. For example: R(2) = 11 and R(5) = 11111 are repunits.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Circular primes step by step in the jq programming language

Source code in the jq programming language

def is_circular_prime:
    def circle: range(0;length) as $i | .[$i:] + .[:$i];
    tostring as $s
    | [$s|circle|tonumber] as $c
    | . == ($c|min) and all($c|unique[]; is_prime);
 
def circular_primes:
   2, (range(3; infinite; 2) | select(is_circular_prime));

# Probably only useful with unbounded-precision integer arithmetic:
def repunits:
  1 | recurse(10*. + 1);

limit(19; circular_primes)

last(limit(19; circular_primes)) as $max
| limit(4; repunits | select(. > $max and is_prime))
| "R(\(tostring|length))"

  

You may also check:How to resolve the algorithm Loops/Do-while step by step in the Befunge programming language
You may also check:How to resolve the algorithm Determine if a string is numeric step by step in the Java programming language
You may also check:How to resolve the algorithm Faulhaber's triangle step by step in the C programming language
You may also check:How to resolve the algorithm String matching step by step in the MATLAB / Octave programming language
You may also check:How to resolve the algorithm Four is magic step by step in the Nim programming language