Cyclops numbers: Difference between revisions

From Rosetta Code
Content added Content deleted
(→‎{{header|REXX}}: added the computer programming language REXX.)
m (→‎{{header|REXX}}: optimized the use of only odd-length integers.)
Line 127: Line 127:
$= /*$: list of the numbers found (so far)*/
$= /*$: list of the numbers found (so far)*/
do j=101 until finds== n; L= length(j) /*$: list of the numbers found (so far)*/
do j=101 until finds== n; L= length(j) /*$: list of the numbers found (so far)*/
if L//2==0 then iterate /*Not a cyclops # (odd length)? Skip.*/
if L//2==0 then do; j= left(1, L+1, 0) /*Not a cyclops # (even length)? Skip.*/
iterate /*use a new J that has an odd # of digs*/
end
z= pos(0, j); if z\==(L+1)%2 then iterate /* " " " " (zero in mid)? " */
z= pos(0, j); if z\==(L+1)%2 then iterate /* " " " " (zero in mid)? " */
if pos(0, j, z+1)>0 then iterate /* " " " " (has two 0's)? " */
if pos(0, j, z+1)>0 then iterate /* " " " " (has two 0's)? " */
Line 145: Line 147:
idx= idx + cols /*bump the index count for the output*/
idx= idx + cols /*bump the index count for the output*/
end /*j*/
end /*j*/

if $\=='' then say center(idx, 7)"│" substr($, 2) /*possible show residual output.*/
if $\=='' then say center(idx, 7)"│" substr($, 2) /*possible show residual output.*/
say '───────┴'center("" , 1 + cols*(w+1), '─'); say; say
say '───────┴'center("" , 1 + cols*(w+1), '─'); say
return
return
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────────────────────────────────────────────────────────*/

Revision as of 02:05, 24 June 2021

Cyclops numbers is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

A cyclops number is a number with an odd number of digits that has a zero in the center, but nowhere else. They are named so in tribute to the one eyed giants Cyclops from Greek mythology.

Cyclops numbers can be found in any base. This task strictly is looking for cyclops numbers in base 10.

There are many different classifications of cyclops numbers with minor differences in characteristics. In an effort to head off a whole series of tasks with tiny variations, we will cover several variants here.


Task
  • Find and display here on this page the first 50 cyclops numbers in base 10 (all of the sub tasks are restricted to base 10).
  • Find and display here on this page the first 50 prime cyclops numbers. (cyclops numbers that are prime.)
  • Find and display here on this page the first 50 blind prime cyclops numbers. (prime cyclops numbers that remain prime when "blinded"; the zero is removed from the center.)
  • Find and display here on this page the first 50 palindromic prime cyclops numbers. (prime cyclops numbers that are palindromes.)


Stretch
  • Find and display the first cyclops number greater than ten million (10,000,000) and the index (place) in the series where it is found.
  • Find and display the first prime cyclops number greater than ten million (10,000,000) and the index (place) in the series where it is found.
  • Find and display the first blind prime cyclops number greater than ten million (10,000,000) and the index (place) in the series where it is found.
  • Find and display the first palindromic prime cyclops number greater than ten million (10,000,000) and the index (place) in the series where it is found.

(Note: there are no cyclops numbers between ten million and one hundred million, they need to have an odd number of digits)


See also


Raku

<lang perl6>use Lingua::EN::Numbers;

my @cyclops = lazy gather for 0..* -> $exp {

   (exp($exp, 10) ..^ exp($exp + 1, 10)).map: -> $start {
       next if $start.contains: 0;
       for exp($exp, 10) ..^ exp($exp + 1, 10) -> $end {
           next if $end.contains: 0;
           take $start ~ 0 ~ $end;
       }
   }

}

my @prime-cyclops = @cyclops.grep: { .is-prime };

for , @cyclops,

   'prime ',             @prime-cyclops,
   'blind prime ',       @prime-cyclops.grep( { .trans('0' => ).is-prime } ),
   'palindromic prime ', @prime-cyclops.grep( { $_ eq .flip } )
 -> $type, $iterator {
   say "\n\nFirst 50 {$type}cyclops numbers:\n" ~ $iterator[^50].batch(10)».fmt("%7d").join("\n") ~
       "\n\nFirst {$type}cyclops number > 10,000,000: " ~ comma($iterator.first: * > 1e7 ) ~
       " - at (zero based) index: " ~ comma $iterator.first: * > 1e7, :k;

}</lang>

Output:
First 50 cyclops numbers:
    101     102     103     104     105     106     107     108     109     201
    202     203     204     205     206     207     208     209     301     302
    303     304     305     306     307     308     309     401     402     403
    404     405     406     407     408     409     501     502     503     504
    505     506     507     508     509     601     602     603     604     605

