Brain Teaser (Computer related)

2lazybutsmart2lazybutsmart Member Posts: 1,119
Ok guys;

The following are 2 number series I came by today. Continue the series an additional 5 times for each pattern.


1) 0,1,1,2,3,5,8,13,...

2)
a- 1,4,9,16,25,...
b- 1,8,27,64,...
c- 1,4,9,256,...


....

2lbs.
Exquisite as a lily, illustrious as a full moon,
Magnanimous as the ocean, persistent as time.

Comments

  • WebmasterWebmaster Admin Posts: 10,292 Admin
    1) 21

    2)
    a. 36
    b. 125
    c. 3125

    icon_profileright.gif
  • 2lazybutsmart2lazybutsmart Member Posts: 1,119
    Oh well...there goes our quiz Johan.

    OK. Based on the same quiz: You're provided with 3 variables:

    a which equals 0, b which equals 1, and c which equals 0.

    Now what is the logic for printing series #1 using the three variables mentioned above.

    (I'm not assuming everybody knows programming; but this can be done with simple equation skills).

    icon_wink.gif
    ....2lbs.
    Exquisite as a lily, illustrious as a full moon,
    Magnanimous as the ocean, persistent as time.
  • WebmasterWebmaster Admin Posts: 10,292 Admin
    icon_confused.gif Actually Abdul, I posted the answers without explanations so the series can be continued. icon_wink.gif

    I'll pass on the programming lesson, but I wonder if there is only one possible correct answer to it.
  • 2lazybutsmart2lazybutsmart Member Posts: 1,119
    that's right. I didn't notice that. icon_lol.gif

    Well then: the question rephrased: Continue the series an additional 5 times for each pattern.

    OK Johan, I know there's never only one correct answer when it comes to coding ( icon_wink.gif ), but the tough part is usually giving the logic that will do something using the least possible steps. Keep in mind that your core logic cannot exceed 3 lines ( icon_rolleyes.gif arrrghh).

    And to tighten-up the knot a bit for you: your 3 lines must print the series in question "1" from 0. You can't start from 1.

    Abdul.
    Exquisite as a lily, illustrious as a full moon,
    Magnanimous as the ocean, persistent as time.
  • JOblessELementJOblessELement Member Posts: 134
    Simple fibonacci series. Here's pseudocode:

    print a, then b;
    for (int i = 3; i <= numberOfTimes; i++) {
    c = a + b; print c;
    a = b; b = c;}
    I am free of all prejudices. I hate everyone equally.
  • Kevin57Kevin57 Member Posts: 16 ■□□□□□□□□□
    2a= 6,7,8,910 squared
    2b= 5,6,7,8,9 cubed
  • 2lazybutsmart2lazybutsmart Member Posts: 1,119
    OK. Anybody gonna try this out:

    *****
    -****
    --***
    ---**
    ----*
    Write a few lines of code (in whatever language; but perferably C) to print the above image. (the hyphens (-) shouldn't be included in the code. The hyphen means a blank space. Couldn't get the spaces to show up properly icon_cry.gif don't know why).

    2lbs.
    Exquisite as a lily, illustrious as a full moon,
    Magnanimous as the ocean, persistent as time.
  • JOblessELementJOblessELement Member Posts: 134
    Here ya go :)
    for (int i = 0; i< 5; i++)
    {
        // print space
        for (int j = i; j>0; j--) 
            cout << " ";
        // print *'s
        for (int k = 5-i; k>0; k--) 
            cout << "*";
    }
    




    It's 3 in the morning and yes I know, I'm a loser :P
    I am free of all prejudices. I hate everyone equally.
  • SartanSartan Inactive Imported Users Posts: 152
    Couldn't get the spaces to show up properly icon_cry.gif don't know why).

    Just encapsulate them in the "code" format:
    *****
     ****
      ***
       **
        *
    
    

    heh.. guess not :/
    Network Tech student, actively learning Windows 2000, Linux, Cisco, Cabling & Internet Security.
  • 2lazybutsmart2lazybutsmart Member Posts: 1,119
    joblesselement: that looks great. I'm not that good in c++ but it's more or less much like C. anyways, your logic looks neat, just couldn't understand what the "cout" means. Is that like the printf in C, or did you mean "count"?

    OK. Looks like we need some tough stuff here. Here's another one:

    1
    ----121
    ---12321
    -1234321
    123454321
    123454321
    -1234321
    ---12321
    ----121
    1


    icon_wink.gif
    Exquisite as a lily, illustrious as a full moon,
    Magnanimous as the ocean, persistent as time.
  • 2lazybutsmart2lazybutsmart Member Posts: 1,119
    Sartan wrote:
    Couldn't get the spaces to show up properly icon_cry.gif don't know why).

    Just encapsulate them in the "code" format:
    *****
     ****
      ***
       **
        *
    
    

    heh.. guess not :/

    you know... the Courier New font aligns things properly. Just wondering if we could get Courier New here icon_rolleyes.gif
    Exquisite as a lily, illustrious as a full moon,
    Magnanimous as the ocean, persistent as time.
  • JOblessELementJOblessELement Member Posts: 134
    joblesselement: that looks great. I'm not that good in c++ but it's more or less much like C.

    You're bound to learn it from me if you keep posting more of these so-called "brainteasers" :P On a serious note, you should get into C++ or Java, that is, if you want something more challenging than network troubleshooting.
    anyways, your logic looks neat.

    thanks =)
    just couldn't understand what the "cout" means. Is that like the printf in C, or did you mean "count"?

    Yep, couts are an idiotproof version of printf in that you don't have to specify the variable type.



    Give me a few minutes and I'll cough up the next solution =)
    I am free of all prejudices. I hate everyone equally.
  • 2lazybutsmart2lazybutsmart Member Posts: 1,119
    You're bound to learn it from me if you keep posting more of these so-called "brainteasers" :P On a serious note, you should get into C++ or Java, that is, if you want something more challenging than network troubleshooting.

    I sure will icon_lol.gif . Me being a VB dweller for 7+ yrs, I've found C to be rather interesting since I started taking in a few bits and pieces now and then on my free time. The really challenging part in this language is that you've got no functions to do everything for you (which is quite opposite to VB). You gotta use your hands to crank out the code and your head to cough out the logic. In fact, I've got this colleague who's a real pain-in-the-ass c++ guru. With my skills, I can put together a working program much faster than he can; but this dude works out some stuff that scares the $hit outta your brains.

    To wrap up the rant: this is one of the questions he asked me a couple of months ago (I still remember it cuz the beauty of it's logic was mesmerizing).

    Q: n = 12345. Reverse this number so that the result is 54321. You're not allowed to use any functions and You're not allowed to use Control Structures.

    Try it out when you u work out the previous one. icon_wink.gif
    Exquisite as a lily, illustrious as a full moon,
    Magnanimous as the ocean, persistent as time.
  • JOblessELementJOblessELement Member Posts: 134
    Here's the solution :)
        // top pyramid
        for (int x = 1; x<=5; x++) 
        { 
            printPyr(x);
        }
        // bottom upside down pyramid
        for (x = 5; x>=1; x--) 
        {
             printPyr(x);
         }
         void printPyr(int i)
         {
             // print space 
             for (int m = 5; m>i; m--) 
                 cout << " ";
             // print 1 to n
             for (int n = 1; n<=i; n++) 
                 cout << n;
             // print n-1 to 1
             for (int o = i-1; o >= 1; o--) 
                 cout << o;
             // print new line
             cout << endl;
           }
    
    You gotta use your hands to crank out the code and your head to cough out the logic.

    That's what "real" programming is all about. :P You've spent 7 years with VB (and I like you :P) ... so I'm not gonna say anything about it =)
    Q: n = 12345. Reverse this number so that the result is 54321. You're not allowed to use any functions and You're not allowed to use Control Structures.

    The only way I can think of doing it is to **** the integer into character array and then print backwards starting from the last pointer of the array. But that would involve a control structure.

    Here's the fastest way I'd do it with C++ strings and C++'s STL. Sorry if none of it makes sense.
    int num = 12345;
    ostringstream ostringnum;
    
    // push into output string
    ostringnum << num;
    string str_num = ostringnum.str();
    
    // reverse
    reverse(str_num.begin(), str_num.end());
    
    istringstream iss(str_num);
    int new_num;
    // push num from input string to new integer
    iss >> new_num;
    
    cout << new_num;
    

    Oh ... do let me know how your friend did it because I can't think of a way without using either a control loop or a pre-defnied function (reverse).
    I am free of all prejudices. I hate everyone equally.
  • 2lazybutsmart2lazybutsmart Member Posts: 1,119
    Great. Logic's neat and tight again.
    ... so I'm not gonna say anything about it =)

    hehe. now what is it that you wanna say ;). Things like VB's a joke when compared to C++ and stuff like that is all true. You don't have to shy from saying that. Imagine a person using C++ together with the OOP functionality and the visual capabilities of VB; that person really rocks. Now C by itself won't be able to compete in the industry today. Programmers now value ease and time more than anything else. And so that brings up the question: What development application do you use when programming in C++?
    Oh ... do let me know how your friend did it because I can't think of a way without using either a control loop or a pre-defnied function (reverse).

    You can use Loop's. You're only not allowed to use If's and switches/cases.

    Here's the fastest way I'd do it with C++ strings and C++'s STL. Sorry if none of it makes sense.

    hehe. it doesn't make sense; when functions are working right and left for you; I call that code nonsense :D. Now that you're allowed Loop's, cough up some tight logic ;)

    Here's how it would look like in VB (neat nonsense :rolleyes:)
    n="12345"
    For i=1 to 5
    	s= s & right(n,1)
    	n= mid(n,1,5-i)
    Next
    

    Nah... the mesmerizing logic I've been talking about is all math, math, math! None of that sloppy use of Mid and Right functions.

    OK. try out this one, which is more or less a variation of the previous problem when you're done.

    ----1
    ---212
    --32123
    -4321234
    543212345

    And then this:

    *********
    ****-****
    ***---***
    **
    **
    *
    *
    **
    **
    ***---***
    ****-****
    *********

    As usual, hyphens indicate a blank space.

    icon_wink.gif
    2lbs.
    Exquisite as a lily, illustrious as a full moon,
    Magnanimous as the ocean, persistent as time.
  • JOblessELementJOblessELement Member Posts: 134
    Imagine a person using C++ together with the OOP functionality and the visual capabilities of VB; that person really rocks.
    What do you think *Visual* C++ (and now C#) .Net and MFCs (Microsoft Foundation Classes) are all about then? :)
    Programmers now value ease and time more than anything else.

    This is turning out to be a really touch subject actually. Check out this article that wants the *fun* put back into programming : http://www.milbertus.com/archives/2004/03/003100.php
    I agree and disagree with the author but who gives a *bleep* about what I think =)
    You can use Loop's. You're only not allowed to use If's and switches/cases.
    Argh ... I'm gonna kill you :P But hey, atleast you got me thinking and I came up with that crazy ass string BS. Here's a C++ representation of your VB code in *simple* math:
    int num = 12345;
    while( num > 0 )
    {
        cout << num%10;
        num /= 10;
    }
    
    OK. try out this one, which is more or less a variation of the previous problem when you're done.
    These are nothing but tweaks of the previous ones ... how about a real brain twister ... perhaps on Microsoft interview level? :)
    I am free of all prejudices. I hate everyone equally.
  • JOblessELementJOblessELement Member Posts: 134
    There are 4 women who want to cross a bridge. They all begin on the same side. You have 17 minutes to get all of them across to the other side. It is night. There is one flashlight. A maximum of two people can cross at one time. Any party who crosses, either 1 or 2 people, must have the flashlight with them. The flashlight must be walked back and forth, it cannot be thrown, etc. Each woman walks at a different speed. A pair must walk together at the rate of the slower woman's pace.
    Woman 1: 1 minute to cross
    Woman 2: 2 minutes to cross
    Woman 3: 5 minutes to cross
    Woman 4: 10 minutes to cross


    For example if Woman 1 and Woman 4 walk across first, 10 minutes have elapsed when they get to the other side of the bridge. If Woman 4 then returns with the flashlight, a total of 20 minutes have passed and you have failed the mission. What is the order required to get all women across in 17 minutes?
    I am free of all prejudices. I hate everyone equally.
  • 2lazybutsmart2lazybutsmart Member Posts: 1,119
    That was a good one; I'll have to admit.

    Here's the solution

    Women 1 takes Women 2 = 2 min elapsed
    Women 2 Returns = 4 min elapsed
    Women 5 takes Women 10 = 14 min elapsed
    Women 1 Returns = 15 min elapsed
    Women 1 and Women 2 cross = 17 min elapsed

    icon_wink.gif
    2lbs.
    Exquisite as a lily, illustrious as a full moon,
    Magnanimous as the ocean, persistent as time.
  • 2lazybutsmart2lazybutsmart Member Posts: 1,119
    What do you think *Visual* C++ (and now C#) .Net and MFCs (Microsoft Foundation Classes) are all about then? :)

    Well that's actually why I've been picking at C lately. My company is all VB and nothing but VB together with SQL Server. Now since I'm certified in both, it kinda secures my job. But hey.... that's not a challange is it? I've already decided to prepare C#.net probably after 2 years from now. I'm currently preparing CCNA and then I want to to take another exam related to Windows Severs (most likely win2k3 netowrk infra). Then I'll be having one more exam to complete my MCDBA (that is if everything goes as planned) and I'll probably write it then. This whole plan could take upto 2 years (if all goes well). Now when I do plan to return to programming certifications and upgrade my MCSD; I'll make sure I study C# and declare myself a Cshaper from that day on.

    Till that time comes..... I'll try to learn as much as I can from C.
    int num = 12345;
    while( num > 0 )
    {
        cout << num%10;
        num /= 10;
    }
    

    That's awesome. The logic is great; but the only difference here is that you're printing the value retieved by %ing it by 10 instead of storing it in a variable. It still works and the core logic is excellent.

    This is how I did it (in C icon_cry.gif ).
    int n,i,j=0;
    n=12345;
    while (n != 0)
    {
       i = n % 10;
       n= n / 10;
       j= (j*10) + i;
    }
    


    Did you do programming as a hobby; or was it your major in university? I see you have a Bsc, so I'm assuming it's a Bsc in Computer Science or something related to that field.
    icon_wink.gif
    2lbs.
    Exquisite as a lily, illustrious as a full moon,
    Magnanimous as the ocean, persistent as time.
  • 2lazybutsmart2lazybutsmart Member Posts: 1,119
    Check out this article that wants the *fun* put back into programming :

    Whew.... this is probably the best way to increast my post count icon_lol.gif

    Hey... the *fun* here really depends on what everyone thinks fun is in progamming. I think many programmers tend to use the two words 'fun' and 'challenge' interchangebly these days. However, this is not, or will not be, the case when you end up in a 1K+ user firm that wants you and your team of 10 icon_cry.gif to develop an multi-tier enterprise application.

    I don't think many people would want to 'draw' command buttons in that situation. I know I'm taking it to the extreme, but my point remains.

    Now to come back to what that guy said in the post; I think he's right about the fact that the "geeks" are loosing the fun of programming everytime Microsoft rolls out a new version of VStudio. Now it's equally important to understand that the "corporate programmers" are getting the ease of development as new versions of development software is made.

    I don't think it's easy to draw a straight line between the two points of fun and ease+speed+reusablity. Different types of people want the line to be placed at different places. But hey.... when the managers are up your azz and you have stuff like vstudio.net2003, you'd think you'd died and went to heaven. icon_lol.gif

    2lbs.
    Exquisite as a lily, illustrious as a full moon,
    Magnanimous as the ocean, persistent as time.
  • JOblessELementJOblessELement Member Posts: 134
    That was a good one; I'll have to admit.
    Here's the solution

    Nicely done. Here's another standard MS "thinker" problem:

    One train leaves Los Angeles at 15mph heading for New York. Another train leaves from New York at 20mph heading for Los Angeles on the same track. If a bird, flying at 25mph, leaves from Los Angeles at the same time as the train and flies back and forth between the two trains until they collide, how far will the bird have traveled?
    I am free of all prejudices. I hate everyone equally.
  • JOblessELementJOblessELement Member Posts: 134
    but the only difference here is that you're printing the value retieved by %ing it by 10 instead of storing it in a variable.
    Well, you asked me to take and integer and hand out it's reverse as the result. I accomplished just that :)

    In your code, one variable can be eliminated. [And no this is not thanks to the magic of C++ :P]
    int i,n=0;
    cin >> n; // accept input from console
    while (n != 0)
    {
       i = (i*10) + (n%10);
       n= n / 10;
    }
    
    Did you do programming as a hobby; or was it your major in university?
    You guessed right though it did start off as a hobby in high school. Now, after graduation, I get to sit at home, discuss *tech* - gadgets, books, stock, etc with my 12 yr old sis (who doesn't give a hoot ... be better of talkin to a wall) as well as well live off my parents. It sure is a sad state of affairs ...
    but VB together with SQL Server. Now since I'm certified in both
    Is this what you have your MCSD in? I've personally never considered a programming certification because quite frankly, I'd like to think I can walk into an interview and prove my proficiency with my raw fresh-outta-college-with-2-yr-experience skills. Though with the way the current IT market is, I'm beginning to have second thoughts ... And to make things worse, the *network-adminER*-me is in debate with the programmer-me and it's becoming increasingly harder to make a choice :/ Just wasting more valuable time ... I just hope I find a job and not have to go into certs at all ...
    I'll try to learn as much as I can from C
    C's great and what not but with C# already out, that (to me) is like picking up an Office 95 book when we're already upto version 2003. As an avid Java programmer, I've had no trouble transitioning to C# and while you're busy "trying to learn" as much, why not spend that time with picking up tidbits of C++ instead? I've got a few *books* that I'd be willing you "lend" you ... PM me.

    -Joel
    I am free of all prejudices. I hate everyone equally.
  • 2lazybutsmart2lazybutsmart Member Posts: 1,119
    int i,n=0;
    cin >> n; // accept input from console
    while (n != 0)
    {
       i = (i*10) + (n%10);
       n= n / 10;
    }
    

    aha. that's nice, but did you forget to intialize i as 0 or can you get away with it in C++?
    Is this what you have your MCSD in?

    yes. and SQL Sever was an elective.
    I've personally never considered a programming certification because quite frankly, I'd like to think I can walk into an interview and prove my proficiency with my raw fresh-outta-college-with-2-yr-experience skills. Though with the way the current IT market is, I'm beginning to have second thoughts ...

    I'm sure you'd land a job if you could sit an interview and prove your skills, but the question, or problem, here is "how" will you get to the interview. These certs are all those good stuff you put on your resume to make it look "sexxy" and marketable. It's true that the knowledge is what really is tucked away in your brains and that employers should hire you because of it..... but hey... certs are just called certifications becuase they certify that the knowledge is indeed tucked away as you claim. Now since you know this stuff inside out, I think you should consider putting in about 1k and getting yourself a proffesional level certificate.

    ...and of course for the sake of competition; not only for certification. Mind you that the IT industry's got many unemployed people with truckloads of certs buzzing like a hive o' bees.

    Msc, Bsc, MCSE, MCSD,MCDBA's are out there who don't know **** (and this is true, sad, and funny all together icon_cry.gif ) and don't have jobs.
    And to make things worse, the *network-adminER*-me is in debate with the programmer-me and it's becoming increasingly harder to make a choice :/ Just wasting more valuable time ... I just hope I find a job and not have to go into certs at all ...

    As the saying goes "the hardest part of an author's day is passing the blank page". You gotta wirte something on it. Anything. "Write with your heart first, and then with your head" (Finding Forrestor ;)). You can't expect to get the right pargaraph on your first day and neither can you expect to make the right choice on day one. Just get yourself started with something. Anything. Then you'll gain momentum without even knowing it, and you'll find yourself either on the right track or on the wrong one. Trial and Error, that's how all humans evolve.


    2lbs.
    Exquisite as a lily, illustrious as a full moon,
    Magnanimous as the ocean, persistent as time.
  • 2lazybutsmart2lazybutsmart Member Posts: 1,119
    One train leaves Los Angeles at 15mph heading for New York. Another train leaves from New York at 20mph heading for Los Angeles on the same track. If a bird, flying at 25mph, leaves from Los Angeles at the same time as the train and flies back and forth between the two trains until they collide, how far will the bird have traveled?

    This is a tough one but let me give it a try.

    Let x stand for the num of hrs the 2 trains must travel before they can collide . and let's suppose the distance between the 2 cities is 1000 miles.

    So:

    (x*15) + (x*20) = 1000
    (15+20) * x = 1000
    x = 1000 / 35
    x=28.57 (rounded)

    Now that part is easy but this other part is a bit tricky. Let y stand for the number of hours the bird has to travel before she can reach the train that originates from NY.

    So based on our previous calculations, y=1000/45. y= 22.22 hrs. If the bird travled 22 hrs before she reached the train from NY, the train from LA would have already travled (15*22.22) 333 miles and the train from NY would have traveled (20*22.22) 444 miles. Now if we subtract the sum of the two distances travled by the two trains from the original total distance that leaves us with 223 miles. Now we have to calculate how much time the bird needs to reach the train from LA. The distance we have left at this point is 223.

    So based on our previous calculations, y=223/45. y=4.9 hours. So the bird will need an additional 4.9 hours before she can reach the train from LA. Now in gives the bird a total of (22.22+4.9) 27.17 hrs of flying and the distance the trains cover in 27.17 hours would be (20*27.17 + 15*27.17) 950 miles. The trains have 50 miles (or 1 hours and half) to go before they collide. Now if the bird heads east again, it will reach the train from NY in:

    (x*25) + (x*20) = 50
    x = 50/45
    x=1.11 hrs

    Now 1.11 plus it's previous total distance is (27.17+1.11) 28.28 hrs. And the distance covered by the 2 trains in that time would be around 990 miles. Now if the bird heads west again, it will reach the train from LA in.

    x=10miles/25 = 22mins. That added to it's previous total of 28.28 = 28.50.

    Darn! so the bird willt travel the same distance as the trains. (if this is right, which i doubt, my keybord needs a repair and my math skills need to be updated :D)

    2lbs
    Exquisite as a lily, illustrious as a full moon,
    Magnanimous as the ocean, persistent as time.
  • 2lazybutsmart2lazybutsmart Member Posts: 1,119
    ahm... well how 'bout this one:

    "On a man's tombstone, it is said that one sixth of his life was spent in childhood and one twelfth as a teenager. One seventh of his life passed between the time he became an adult and the time he married; five years later, his son was born. Alas, the son died four years before he did. He lived to be twice as old as his son did. How old did the man live to be?"

    icon_wink.gif
    2lbs.
    Exquisite as a lily, illustrious as a full moon,
    Magnanimous as the ocean, persistent as time.
  • JOblessELementJOblessELement Member Posts: 134
    ... and one twelfth as a teenager.


    I could use the lengthy "variabled" mathroute that you did for the last problem ... but here's a simpler solution to this one:

    The man lived 1/12 of his life as a teenager. There are seven teen years. 7 x 12 = 84.

    I swear I didn't have to pull out the calculator for that one :P
    I am free of all prejudices. I hate everyone equally.
  • JOblessELementJOblessELement Member Posts: 134
    Here's a real tricky one:

    Two mathematicians, Albert and Isaac, chat. Isaac says he has three children who all have the same birthday (but who weren't necessarily born in the same year). Albert asks their ages. Isaac replies, "The product of the ages of my children is 72." Albert points out that this is not enough information to determine their ages. Isaac responds with another clue -- he tells Albert the sum of the ages of his children. But Albert again points out that there is not enough information. Finally Isaac says, "My youngest child is named Galileo." At last, Albert correctly determines the ages of Isaac's children. What are the ages?


    No cheating :P
    (I hope more people join in this)
    I am free of all prejudices. I hate everyone equally.
  • 2lazybutsmart2lazybutsmart Member Posts: 1,119
    Not enough information! icon_lol.gif

    This problem is not hard, but I'd have to list all the possible combinations of numbers that will end up giving me a product of 72. Because you said the "product": we'd have to figure out how many *different* numbers times each other will equal 72.

    1*1*72=72 ( icon_lol.gif that's one example)
    2*2*18=72 (another one...bla bla)

    Is there any other information to narrow the search??

    I'll be back with some more.
    2lbs.
    Exquisite as a lily, illustrious as a full moon,
    Magnanimous as the ocean, persistent as time.
Sign In or Register to comment.