Self numbers: Difference between revisions
m
→{{header|Phix}}: spotted another unnecessary test (counter!=startIndex), hoisted some loops into doneAfterAdding
m (→first 50 self numbers: added a feature to the program.) |
m (→{{header|Phix}}: spotted another unnecessary test (counter!=startIndex), hoisted some loops into doneAfterAdding) |
||
Line 900:
sequence output
function doneAfterAdding(integer interval, n)
-- Advance to the next self number in the sequence, append it to the output if required, indicate if finished.
if counter < startIndex then return false end if▼
output &= currentSelf
end for▼
return false
end function
function selfNumbers(sequence indexRange)
startIndex = indexRange[1]
Line 917 ⟶ 921:
-- Main process. Start with the single-digit odd numbers and first run.
▲ end for
-- Fast forward to the last self number before the lowest-order block containing the first number required.▼
▲ if doneAfterAdding(11) then return output end if
end for▼
atom indexDiff = 97777777778,
▲ -- lowest-order block containing the first number required.
if counter+indexDiff > startIndex then
▲ -- The highest-order blocks whose ends this is thought to handle correctly contain 97,777,777,778 self numbers.
▲ -- The difference between equivalently positioned numbers in these blocks is 1,000,000,000,001.
numericDiff = floor(numericDiff/10) + 1
▲ -- The figures for successively lower-order blocks are obtained by successively removing 7s and 0s!
counter
end if▼
▲ if (test > startIndex) then
▲ indexDiff = floor(indexDiff/10) + 1
▲ numericDiff = floor(numericDiff/10) + 1
▲ counter = test
▲ end if
▲ end if
-- Sequencing loop, per lowest-order block.
Line 948 ⟶ 944:
-- Eight ten-number runs, each at a numeric interval of 2 from the end of the previous one.
for i=1 to 8 do
if doneAfterAdding(2,1) then return output end if
end for
-- Two shorter runs, the second at an interval inversely related to their length.
integer
temp = floor(currentSelf/1000)
-- Work out a shorter run length based on the most significant digit change about to happen.
Line 962 ⟶ 955:
temp = floor(temp/10)
end while
integer interval = 2
for i=1 to 2 do
if doneAfterAdding(interval,1) then return output end if
▲ interval += (10-shorterRunLength)*13
end for
end while
|