Lover1500 Posted November 27, 2020 Posted November 27, 2020 I need a help in writhing script. Read code. a = 1 check = 8 divided = check / 2 minus = check - a if a <= divided then print(minus) end So what i want is to create an algorithm by using original check number 8. As example, Give a random number. Check it if its lower than half of 8.if it is,then show me that 8 - given number. So if a number is 1 then show me 7. If 2, show 6. If 4,show 4. If 5, then i wont use 8 as check number anymore. Because 5 is greater than half of 8(4). Now i am going use 16(2×8) as check number. So if 5 then show 11. If 7, show 9. if 8, show 8. If 9, now its greater than half of 16(8). So i will use 24(3×8) instead of 16. Yap the progress is that. Ive tried in many ways till to what i can think.with conbination of loop and table. But i couldnt. How can i create that algorithm. Im really appreciate to hear your help news. Thanks in advance all sir!
CmP Posted November 27, 2020 Posted November 27, 2020 The following solution should work for positive numbers: -- Taken from https://stackoverflow.com/a/3407254 function roundUp(num, multiple) if multiple == 0 then return num end local remainder = num % multiple if remainder == 0 then return num end return num + multiple - remainder end function calculateResult(num) local checkMinBound = num * 2 local check = roundUp(checkMinBound, 8) local result = check - num print(result) return result end -- Test cases calculateResult(1) -- 7 calculateResult(2) -- 6 calculateResult(4) -- 4 calculateResult(5) -- 11 calculateResult(7) -- 9 calculateResult(8) -- 8 calculateResult(9) -- 15 calculateResult(11) -- 13 calculateResult(15) -- 17 solution.lua
Lover1500 Posted November 28, 2020 Author Posted November 28, 2020 10 hours ago, CmP said: The following solution should work for positive numbers: -- Taken from https://stackoverflow.com/a/3407254 function roundUp(num, multiple) if multiple == 0 then return num end local remainder = num % multiple if remainder == 0 then return num end return num + multiple - remainder end function calculateResult(num) local checkMinBound = num * 2 local check = roundUp(checkMinBound, 8) local result = check - num print(result) return result end -- Test cases calculateResult(1) -- 7 calculateResult(2) -- 6 calculateResult(4) -- 4 calculateResult(5) -- 11 calculateResult(7) -- 9 calculateResult(8) -- 8 calculateResult(9) -- 15 calculateResult(11) -- 13 calculateResult(15) -- 17 Pretty perfect code sir! I need more learning in coding. I'll try. Really thank you !its worth with hundreds likes.
Question
Lover1500
I need a help in writhing script. Read code.
So what i want is to create an algorithm by using original check number 8.
As example,
Give a random number. Check it if its lower than half of 8.if it is,then show me that
8 - given number.
So if a number is 1 then show me 7.
If 2, show 6.
If 4,show 4.
If 5, then i wont use 8 as check number anymore. Because 5 is greater than half of 8(4). Now i am going use 16(2×8) as check number.
So if 5 then show 11.
If 7, show 9.
if 8, show 8.
If 9, now its greater than half of 16(8).
So i will use 24(3×8) instead of 16.
Yap the progress is that. Ive tried in many ways till to what i can think.with conbination of loop and table. But i couldnt.
How can i create that algorithm.
Im really appreciate to hear your help news. Thanks in advance all sir!
2 answers to this question
Recommended Posts
Archived
This topic is now archived and is closed to further replies.