First cyclops number > 10,000,000: 111,101,111 - at (zero based) index: 538,083


First 50 prime cyclops numbers:
    101     103     107     109     307     401     409     503     509     601
    607     701     709     809     907   11027   11047   11057   11059   11069
  11071   11083   11087   11093   12011   12037   12041   12043   12049   12071
  12073   12097   13033   13037   13043   13049   13063   13093   13099   14011
  14029   14033   14051   14057   14071   14081   14083   14087   15013   15017

First prime cyclops number > 10,000,000: 111,101,129 - at (zero based) index: 39,319


First 50 blind prime cyclops numbers:
    101     103     107     109     307     401     503     509     601     607
    701     709     809     907   11071   11087   11093   12037   12049   12097
  13099   14029   14033   14051   14071   14081   14083   14087   15031   15053
  15083   16057   16063   16067   16069   16097   17021   17033   17041   17047
  17053   17077   18047   18061   18077   18089   19013   19031   19051   19073

First blind prime cyclops number > 10,000,000: 111,101,161 - at (zero based) index: 11,393


First 50 palindromic prime cyclops numbers:
    101   16061   31013   35053   38083   73037   74047   91019   94049 1120211
1150511 1160611 1180811 1190911 1250521 1280821 1360631 1390931 1490941 1520251
1550551 1580851 1630361 1640461 1660661 1670761 1730371 1820281 1880881 1930391
1970791 3140413 3160613 3260623 3310133 3380833 3460643 3470743 3590953 3670763
3680863 3970793 7190917 7250527 7310137 7540457 7630367 7690967 7750577 7820287

First palindromic prime cyclops number > 10,000,000: 114,808,411 - at (zero based) index: 66

REXX

