This is a read-only snapshot of the ComputerCraft forums,
taken in April 2020.
[Question] How can I optimze my code with math?
Started by H4X0RZ, 17 April 2013 - 12:03 AMPosted 17 April 2013 - 02:03 AM
Yea, em. I think the title is self explanatory ^^
Posted 17 April 2013 - 02:09 AM
I really don't think it is……….Yea, em. I think the title is self explanatory ^^
where is the code?
what exactly is being optimised?
Posted 17 April 2013 - 02:40 AM
Where is the code?I really don't think it is……….Yea, em. I think the title is self explanatory ^^
where is the code?
what exactly is being optimised?
The code isn't finnished yet.
Because of that the code isn't public.
What exactly is beeing optimized?
I will optimize the function calling.
Yea. Do you still not understanding me?
Posted 17 April 2013 - 02:50 AM
I'm going to have to go with no… why are you trying to optimise function calls with math?Do you still not understanding me?
Posted 17 April 2013 - 03:06 AM
[Ironie]you can optimse it with loops that use nifty mathematical calculations
i think this answer is pretty self explaining [/ironie]
if you want a good answer you have to be a little bit more specific :D/>
i think this answer is pretty self explaining [/ironie]
if you want a good answer you have to be a little bit more specific :D/>
Posted 17 April 2013 - 03:20 AM
Okay, I'm a big noob in optimizing, so what can I optimize with math?
BTW what does the Modulo?
BTW what does the Modulo?
Posted 17 April 2013 - 03:29 AM
Stuff you can optimise with maths
So say 6 % 2 would return 0, as 6 / 2 is 3 times with a remainder of 0
7 % 3 would return 1, as 7 / 3 is 2 times with a remainder of 1
basically the common usages are checking if a number is a multiple of (or divisible by) another number, or to wrap a number, i.e. if i do this
For Lordlkol
- Nothing noteworthy
- Drawing
- Physics
- Other misc calculations
- Virtually everything using math
So say 6 % 2 would return 0, as 6 / 2 is 3 times with a remainder of 0
7 % 3 would return 1, as 7 / 3 is 2 times with a remainder of 1
basically the common usages are checking if a number is a multiple of (or divisible by) another number, or to wrap a number, i.e. if i do this
num = (num + num) % 26
num will always be in the range 0-25For Lordlkol
Spoiler
well apparently it took me 9 minutes to type that, I thought it was quite slow reallyyou are fast as lightning Bit :D/>
Edited on 17 April 2013 - 01:38 AM
Posted 17 April 2013 - 03:34 AM
got ninjad :D/>
you are fast as lightning Bit :D/>
to not leave nothing then the complain about being ninjad another often practised use for modulo in Computercraft is to make a turtle to things after a specific amount of moves.
Example sets a torch every 5 steps :)/>
for me it was like
1. i see the post
2. i start an answer
3. 2 seconds later message pops up that someone answered
Result in my brain = Bit is lightning fast :D/>
you are fast as lightning Bit :D/>
to not leave nothing then the complain about being ninjad another often practised use for modulo in Computercraft is to make a turtle to things after a specific amount of moves.
Example sets a torch every 5 steps :)/>
for i = 1, 100 do
turtle.forward()
if i%5 == 0 then
placetorch()
end
end
Spoiler
Ok i have seen it late cause sometimes i have to to a little bit work between reading forums :D/>for me it was like
1. i see the post
2. i start an answer
3. 2 seconds later message pops up that someone answered
Result in my brain = Bit is lightning fast :D/>
Posted 17 April 2013 - 04:12 AM
All programs are open-source. No one will steal your code. We can't help you if we can't see what you need.
Posted 17 April 2013 - 04:25 AM
Yea, I know ^^All programs are open-source. No one will steal your code. We can't help you if we can't see what you need.
But It's pain work to copy it with my phone ^^
Posted 17 April 2013 - 04:27 AM
Uhhhh, http://pastebin.comYea, I know ^^
But It's pain work to copy it with my phone ^^
Posted 17 April 2013 - 04:38 AM
Pastebin doesn't work mobile, and my Internet connection has only about 50-80 kb/s ^^Uhhhh, http://pastebin.comYea, I know ^^
But It's pain work to copy it with my phone ^^
Posted 17 April 2013 - 04:44 AM
Wait so you are doing your programming on your phone?Pastebin doesn't work mobile
If not just use the pastebin program, it will be quicker than trying to load the pastebin website, then just tell us the code, which is easy to type on a phone.
Posted 17 April 2013 - 04:50 AM
I program on my Computer o.OWait so you are doing your programming on your phone?Pastebin doesn't work mobile
If not just use the pastebin program, it will be quicker than trying to load the pastebin website, then just tell us the code, which is easy to type on a phone.
The problem with the internet is, that it's "mobile" internet and that is very slow ^^
Ok, at the moment i can tell you what the program does, okay?
If it's finnished, I will copy the code :D/>
Posted 17 April 2013 - 05:28 AM
My program allows you to use Floppy Disks as Credit Cards. :)/>
The gui looks like:
I think I never release it cuz I'm have to do so many things ^^
The gui looks like:
+-----------------------------+
| Credit Card Creator|
| Version 0.2 |
+----------------------------+
+------------------------+
Username: | |
+------------------------+
+------------------------+
Password: | |
+------------------------+
I think I never release it cuz I'm have to do so many things ^^
Posted 17 April 2013 - 05:38 AM
we cant really help you in optimizing the program
the only thing we can do without actual code is show you the built in math functions
the only thing we can do without actual code is show you the built in math functions
Posted 17 April 2013 - 07:08 AM
Okay, I finished the code a few moments ago ^^we cant really help you in optimizing the program
the only thing we can do without actual code is show you the built in math functions
It's buggy but it does his work :P/>
Spoiler
term.clear()
local Data = {}
--Functions
function header()
print(textutils.centerprint("+-------------------+", 1))
print(textutils.centerprint("|Credit Card Creator|", 2))
print(textutils.centerprinr("| Version 0.2 |", 3))
print(textutils.centerprint("+-------------------", 4))
end
function no_card()
while not fs.exists("disk") do
print(textutils.centerprint("------------------------+", 8))
print(textutils.centerprint("|Please insert Credit Card |", 9))
print(textutils.centerprint("+-------------------------+", 10))
sleep(2)
end
end
function card_isPresent_but_not_prepared()
function load()
fs.makeDir("disk/reditcards/")
sleep(1)
file = fs.open("disk/creditcards/card1.acc", "w")
file.close()
sleep(1)
return true
end
function screen()
while true do
print(textutils.centerprint(" ", 8))
print(textutils.centerprint(" ", 9))
print(textutils.centerprint(" ", 10))
print(textutils.centerprint("+--------------------------+", 8))
print(textutils.centerprint("| Preparing to Install |", 9))
print(textutils.centerprint("+--------------------------+", 10))
sleep(1)
end
end
parallel.waitForAny(load, screen)
print(textutils.centerprint(" ", 8))
print(textutils.centerprint(" ", 9))
print(textutils.centerprint(" ", 10))
print(textutils.centerprint("+--------------------------+", 8))
print(textutils.centerprint("| Finishe |", 9))
print(textutils.centerprint("+--------------------------+", 10))
sleep(3)
end
function asking_for_data()
print(textutils.centerprint(" ", 8))
print(textutils.centerprint(" ", 9))
print(textutils.centerprint(" ", 10))
tem.setCursorPos(20,6)
print("+-------------------+")
term.setCursorPos(20,7)
print("| |")
term.setCursorPos(20,8)
print("+-------------------+")
term.setCursorPos(20,10)
print("+-------------------+")
term.setCursorPos(20,11)
print("| |")
term.setCursorPos(20,12)
print("---------------------+")
term.setCursorPos(10,7)
write("Username:)
term.setCursrPos(10,1)
write("Password:")
sleep(0.5)
term.setCursorPos(21,7)
username = read()
term.setCursorPos(21,11)
password = read("*")
end
function checking()
if not username == nil and not password == nil then
table.insert("Data", username)
random_ID = (math.random(1000, 9999)
table.insert("Data", random_ID)
table.insert("Data", 0) -- This is the start money
end
end
function create_file()
pass_file = fs.open("disk/creditcards/pass1.pass", "w")
pass_file.writeLine(StrUtils.SHA1(password))
pass_file.close()
h = fs.open("disk/creditcards/card1.acc", "w")
local serialized = (textutils.serialize(Data))
encrypted = StrUtils.encrypt(serialized, password)
h.writeLine(encrypted)
h.close()
--Function Calling
header()
if not fs.exists("disk/creditcards/card1.acc") then
card_isPresent_but_not_prepared()
end
if fs.exists("disk/creditcards/card1.acc") then
asking_for_data()
end
checking()
create_file()
Posted 17 April 2013 - 11:29 AM
No math optimizations per se, but there are some code optimizations that could be made.
One would be:
Turn that into something like this:
Edit: For some reason the forum messed up the spacing. But you get the jist. Use multi-line strings as opposed to setting the cursor position every time.
Another thing you should do is localize. This makes your functions invisible to the global environment, meaning that people won't be able to access them from outside your program.
One would be:
tem.setCursorPos(20,6)
print("+-------------------+")
term.setCursorPos(20,7)
print("| |")
term.setCursorPos(20,8)
print("+-------------------+")
term.setCursorPos(20,10)
print("+-------------------+")
term.setCursorPos(20,11)
print("| |")
term.setCursorPos(20,12)
print("---------------------+")
Turn that into something like this:
term.setCursorPos(20, 6)
term.write(
[[+-------------------+
| |
+--------------------+
+--------------------+
| |
+--------------------+]]
)
Edit: For some reason the forum messed up the spacing. But you get the jist. Use multi-line strings as opposed to setting the cursor position every time.
Another thing you should do is localize. This makes your functions invisible to the global environment, meaning that people won't be able to access them from outside your program.
Posted 17 April 2013 - 11:57 AM
I don't know how this would ever work with this:
write("Username:) –Where is the ending quotation?
textutils.centerprinr("| Version 0.2 |", 3)) –prinr?
term.setCursrPos(10,1) –CursrPos?
write("Username:) –Where is the ending quotation?
textutils.centerprinr("| Version 0.2 |", 3)) –prinr?
term.setCursrPos(10,1) –CursrPos?
Posted 17 April 2013 - 01:00 PM
I don't know how this would ever work with this:
write("Username:) –Where is the ending quotation?
textutils.centerprinr("| Version 0.2 |", 3)) –prinr?
term.setCursrPos(10,1) –CursrPos?
textutils.centerprint() exists? This is news to me, I've always written my own function.
Posted 17 April 2013 - 01:04 PM
I don't know how this would ever work with this:
write("Username:) –Where is the ending quotation?
textutils.centerprinr("| Version 0.2 |", 3)) –prinr?
term.setCursrPos(10,1) –CursrPos?
textutils.centerprint() exists? This is news to me, I've always written my own function.
It isn't a function actually, now that I look.
Posted 17 April 2013 - 01:04 PM
I don't think it does. Just checked and it doesn't. http://computercraft.info/wiki/index.php?title=Textutils_%28API%29I don't know how this would ever work with this:
write("Username:) –Where is the ending quotation?
textutils.centerprinr("| Version 0.2 |", 3)) –prinr?
term.setCursrPos(10,1) –CursrPos?
textutils.centerprint() exists? This is news to me, I've always written my own function.
Posted 17 April 2013 - 01:36 PM
He could have made his own function. However, printing the function makes no sense since it should be printing the centered text in the function..
I also highly doubt cloudy or dan would add this function since it is already possible to do by ourselves..
I also highly doubt cloudy or dan would add this function since it is already possible to do by ourselves..
Posted 17 April 2013 - 02:13 PM
I also highly doubt cloudy or dan would add this function since it is already possible to do by ourselves..
So is every other function in textutils. :P/>
Posted 17 April 2013 - 02:14 PM
The center print function needs to be custom a lot of the time. Sometimes I make it to just center text other times I make it to be able to switch the colors and center the text.I also highly doubt cloudy or dan would add this function since it is already possible to do by ourselves..
So is every other function in textutils. :P/>
Posted 17 April 2013 - 02:43 PM
The center print function needs to be custom a lot of the time. Sometimes I make it to just center text other times I make it to be able to switch the colors and center the text.
I agree with this. But as to the patented "you can already do it yourself" argument, I strongly disagree. So I can do something myself: big whoop. I can rewrite ComputerCraft in C++ myself. But that doesn't mean I want to do it. And some things should be implemented just so you don't have to redo and/or redownload them every time. It may not take much time to do some of the small taks, but small amounts of time add up. Not to mention the fact that everybody ends up unnecessarily re-making things which could be created on the dev's end.
Now I'm not saying that CraftOS is bad right now in terms of needed functions. In fact, I'm pretty happy with it. However, even despite the fact that I am a big proponent of DIY for a large number of things (I still refuse to use programs that other people have made simply because I enjoy the challenge of doing it myself), I still can. not. stand. that logic. It doesn't make sense.
No offense SuicidalSTDz :)/>
Posted 17 April 2013 - 02:53 PM
Good arguement, I agree with both sides. Why? Well, because I can (when Cloudy permits it of course :P/>)
No offense taken ^_^/> I actually like it when people challenge me. It shows assertion and dominance.
No offense taken ^_^/> I actually like it when people challenge me. It shows assertion and dominance.
Posted 17 April 2013 - 07:12 PM
I think I have made this spelling misstakes on my phone(It'svreally pain when your fingers are bigger than the buttons on the VK ^^I don't know how this would ever work with this:
write("Username:) –Where is the ending quotation?
textutils.centerprinr("| Version 0.2 |", 3)) –prinr?
term.setCursrPos(10,1) –CursrPos?
Yea, centerprint is an function I found on this forum ^_^/>
Posted 18 April 2013 - 01:38 AM
Someone made it? Who? Why do they not print the text within the function?
Posted 18 April 2013 - 01:52 AM
Someone made it? Who? Why do they not print the text within the function?
From what i could find on google i would would guess he found it HERE and used it wrong :D/>
Posted 18 April 2013 - 03:04 AM
Yea, I got it there ^_^/>Someone made it? Who? Why do they not print the text within the function?
From what i could find on google i would would guess he found it HERE and used it wrong :D/>/>
but what is my misstake?
It's working :D/>
Posted 18 April 2013 - 03:56 AM
Yea, I got it there ^_^/>/>Someone made it? Who? Why do they not print the text within the function?
From what i could find on google i would would guess he found it HERE and used it wrong :D/>/>/>
but what is my misstake?
It's working :D/>/>
Then why did you post in Ask a Pro?
Posted 18 April 2013 - 05:14 AM
-snip-
Yea, I got it there ^_^/>/>/>
but what is my misstake?
It's working :D/>/>/>
I dont know how you implemented it but it should work like this
textutils.centerprint("|Credit Card Creator|", 2)
--And not
print(textutils.centerprint("|Credit Card Creator|", 2))
Posted 19 April 2013 - 04:22 AM
Cuz Iwant to know how can I optimize ItYea, I got it there ^_^/>/>/>Someone made it? Who? Why do they not print the text within the function?
From what i could find on google i would would guess he found it HERE and used it wrong :D/>/>/>/>
but what is my misstake?
It's working :D/>/>/>
Then why did you post in Ask a Pro?
Posted 19 April 2013 - 04:27 AM
I have edited the textutils API in my CC installation ^^-snip-
Yea, I got it there ^_^/>/>/>/>
but what is my misstake?
It's working :D/>/>/>/>
I dont know how you implemented it but it should work like thistextutils.centerprint("|Credit Card Creator|", 2) --And not print(textutils.centerprint("|Credit Card Creator|", 2))
Oh, thanks for the answer ^_^/>
I will correct it.