Zelaron Gaming Forum  
Stats Arcade Portal Forum FAQ Members List Social Groups Calendar Search Today's Posts Mark Forums Read
Go Back   Zelaron Gaming Forum > The Zelaron Nexus > Science and Art > Tech Help

 
 
Thread Tools Display Modes

 
[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
 



 
Reply
Posted 2010-12-14, 06:56 AM in reply to Goodlookinguy's post "[C#,Math] Anyone think they can do this..."
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;

Last edited by WetWired; 2010-12-14 at 07:12 AM.
Old
Profile PM WWW Search
WetWired read his obituary with confusionWetWired read his obituary with confusionWetWired read his obituary with confusionWetWired read his obituary with confusion
 
 
WetWired
 



 
Reply
Posted 2010-12-14, 10:48 AM in reply to WetWired's post starting "if(intNumber==0)return 0; int mag;..."
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
Old
Profile PM WWW Search
WetWired read his obituary with confusionWetWired read his obituary with confusionWetWired read his obituary with confusionWetWired read his obituary with confusion
 
 
WetWired
 



 
Reply
Posted 2010-12-14, 01:59 PM in reply to WetWired's post starting "Really, you could use a log 2 operation..."
WetWired said: [Goto]
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.
 
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
 



 
Reply
Posted 2010-12-14, 02:44 PM in reply to Goodlookinguy's post starting "I don't know enough math to go into..."
Is this for something?
Old
Profile PM WWW Search
WetWired read his obituary with confusionWetWired read his obituary with confusionWetWired read his obituary with confusionWetWired read his obituary with confusion
 
 
WetWired
 



 
Reply
Posted 2010-12-14, 04:03 PM in reply to WetWired's post starting "Is this for something?"
WetWired said: [Goto]
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.
 
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
 



 
Reply
Posted 2010-12-15, 07:01 AM in reply to Goodlookinguy's post "[C#,Math] Anyone think they can do this..."
goodlookinGuy said:
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


Last edited by Knight Sir Rick; 2010-12-15 at 07:07 AM.
Old
Profile PM WWW Search
Knight Sir Rick enjoys the static noises of ten television sets simultaneously tuned to 412.84 MHzKnight Sir Rick enjoys the static noises of ten television sets simultaneously tuned to 412.84 MHz
 
 
Knight Sir Rick
 



 
Reply
Posted 2010-12-16, 01:25 AM in reply to Knight Sir Rick's post starting "Not enough math in this "math" thread...."
knight sir Rick said: [Goto]
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.
 
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
 



 
Reply
Posted 2010-12-18, 08:11 PM in reply to Goodlookinguy's post "[C#,Math] Anyone think they can do this..."
Holy shit, math is fucking hard.
Skurai
Old
Profile PM WWW Search
Skurai has an imagination enthroned in its own recess, incomprehensible as from darknessSkurai has an imagination enthroned in its own recess, incomprehensible as from darknessSkurai has an imagination enthroned in its own recess, incomprehensible as from darkness
 
 
Skurai
 



 
Reply
Posted 2010-12-19, 09:05 PM in reply to Skurai's post starting "Holy shit, math is fucking hard."
just think about this ... most if not all of us on this forum have taken or is taking algebra atm
Tim
I know you
said not to
deal w/ them
I didn't think
I'm lost and
I'm sorry
They Know
Run
Old
Profile PM WWW Search
jamer123 shouldn't have fed itjamer123 shouldn't have fed itjamer123 shouldn't have fed itjamer123 shouldn't have fed itjamer123 shouldn't have fed it
 
 
jamer123
 



 
Reply
Posted 2010-12-19, 09:27 PM in reply to jamer123's post starting "just think about this ... most if not..."
Algebra is srs bsns
Old
Profile PM WWW Search
!King_Amazon! simplifies with no grasp of the basics!King_Amazon! simplifies with no grasp of the basics!King_Amazon! simplifies with no grasp of the basics!King_Amazon! simplifies with no grasp of the basics!King_Amazon! simplifies with no grasp of the basics!King_Amazon! simplifies with no grasp of the basics!King_Amazon! simplifies with no grasp of the basics
 
 
!King_Amazon!
 



 
Reply
Posted 2010-12-20, 11:09 AM in reply to !King_Amazon!'s post starting "Algebra is srs bsns"
Algebra is a myth.
Skurai
Old
Profile PM WWW Search
Skurai has an imagination enthroned in its own recess, incomprehensible as from darknessSkurai has an imagination enthroned in its own recess, incomprehensible as from darknessSkurai has an imagination enthroned in its own recess, incomprehensible as from darkness
 
 
Skurai
 
 

Bookmarks

« Previous Thread | Next Thread »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules [Forum Rules]
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -6. The time now is 07:12 AM.
'Synthesis 2' vBulletin 3.x styles and 'x79' derivative
by WetWired the Unbound and Chruser
Copyright ©2002-2008 zelaron.com
Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
This site is best seen with your eyes open.