<lang rexx>/*REXX pprogram finds and displays the first fifty cyclops numbers, cyclops primes,*/ /*─────────────────────────────── blind cyclops primes, and palindromic cyclops primes.*/ parse arg n cols . /*obtain optional argument from the CL.*/ if n== | n=="," then n= 50 /*Not specified? Then use the default.*/ if cols== | cols=="," then cols= 10 /* " " " " " " */ call genP /*build array of semaphores for primes.*/ w= max(10, length( commas(@.#) ) ) /*max width of a number in any column. */

                                     first= ' the first '

isPri= 0; isBli= 0; isPal= 0; call 0 first commas(n) " cyclops numbers" isPri= 1; isBli= 0; isPal= 0; call 0 first commas(n) " prime cyclops numbers" isPri= 1; isBli= 1; isPal= 0; call 0 first commas(n) " blind prime cyclops numbers" isPri= 1; isBli= 0; isPal= 1; call 0 first commas(n) " palindromic prime cyclops numbers" exit 0 /*stick a fork in it, we're all done. */ /*──────────────────────────────────────────────────────────────────────────────────────*/ commas: parse arg ?; do jc=length(?)-3 to 1 by -3; ?=insert(',', ?, jc); end; return ? /*──────────────────────────────────────────────────────────────────────────────────────*/ 0: parse arg title /*get the title of this output section.*/

  say ' index │'center(title,   1 + cols*(w+1)     )
  say '───────┼'center(""   ,   1 + cols*(w+1), '─')
  finds= 0;                   idx= 1            /*define # of a type of cyclops # & idx*/
  $=                                            /*$: list of the numbers found (so far)*/
    do j=101  until finds== n;  L= length(j)    /*$: list of the numbers found (so far)*/
    if L//2==0  then do;    j= left(1, L+1, 0)  /*Not a cyclops # (even length)?  Skip.*/
                                       iterate  /*use a new J that has an odd # of digs*/
                     end
    z= pos(0, j);  if z\==(L+1)%2 then iterate  /* "  "    "    " (zero in mid)?    "  */
    if pos(0, j, z+1)>0           then iterate  /* "  "    "    " (has two 0's)?    "  */
    if isPri  then if \!.j        then iterate  /*Need a cyclops prime?      Then skip.*/
    if isBli  then do;   ?= space(translate(j,,0), 0)   /*Need a blind cyclops prime ? */
                         if \!.?  then iterate  /*Not a blind cyclops prime? Then skip.*/
                   end
    if isPal  then do;   r= reverse(j)          /*Need a palindromic cyclops prime?    */
                         if r\==j then iterate  /*Cyclops number not palindromic? Skip.*/
                         if \!.r  then iterate  /*Cyclops palindromic not prime?  Skip.*/
                   end
    finds= finds + 1                            /*bump the number of palindromic primes*/
    if cols<0              then iterate         /*Build the list  (to be shown later)? */
    $= $ right( commas(j), w)                   /*add a palindromic prime ──►  $  list.*/
    if finds//cols\==0     then iterate         /*have we populated a line of output?  */
    say center(idx, 7)'│'  substr($, 2);     $= /*display what we have so far  (cols). */
    idx= idx + cols                             /*bump the  index  count for the output*/
    end   /*j*/
  if $\==  then say center(idx, 7)"│"  substr($, 2)  /*possible show residual output.*/
  say '───────┴'center(""  ,   1 + cols*(w+1), '─');  say
  return

/*──────────────────────────────────────────────────────────────────────────────────────*/ genP: !.= 0; hip= 8000000 - 1 /*placeholders for primes (semaphores).*/

     @.1=2;  @.2=3;  @.3=5;  @.4=7;  @.5=11     /*define some low primes.              */
     !.2=1;  !.3=1;  !.5=1;  !.7=1;  !.11=1     /*   "     "   "    "     flags.       */
                     #=5;     s.#= @.# **2      /*number of primes so far;     prime². */
       do j=@.#+2  by 2  to hip                 /*find odd primes from here on.        */
       parse var j  -1 _; if     _==5  then iterate  /*J divisible by 5?  (right dig)*/
                            if j// 3==0  then iterate  /*"     "      " 3?             */
                            if j// 7==0  then iterate  /*"     "      " 7?             */
              do k=5  while s.k<=j              /* [↓]  divide by the known odd primes.*/
              if j // @.k == 0  then iterate j  /*Is  J ÷ X?  Then not prime.     ___  */
              end   /*k*/                       /* [↑]  only process numbers  ≤  √ J   */
       #= #+1;    @.#= j;    s.#= j*j;   !.j= 1 /*bump # of Ps; assign next P;  P²; P# */
       end          /*j*/;   return</lang>
output   when using the default inputs:
 index │                                         the first  50  cyclops numbers
───────┼───────────────────────────────────────────────────────────────────────────────────────────────────────────────
   1   │        101        102        103        104        105        106        107        108        109        201
  11   │        202        203        204        205        206        207        208        209        301        302
  21   │        303        304        305        306        307        308        309        401        402        403
  31   │        404        405        406        407        408        409        501        502        503        504
  41   │        505        506        507        508        509        601        602        603        604        605
───────┴───────────────────────────────────────────────────────────────────────────────────────────────────────────────


 index │                                      the first  50  prime cyclops numbers
───────┼───────────────────────────────────────────────────────────────────────────────────────────────────────────────
   1   │        101        103        107        109        307        401        409        503        509        601
  11   │        607        701        709        809        907     11,027     11,047     11,057     11,059     11,069
  21   │     11,071     11,083     11,087     11,093     12,011     12,037     12,041     12,043     12,049     12,071
  31   │     12,073     12,097     13,033     13,037     13,043     13,049     13,063     13,093     13,099     14,011
  41   │     14,029     14,033     14,051     14,057     14,071     14,081     14,083     14,087     15,013     15,017
───────┴───────────────────────────────────────────────────────────────────────────────────────────────────────────────


 index │                                   the first  50  blind prime cyclops numbers
───────┼───────────────────────────────────────────────────────────────────────────────────────────────────────────────
   1   │        101        103        107        109        307        401        503        509        601        607
  11   │        701        709        809        907     11,071     11,087     11,093     12,037     12,049     12,097
  21   │     13,099     14,029     14,033     14,051     14,071     14,081     14,083     14,087     15,031     15,053
  31   │     15,083     16,057     16,063     16,067     16,069     16,097     17,021     17,033     17,041     17,047
  41   │     17,053     17,077     18,047     18,061     18,077     18,089     19,013     19,031     19,051     19,073
───────┴───────────────────────────────────────────────────────────────────────────────────────────────────────────────


 index │                                the first  50  palindromic prime cyclops numbers
───────┼───────────────────────────────────────────────────────────────────────────────────────────────────────────────
   1   │        101     16,061     31,013     35,053     38,083     73,037     74,047     91,019     94,049  1,120,211
  11   │  1,150,511  1,160,611  1,180,811  1,190,911  1,250,521  1,280,821  1,360,631  1,390,931  1,490,941  1,520,251
  21   │  1,550,551  1,580,851  1,630,361  1,640,461  1,660,661  1,670,761  1,730,371  1,820,281  1,880,881  1,930,391
  31   │  1,970,791  3,140,413  3,160,613  3,260,623  3,310,133  3,380,833  3,460,643  3,470,743  3,590,953  3,670,763
  41   │  3,680,863  3,970,793  7,190,917  7,250,527  7,310,137  7,540,457  7,630,367  7,690,967  7,750,577  7,820,287
───────┴───────────────────────────────────────────────────────────────────────────────────────────────────────────────

Wren

Library: Wren-seq
Library: Wren-fmt
Library: Wren-str

<lang ecmascript>import "/math" for Int import "/seq" for Lst import "/fmt" for Fmt import "/str" for Str

var findFirst = Fn.new { |list|

   var i = 0
   for (n in list) {
       if (n > 1e7)  return [n, i]
       i = i + 1
   }

}

var ranges = [0..0, 101..909, 11011..99099, 1110111..9990999, 111101111..119101111] var cyclops = [] for (r in ranges) {

   var numDigits = r.from.toString.count
   var center = (numDigits / 2).floor
   for (i in r) {
       var digits = Int.digits(i)
       if (digits[center] == 0 && digits.count { |d| d == 0 } == 1) cyclops.add(i)
   }

}

System.print("The first 50 cyclops numbers are:") var candidates = cyclops[0...50] var ni = findFirst.call(cyclops) for (chunk in Lst.chunks(candidates, 10)) Fmt.print("$,6d", chunk) Fmt.print("\nFirst such number > 10 million is $,d at zero-based index $,d", ni[0], ni[1])

System.print("\n\nThe first 50 prime cyclops numbers are:") var primes = cyclops.where { |n| Int.isPrime(n) } candidates = primes.take(50).toList ni = findFirst.call(primes) for (chunk in Lst.chunks(candidates, 10)) Fmt.print("$,6d", chunk) Fmt.print("\nFirst such number > 10 million is $,d at zero-based index $,d", ni[0], ni[1])

System.print("\n\nThe first 50 blind prime cyclops numbers are:") var bpcyclops = [] var ppcyclops = [] for (p in primes) {

   var ps = p.toString
   var numDigits = ps.count
   var center = (numDigits/2).floor
   var noMiddle = Num.fromString(Str.delete(ps, center))
   if (Int.isPrime(noMiddle)) bpcyclops.add(p)
   if (ps == ps[-1..0]) ppcyclops.add(p)

} candidates = bpcyclops[0...50] ni = findFirst.call(bpcyclops) for (chunk in Lst.chunks(candidates, 10)) Fmt.print("$,6d", chunk) Fmt.print("\nFirst such number > 10 million is $,d at zero-based index $,d", ni[0], ni[1])

System.print("\n\nThe first 50 palindromic prime cyclops numbers are:") candidates = ppcyclops[0...50] ni = findFirst.call(ppcyclops) for (chunk in Lst.chunks(candidates, 8)) Fmt.print("$,9d", chunk) Fmt.print("\nFirst such number > 10 million is $,d at zero-based index $,d", ni[0], ni[1])</lang>

Output:
The first 50 cyclops numbers are:
     0    101    102    103    104    105    106    107    108    109
   201    202    203    204    205    206    207    208    209    301
   302    303    304    305    306    307    308    309    401    402
   403    404    405    406    407    408    409    501    502    503
   504    505    506    507    508    509    601    602    603    604

First such number > 10 million is 111,101,111 at zero-based index 538,084


The first 50 prime cyclops numbers are:
   101    103    107    109    307    401    409    503    509    601
   607    701    709    809    907 11,027 11,047 11,057 11,059 11,069
11,071 11,083 11,087 11,093 12,011 12,037 12,041 12,043 12,049 12,071
12,073 12,097 13,033 13,037 13,043 13,049 13,063 13,093 13,099 14,011
14,029 14,033 14,051 14,057 14,071 14,081 14,083 14,087 15,013 15,017

First such number > 10 million is 111,101,129 at zero-based index 39,319


The first 50 blind prime cyclops numbers are:
   101    103    107    109    307    401    503    509    601    607
   701    709    809    907 11,071 11,087 11,093 12,037 12,049 12,097
13,099 14,029 14,033 14,051 14,071 14,081 14,083 14,087 15,031 15,053
15,083 16,057 16,063 16,067 16,069 16,097 17,021 17,033 17,041 17,047
17,053 17,077 18,047 18,061 18,077 18,089 19,013 19,031 19,051 19,073

First such number > 10 million is 111,101,161 at zero-based index 11,393


The first 50 palindromic prime cyclops numbers are:
      101    16,061    31,013    35,053    38,083    73,037    74,047    91,019
   94,049 1,120,211 1,150,511 1,160,611 1,180,811 1,190,911 1,250,521 1,280,821
1,360,631 1,390,931 1,490,941 1,520,251 1,550,551 1,580,851 1,630,361 1,640,461
1,660,661 1,670,761 1,730,371 1,820,281 1,880,881 1,930,391 1,970,791 3,140,413
3,160,613 3,260,623 3,310,133 3,380,833 3,460,643 3,470,743 3,590,953 3,670,763
3,680,863 3,970,793 7,190,917 7,250,527 7,310,137 7,540,457 7,630,367 7,690,967
7,750,577 7,820,287

First such number > 10 million is 114,808,411 at zero-based index 66