View Single Post
 
[C#,Math] Anyone think they can do this better?
Reply
Posted 2010-12-13, 04:36 PM
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.
 
Work List
疲れていますから 寝むってありますね。 むずかしいです。 また、ケーキ屋で ケーキを食べていました。

I've considered being a translator, but I dunno. It feels like a lot of work. If someone gets angry then I have to deal with it, you know? I'd rather just relax.

 
Speed Test
 
Favorite Anime/Manga
#01 Clannad ~After Story~
#02 Trigun {Maximum}
#03 Koi Kaze
#04 Berserk
#05 Outlaw Star
#06 Slayers
#07 Desert Punk
#08 Spirited Away
#09 Fullmetal Alchemist
#10 Shakugan no Shana
#11 Death Note
#12 FLCL
#13 Tokyo Magnitude 8.0
#14 Toradora
#15 Gunslinger Girl

 
Anime List
Old
Profile PM WWW Search
Goodlookinguy seldom sees opportunities until they cease to beGoodlookinguy seldom sees opportunities until they cease to beGoodlookinguy seldom sees opportunities until they cease to beGoodlookinguy seldom sees opportunities until they cease to be
 
 
Goodlookinguy