Computer Science Canada Programming C, C++, Java, PHP, Ruby, Turing, VB   Username:   Password: Wiki   Blog   Search   Turing   Chat Room  Members
The Genius Answer
Author Message
abcdefghijklmnopqrstuvwxy

Posted: Mon Mar 26, 2007 12:39 am   Post subject: The Genius Answer

I found my solution opened my eyes to mathematics, making it more interesting for me. I never had a problem with math in highschool but I also never spent my free time working out math equations either... Just never had the interest.

Last night I was overtired(6 hours of sleep in 50 hours) but my brain was thinking pristinely and I forget how it happened but I looked at a swap function using the temp variable and for some reason it bothered me that the temp was necessary. So I started brainstorming a mathematical way you could swap two integers (with the help of little programming obviously since variables in the swap context are programming specific.) The first minute yielded that it might be impossible to do it, the second minute gave me hope, and then I remember I had something put together within 10 minutes that turned out to be valid.

Here's what I came up with... by the way thanks to you mods who completely ruined my quiz I made earlier.

 code: int swap(int& x, int& y) {    if (x > y) {       x = x - y;       y = (x+y) + y;       y = y + x/2;  //I'm using the difference beteween the orignal numbers to calculate the orignal x.       x = y - x;     }     else {       y = y - x;       x = (x+y) + x;       x = x + y/2;  //I'm using the difference between the original numbers to calculate the original y.       y = x - y;     } }

of course this can be simplified to:
 code: int swap(int& x, int& y) {       x = x - y;       y = x + y;       x = y - x; }

Doing this gave me a new outlook towards math, I realized that I actually like solving problems with it because it's so cool
Sponsor
Sponsor

bugzpodder

Posted: Mon Mar 26, 2007 12:52 am   Post subject: RE:The Genius Answer

Try this: x^=y^=x^=y;
abcdefghijklmnopqrstuvwxy

Posted: Mon Mar 26, 2007 12:54 am   Post subject: RE:The Genius Answer

okay... But yeah too bad you replied cause i changed my original swap functions cause i figured some lines weren't needed but then i just realized they are (i'm so rusty on math i factored it out wrong) So here is the originals that do work..

 code: int swap(int& x, int& y) {    if (x > y) {       x = x - y;       y = 2(x+y) + 2y;       y = y/4;       y = y + x/2;       x = y - x;     }     else {       y = y -x;       x = 2(x+y) + 2y;       x = x/4;       x = y + x/2;       x = y - x;     } }
zylum

Posted: Mon Mar 26, 2007 12:57 am   Post subject: RE:The Genius Answer

or you could do a = a ^ b ^ (b = a) (works in both c and java)
Hikaru79

Posted: Mon Mar 26, 2007 1:16 am   Post subject: Re: The Genius Answer

Good job, alphabet. (Mind if I call you that?)

In all seriousness, there is often a definite line that is crossed when you didn't understand or appreciate something before, and suddenly you are made aware of its beauty and potential. Math is a great thing to be appreciative of. I hope your revelation leads you to a lot more personal discoveries.

That being said, I hope you realize why your earlier 'quiz' comes across as unbelievably arrogant; you may not have intended it that way, but thats how it was recieved. A lot of the members here are incredibly accomplished and very knowledgable, and questioning their "genius" based on their willingness or ability to solve a problem you pose is a little presumptious. In fact, most of the active members here are University students pursuing higher education in mathematics and computer science; they've (we've) realized and appreciated this beauty long ago. While we're very glad you're "one of us now" () you didn't invent the field. It was especially insulting when you implied that some of the posters may have cheated. Not only is that attacking one's character, but its also a bit funny since the problem itself isn't that difficult. Most math/CS majors would be expected to arrive at this solution pretty quickly, on their own, in a test setting. It seems like a two or three mark question on a midterm. Very neat, of course, when you first discover it, but its not nice to imply that since you found it difficult, anyone who didn't is either cheating or lying. But that's probably not how you intended to come across; sensitive egoes is all, I bet.

I say we put the unpleasantness behind us now, and say "Welcome aboard" What grade are you in? Tell us a bit about yourself (unless you did on a thread earlier and I missed it...)
 Display posts from previous: All Posts1 Day7 Days2 Weeks1 Month3 Months6 Months1 Year Oldest FirstNewest First

Page 1 of 1  [ 5 Posts ]
 Jump to:  Select a forum  CompSci.ca ------------ - Network News - General Discussion     General Forums   -----------------   - Hello World   - Featured Poll   - Contests     Contest Forums   -----------------   - DWITE   - [FP] Contest 2006/2008   - [FP] 2005/2006 Archive   - [FP] 2004/2005 Archive   - Off Topic     Lounges   ---------   - User Lounge   - VIP Lounge     Programming -------------- - General Programming     General Programming Forums   --------------------------------   - Functional Programming   - Logical Programming   - C     C   --   - C Help   - C Tutorials   - C Submissions   - C++     C++   ----   - C++ Help   - C++ Tutorials   - C++ Submissions   - Java     Java   -----   - Java Help   - Java Tutorials   - Java Submissions   - Ruby     Ruby   -----   - Ruby Help   - Ruby Tutorials   - Ruby Submissions   - Turing     Turing   --------   - Turing Help   - Turing Tutorials   - Turing Submissions   - PHP     PHP   ----   - PHP Help   - PHP Tutorials   - PHP Submissions   - Python     Python   --------   - Python Help   - Python Tutorials   - Python Submissions   - Visual Basic and Other Basics     VB   ---   - Visual Basic Help   - Visual Basic Tutorials   - Visual Basic Submissions     Education ----------- - Student Life   Graphics and Design ----------------------- - Web Design     Web Design Forums   ---------------------   - (X)HTML Help   - (X)HTML Tutorials   - Flash MX Help   - Flash MX Tutorials   - Graphics     Graphics Forums   ------------------   - Photoshop Tutorials   - The Showroom   - 2D Graphics   - 3D Graphics     Teams ------ - dTeam Public

 Style: Appalachia blueSilver eMJay subAppalachia subBlue subCanvas subEmjay subGrey subSilver subVereor Search: