Results 1 to 12 of 12

Thread: Can you answer this vb question for fun?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Mar 2000
    Location
    bebenia, PA, USA
    Posts
    241
    A is an integer and A>1. Write a simple program to solve the following problem.

    1+2+3+...+N=?

    Can you figure this one out?

  2. #2
    Fanatic Member
    Join Date
    Jan 1999
    Location
    UK
    Posts
    593

    Lightbulb

    Code:
    Dim sum As Long, n As Integer
    sum = 0.5 * n * (n+1)
    Phew - my first VB code in a while

    John

  3. #3
    Frenzied Member
    Join Date
    Mar 2000
    Posts
    1,089
    OK John, 1 + 4 + 9 + ... + N^2?

  4. #4
    Fanatic Member
    Join Date
    Jan 1999
    Location
    UK
    Posts
    593

    Wink Bring 'em on

    Code:
    Dim sum As Long, n As Integer
    sum = (2 * n + 1) * (n + 1) * n / 6
    Had to think about that one a bit more...

    John

  5. #5
    Guest
    So if N is 5...

    1+2+3+4+5 = 55 ?

    Can someone explain this to me?

    Plus where does "A" come into it?

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Mar 2000
    Location
    bebenia, PA, USA
    Posts
    241
    That was a misprint on my part A is the same as N or N should have been A

    A is an integer A>1:

    1+2+3+...+A=?

    you can use A or N; whatever letter you like; sorry!!!

  7. #7
    Addicted Member
    Join Date
    Mar 2000
    Location
    Gainesville, FL
    Posts
    131
    Well, someone is getting their homework done for free.

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Mar 2000
    Location
    bebenia, PA, USA
    Posts
    241
    Hehe; this is not homework; trying to share a question from a job interview and sadly I don't get homework anymore -- I just have to write programs all day at work!!!

    here's what i got:

    A=0
    if N=3
    for next loop
    A=A+N

  9. #9
    Guest

    an interview question: efficient design

    There are 8 balls of equal size, but there is one ball that is the heaviest, find the heaviest ball by weighing them just twice in the weighing scale.

    If you watch Zoom from Disney, then this question might seem trivial.

  10. #10
    Frenzied Member
    Join Date
    Mar 2000
    Posts
    1,089
    OK, Put 3 balls on one side of the scales and 3 on the other, if they weigh the same then take the 2 unweighed balls and put one on each side of the scales the heavier one is the heavy ball.

    If they didn't weigh the same take the 3 balls from the heavy side and compare 2 of them, If one is heavier then that's the heavy ball, if not the third is the heavy one.


    you should have said 9 balls, that makes it less obvious.

  11. #11
    New Member
    Join Date
    May 2000
    Posts
    14
    Uhm.. first you say that A>1 and then you say that A=0 in your code-example, don't you mean A=>0 or perhaps A=>1?
    Well, it's a pentium parrot, besides, it's only a decimal.

  12. #12

    Thread Starter
    Addicted Member
    Join Date
    Mar 2000
    Location
    bebenia, PA, USA
    Posts
    241
    Actually I was using A as the counter going back to using N as the problem so I meant

    N is an integer, N>1 so I need two variables one is a counter and one is a sum

    A=0 ' Assign the counter to 0
    N=3' Need to get sum

    I like this as an answer better:

    Dim Total as integer

    Total=0 ' start counter off at 0
    For I= 1 to N
    Total=Total+I
    Next I
    Print Total


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width