Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

AppGameKit Classic Chat / Mathamatics help please!

Author
Message
Scraggle
Moderator
20
Years of Service
User Offline
Joined: 10th Jul 2003
Location: Yorkshire
Posted: 20th Feb 2018 11:51 Edited at: 20th Feb 2018 12:11
Hey,

I have a functional function but it is ugly and uses two loops.
I'm pretty sure there must be an elegant mathematical way to achieve what I'm trying to achieve.

Here is the function:

What it does is return the closest value of d that goes into w without a remainder.
It works, but using two loops is ugly!
There has to be a better way - anyone want to show off and help me out?

For example
Assuming w = 1000
if d = 195 then 200 will be returned
if d = 205 then 200 will be returned
if d = 240 then 250 will be returned
Markus
Valued Member
20
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 20th Feb 2018 12:25
my team mate brain said something like this
AGK (Steam) V2017.12.12 : Windows 10 Pro 64 Bit : AMD (17.12.1) Radeon R7 265 : Mac mini OS High Sierra (10.13)
Scraggle
Moderator
20
Years of Service
User Offline
Joined: 10th Jul 2003
Location: Yorkshire
Posted: 20th Feb 2018 12:31
I appreciate you input Markus, thank you but I don't think you understood the problem fully.
Your code doesn't do the same as mine.
The function I have written works perfectly, I just don't like using the loops and I'm pretty sure there must be a more elegant way to do it.
easter bunny
11
Years of Service
User Offline
Joined: 20th Nov 2012
Playing: Dota 2
Posted: 20th Feb 2018 12:51 Edited at: 20th Feb 2018 13:09
I think this should work, I can't think of any circumstances where it won't:



I'll write a quick program to test it on a bunch of different variables and see if it works consistently and get back to you with the results

Edit: never mind, getting some bugs. For example round(100/3)=33, then round(100/33)=3 which is the result, but it is NOT a factor. Gonna experiment with some recursion now

Edit2: I think the only way is by calculating all the divisors of the number

My Games - Latest WIP - My Website: Immortal.Digital - FB - Twitter
130,000 installs with AppGameKit and counting
Scraggle
Moderator
20
Years of Service
User Offline
Joined: 10th Jul 2003
Location: Yorkshire
Posted: 20th Feb 2018 13:09
Here's my test code with along with the function

