Zelaron Gaming Forum

Zelaron Gaming Forum (http://zelaron.com/forum/index.php)
-   Tech Help (http://zelaron.com/forum/forumdisplay.php?f=329)
-   -   [C#,Math] Anyone think they can do this better? (http://zelaron.com/forum/showthread.php?t=50454)

Goodlookinguy 2010-12-13 04:36 PM

[C#,Math] Anyone think they can do this better?
 
I tried for an answer at Yahoo! answers and figured no one would answer. Which was exactly what happened. So I'm here because I know that some of the most brilliant mathematical minds I've met are here. ;) Anyways, here's me quoting, well, me. For the most part anyway.

Quote:

I'm trying to see if there's an easier way, mathematically, to take in a number, and then get it's output based on an adding system of what I have written below. Going in order, adding each time.

256, add 1 time
128, add 1 time
64, add 2 times
32, add 4 times
16, add 8 times
8, add 16 times
4, add 32 times
2, add 64 times
1, add 128 times

Examples
1 would be 256 + 256
2 would be 384 + 128
3 would be 448 + 64
4 would be 512 + 64
5 would be 544 + 32
...
50 would be 968

Here's the code I made in C# to do it. But I feel having to run through the loops for bigger numbers, for instance, would take a bit of strain on certain computers.

Code:

private int Generate(int intNumber)
{
    int intAddTo2 = 0;
    int intOutput = 0;

    if (intNumber != 0) {
        for (int intAddTo = 256, cnt = 0; intNumber > cnt; cnt++) {
            intOutput += intAddTo;
            if (intAddTo2 % 128 == 0) {
                intAddTo /= 2;
                intAddTo2 = 0;
            }
            intAddTo2 += intAddTo;
        }
    }
    else {
        intOutput = 0;
    }
    return intOutput;
}


I'll accept an answer in any language someone writes an answer in, within the boundaries of a high level programming language, and not Brain Fuck or anything like it. That is, if there is a better way to do this. In my head, I always believe there's a better way to do something. For this though, I think I may have hit the pinnacle of a way to do this.

WetWired 2010-12-14 06:56 AM

Code:

if(intNumber==0)return 0;
int mag;
for(mag=7;mag>=0;mag--){
  if((1<<mag)&(intNumber-1)){
    break;
  }
}
mag++;
return 128*mag+(1<<(8-mag))*intNumber;


WetWired 2010-12-14 10:48 AM

Really, you could use a log 2 operation then take the integer part instead of using the for loop, but then you may have to deal with floating point precision issues. That, along with swapping the left shift with an exponent operation would make the solution entirely mathmatical

Goodlookinguy 2010-12-14 01:59 PM

Quote:

Originally Posted by WetWired (Post 692465)
Really, you could use a log 2 operation then take the integer part instead of using the for loop, but then you may have to deal with floating point precision issues. That, along with swapping the left shift with an exponent operation would make the solution entirely mathematical

I don't know enough math to go into dealing with a semi-complex algorithm like that.

The solution you gave was more than enough. Took me a good 45-minutes to an hour to understand how your solution works. I had previously tried to figure out a solution using binary shifting and such, but failed. You nailed it though. So really, thank you so much. I really thought there was no better way to pull this off. I think I may have gotten a bit rusty from not being able to write code as often as I used to.

WetWired 2010-12-14 02:44 PM

Is this for something?

Goodlookinguy 2010-12-14 04:03 PM

Quote:

Originally Posted by WetWired (Post 692468)
Is this for something?

It is indeed. I like to play around with my own private server of Perfect World, among other games. Because I have more fun editing, designing, and figuring out how these things work than I do the game themselves. So I'm in the process of making a hex item data generator. One of the patterns I caught onto was one that started with a base number of 16000, then did the pattern I described. I then made what I had previously written. However, I felt it was too many loops, which is why I thought it could be done better.

I'm making the program freeware, so that's why I didn't want it to be too heavy when it generates data. Considering it could be a very wide-range of users; And all of the other things it has to calculate before the output. That's all.

Knight Sir Rick 2010-12-15 07:01 AM

Quote:

Originally Posted by goodlookinGuy
I'm trying to see if there's an easier way, mathematically [...]


Not enough math in this "math" thread. Anyway, I found you a closed-form function for your problem:



where is the discrete Heaviside step function defined as


Goodlookinguy 2010-12-16 01:25 AM

Quote:

Originally Posted by knight sir Rick (Post 692478)
Not enough math in this "math" thread. Anyway, I found you a closed-form function for your problem:



where is the discrete Heaviside step function defined as



While I understand what you wrote. Converting it to code it seems like it would take longer to compute than WW's answer. Considering for each sigma, there's a for loop that must be run. As well as calls to the function H.

For a completely math based, non-linear, answer though, very impressive. You compressed it into two functions worth. Still much better than my original way of doing it.

------------
Also, what I meant by the "[Math...]" and "...mathematically..." was just because it isn't just some regular programming stuff. It requires a bit of math knowledge to try and make something that works.

Skurai 2010-12-18 08:11 PM

Holy shit, math is fucking hard.

jamer123 2010-12-19 09:05 PM

just think about this ... most if not all of us on this forum have taken or is taking algebra atm

!King_Amazon! 2010-12-19 09:27 PM

Algebra is srs bsns

Skurai 2010-12-20 11:09 AM

Algebra is a myth.


All times are GMT -6. The time now is 04:01 AM.

Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
This site is best seen with your eyes open.