Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
alphabet = "0123456789AB"
function getDigitValue(digit)
for i = 1, 12 do
if digit == string.sub(alphabet, i, i) then return (i-1) end
end
return -1
end
function decToDuodec(dec)
duodec = ""
while dec > 0 do
duodec = string.sub(alphabet, (dec%12)+1, (dec%12)+1) .. duodec
dec = math.floor(dec / 12)
end
return duodec
end
function duodecToDec(duodec)
dec = 0
for i = #duodec, 1, -1 do
value = getDigitValue(string.sub(duodec, i, i))
if value == -1 then
return -1
else
dec = dec + value*math.pow(12, #duodec-i)
end
end
return math.floor(dec)
end
function calcNextValue(currVal)
currDec = duodecToDec(currVal)
prod = 1
for i = 1, #currVal do
value = getDigitValue(string.sub(currVal, i, i))
if value == -1 then
return -1
end
if value ~= 0 then
prod = prod * value
end
end
return decToDuodec(currDec + prod)
end
currVal = "X" --zamień X na poprawną liczbę (bez usuwania cudzysłowu)
nextVal = calcNextValue(currVal)
if nextVal == -1 then
io.write("Wprowadzony ciąg nie jest poprawną liczbą w systemie dwunastkowym.")
else
io.write("Następna liczba to: "..nextVal..".")
end
Advertisements
We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.
AcceptLearn more