As you can see from the results - the number that is returned is the nearest number to the 'desired' number that will divide into the target
easter bunny
11
Years of Service
User Offline
Joined: 20th Nov 2012
Playing: Dota 2
Posted: 20th Feb 2018 13:21 Edited at: 20th Feb 2018 13:22
Mathematically the only way I can think of doing it is to calculate all the divisors of w and see which is closest to d. This page has a fast algorithm for finding the factors (second answer: http://www.quora.com/What-is-an-efficient-algorithm-to-find-divisors-of-any-number

However you can probably get away with a combination of my code and yours. Use mine to approximate the answer, then use brute force to find the real answer. It should provide a slight speed increase (I'm just guessing about that though, maybe it'll even be slower)

Maybe someone smarter than me here can figure out a better way of doing it mathematically, but I certainly can't think of anything.
I suspect there's a way of doing it faster using recursion but I can't figure out how. I just know a lot of work with factors and denominators is done with recursion

My Games - Latest WIP - My Website: Immortal.Digital - FB - Twitter
130,000 installs with AppGameKit and counting
Scraggle
Moderator
20
Years of Service
User Offline
Joined: 10th Jul 2003
Location: Yorkshire
Posted: 20th Feb 2018 14:06
OK, Thanks for trying Bunny.
I'll probably stick with what I have. It's fast enough for what I need, I just feel dirty using loops when I'm pretty sure there must be a nice simple bit of maths I could be using.
Bengismo
6
Years of Service
User Offline
Joined: 20th Nov 2017
Location: Yorkshire, England
Posted: 20th Feb 2018 14:10 Edited at: 20th Feb 2018 19:56
Im at work (where we dont use AppGameKit!) ...so I cant test this but...I think it should work



EDIT - TESTED it - it works as well as your original code scraggle (it should its the same) just looks a bit simpler!
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 20th Feb 2018 14:52
answer = w / round(w / d)

The division in the round function needs to result in a float, so make sure at least d is a float.

Nevermind, I see bunny had the same idea.
Tiled TMX Importer V.2
XML Parser V.2
Base64 Encoder/Decoder
Purple Token - Free online hi-score database
Legend of Zelda

"I like offending people, because I think people who get offended should be offended." - Linus Torvalds
MadBit
VIP Member
Gold Codemaster
14
Years of Service
User Offline
Joined: 25th Jun 2009
Location: Germany
Posted: 21st Feb 2018 04:35
That's what I got.
It's not much different than what you have, I put it in one loop.
Theoretically, it should be faster.
I don't know if the problem could be solved without a loop.

Share your knowledge. It\'s a way to achieve immortality. (Tenzin Gyatso)
Scraggle
Moderator
20
Years of Service
User Offline
Joined: 10th Jul 2003
Location: Yorkshire
Posted: 21st Feb 2018 15:01
Thanks MadBit.

I can't say whether or not your code works because it causes AppGameKit to crash without error.
I don't know why, I can't see anything that would cause it to crash and yet it does.
MadBit
VIP Member
Gold Codemaster
14
Years of Service
User Offline
Joined: 25th Jun 2009
Location: Germany
Posted: 21st Feb 2018 15:12 Edited at: 21st Feb 2018 15:13
sorry it is the for loop. '1 to w' not '0 to w'
and function must return f
Share your knowledge. It\'s a way to achieve immortality. (Tenzin Gyatso)
baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 21st Feb 2018 15:40 Edited at: 21st Feb 2018 16:22
This version seems a lot quicker:

The original averages 1000 runs in 0.016 secs (on my machine) and my version gets around 1000 in 0.0002 secs. You might want to run a few tests yourself on the output but it looks right to me.

EDIT: Test code

EDIT2:
Actually I get incorrect results occasionally. For 166 for example I should get 200. I need to go further than the d value... I will work on it
Using AppGameKit V2 Tier 1
baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 21st Feb 2018 16:40
Ok better version:


I've run this over 30,000 times and got the same results as yours every time. It doesn't seem to run as fast for some reason but it is still quicker.
Using AppGameKit V2 Tier 1
Bengismo
6
Years of Service
User Offline
Joined: 20th Nov 2017
Location: Yorkshire, England
Posted: 21st Feb 2018 16:41
I posted that exact same function yesterday...lol
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 21st Feb 2018 16:46
Quote: "It doesn't seem to run as fast for some reason but it is still quicker."


Come again? It's not as fast but it's quicker?
Tiled TMX Importer V.2
XML Parser V.2
Base64 Encoder/Decoder
Purple Token - Free online hi-score database
Legend of Zelda

"I like offending people, because I think people who get offended should be offended." - Linus Torvalds
baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 21st Feb 2018 16:51
Quote: "I posted that exact same function yesterday...lol"

Great minds think alike I didn't look at everyone else's solutions.

Quote: "Come again? It's not as fast but it's quicker?"

Not as fast as the previous version I posted but quicker than the OP.
Using AppGameKit V2 Tier 1
Bengismo
6
Years of Service
User Offline
Joined: 20th Nov 2017
Location: Yorkshire, England
Posted: 21st Feb 2018 20:19
Your previous version fails on various numbers , so it doesn't really matter if it was quicker or not.... as it was broken.

The one I posted (then you did as well) works fine.

The function could be put into a plugin for speed and run without the interpreter to make it much quicker. A lookup table would be another good option.
baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 22nd Feb 2018 08:53
Quote: "Your previous version fails on various numbers , so it doesn't really matter if it was quicker or not.... as it was broken."

True, after a little testing I realised it was failing on some numbers because I was going by the remainder of the original number. This only worked about 80% of the time.

Quote: "The function could be put into a plugin for speed and run without the interpreter to make it much quicker. A lookup table would be another good option."

Agreed, I had a quick go at a lookup table version and even in AppGameKit Basic it runs about 500 times faster than the original function. Just run the setup once during initialisation (assuming you will only be checking for one target number), then use "lookup[desired]" to get the answer. Alternatively the version Bengismo came up with first works fine if the target number is likely to change:
Using AppGameKit V2 Tier 1

Login to post a reply

Server time is: 2024-04-24 14:42:58
Your offset time is: 2024-04-24 14:42:58