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.
|