How to resolve the algorithm Cyclops numbers step by step in the Wren programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Cyclops numbers step by step in the Wren programming language
Table of Contents
Problem Statement
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.
(Note: there are no cyclops numbers between ten million and one hundred million, they need to have an odd number of digits)
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Cyclops numbers step by step in the Wren programming language
Source code in the wren programming language
import "./math" for Int
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)
Fmt.tprint("$,6d", candidates, 10)
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)
Fmt.tprint("$,6d", candidates, 10)
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)
Fmt.tprint("$,6d", candidates, 10)
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)
Fmt.tprint("$,9d", candidates, 8)
Fmt.print("\nFirst such number > 10 million is $,d at zero-based index $,d", ni[0], ni[1])
You may also check:How to resolve the algorithm Filter step by step in the ALGOL W programming language
You may also check:How to resolve the algorithm Fractal tree step by step in the Logo programming language
You may also check:How to resolve the algorithm Ascending primes step by step in the Phix programming language
You may also check:How to resolve the algorithm Take notes on the command line step by step in the Fortran programming language
You may also check:How to resolve the algorithm Arithmetic/Complex step by step in the C# programming language