How to resolve the algorithm Palindromic gapful numbers step by step in the Julia programming language
How to resolve the algorithm Palindromic gapful numbers step by step in the Julia programming language
Table of Contents
Problem Statement
Numbers (positive integers expressed in base ten) that are (evenly) divisible by the number formed by the first and last digit are known as gapful numbers.
Evenly divisible means divisible with no remainder.
All one─ and two─digit numbers have this property and are trivially excluded. Only numbers ≥ 100 will be considered for this Rosetta Code task.
1037 is a gapful number because it is evenly divisible by the number 17 which is formed by the first and last decimal digits of 1037.
A palindromic number is (for this task, a positive integer expressed in base ten), when the number is reversed, is the same as the original number.
For other ways of expressing the (above) requirements, see the discussion page.
All palindromic gapful numbers are divisible by eleven.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Palindromic gapful numbers step by step in the Julia programming language
This code in Julia finds and prints the last lastones
palindromic gapful numbers from 100, for values of outof
ranging from 20 to 10000000. A palindrome is a number that reads the same forward and backward, e.g. 121. A gapful number is a number whose square has a "gap" of at least one zero between its digits, e.g. 100 whose square is 10000. A palindromic gapful number is a palindrome that's also gapful, e.g. 1331 whose square is 17712671.
The code first defines a custom iterator type Palindrome
using struct
and specifies its iterator size (Base.IteratorSize
) as infinite and its element type (Base.IteratorEltype
) as a vector of Int8
. The iterate
method of the Palindrome
type generates palindrome numbers by iterating through the digits of the number, starting from the middle and working outwards. If the middle digit is less than the outer digit, the iterator returns the current palindrome and advances the middle digit. If the middle digit is equal to the outer digit, the iterator returns the current palindrome and advances both the middle and outer digits.
The isgapful
function checks if a given number is gapful by converting it to an integer using asint
and then checking if the remainder of dividing the integer by the product of its first and eleventh digits is zero.
The GapfulPalindrome
function uses Iterators.filter
to filter the Palindrome
iterator using the isgapful
function and then uses Iterators.take
to limit the number of palindromic gapful numbers generated to 10000000000.
The testpal
function generates and prints the last lastones
palindromic gapful numbers from 100 for various values of outof
. It uses multithreading to speed up the computation. The collect
function is used to collect the generated palindromic gapful numbers into an array, which is then sorted using sort!
and converted to integers using asint
. The string
function is used to convert the array of integers into a string for printing.
The output of the code is a table showing the last lastones
palindromic gapful numbers from 100 for various values of outof
. For example, for lastones = 20
and outof = 10000000
, the output is:
Last digit | Last 20 of 10000000 palindromic gapful numbers from 100
-----------|----------------------------------------------------------------------------------------------------------------
1 101 131 151 181 525 555 585 595 616 626 636 656 666 676 686 696 707 717 727 737
2 202 232 252 282 545 575 606 636 646 666 676 686 696 707 717 727 737 747 757 767
3 303 323 343 373 515 545 565 575 595 616 626 646 656 666 676 686 696 707 717 727
4 404 434 454 484 505 535 555 565 585 606 616 636 646 656 666 676 686 696 707 717
5 505 535 555 585 595 616 626 646 656 666 676 686 696 707 717 727 737 747 757 767
6 606 636 646 666 676 686 696 707 717 727 737 747 757 767 777 787 797 808 818 828
7 707 737 747 757 767 777 787 797 808 818 828 838 848 858 868 878 888 898 909 919
8 808 838 848 858 868 878 888 898 909 919 929 939 949 959 969 979 989 999 000 010
9 909 939 949 959 969 979 989 999 000 010 020 030 040 050 060 070 080 090 101 111
Source code in the julia programming language
import Base.iterate, Base.IteratorSize, Base.IteratorEltype
struct Palindrome x1::UInt8; x2::UInt8; outer::UInt8; end
Base.IteratorSize(p::Palindrome) = Base.IsInfinite()
Base.IteratorEltype(g::Palindrome) = Vector{Int8}
function Base.iterate(p::Palindrome, state=(UInt8[p.x1]))
arr, len = [p.outer; state; p.outer], length(state)
if all(c -> c == p.x2, state)
return arr, fill(p.x1, len + 1)
end
for i in (len+1)÷2:-1:1
if state[i] < p.x2
state[len - i + 1] = state[i] = state[i] + one(UInt8)
return arr, state
else
state[len - i + 1] = state[i] = p.x1
end
end
state[1] += one(UInt8)
push!(state, state[1])
return arr, state
end
asint(s) = foldl((i, j) -> 10i + j, s)
isgapful(a) = mod(asint(a), a[1] * 11) == 0
GapfulPalindrome(i) = Iterators.filter(isgapful, Iterators.take(Palindrome(0, 9, i), 100000000000))
function testpal()
for (lastones, outof) in [(20, 20), (15, 100), (10, 1000), (10, 10000), (10, 100000), (10, 1000000), (10, 10000000)]
@time begin
println("\nLast digit | Last $lastones of $outof palindromic gapful numbers from 100\n",
"-----------|----------------------------------------------------------------------------------------------------------------")
output = fill("", 9)
Threads.@threads for i in 1:9
gplist = sort!(asint.(collect(Iterators.take(GapfulPalindrome(i), outof))))
output[i] = " $i " * string(gplist[end-lastones+1:end]) * "\n"
end
foreach(print, output)
end
end
end
testpal()
You may also check:How to resolve the algorithm Associative array/Merging 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 JavaScript programming language
You may also check:How to resolve the algorithm Solve the no connection puzzle step by step in the JavaScript programming language
You may also check:How to resolve the algorithm Arithmetic/Integer step by step in the LSE64 programming language
You may also check:How to resolve the algorithm Cramer's rule step by step in the Sidef programming language