Results 1 to 20 of 20

Thread: Add Until [Function]

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2011
    Posts
    5

    Thumbs up Add Until [Function]

    Hey guys!

    I was just bored and had nothing to do so I made this super duper easy function. Let me tell you what it does.

    Let us say, that we have a math thing to do

    ? + 321 = 485

    And we are really stupid so we dont know how to solve it. We do like this.

    We use my function!

    Code:
    Public Function Add(byval intyouknow as integer, byval intresult as integer)
    
    Dim i as integer = 0
    Do until i + intyouknow = intresult
    i = i + 1
    Loop
    Return i
    End Function
    This is pretty obvious what it does so here's how we are going to solve our "tricky situation"

    Code:
    Messagebox.Show(add(321,485))
    And that will return 164, since 164 + 321 = 485 (DERP)

    Hope you liked it!

  2. #2

    Re: Add Until [Function]

    You want to make that a bit faster?:

    Code:
            Public Function Add(ByVal intyouknow As Integer, ByVal intresult As Integer) As Integer
                Return intresult - intyouknow
            End Function
    That will do the exact same thing without the For loop; which if you have really big numbers, will slow it down.

  3. #3

    Thread Starter
    New Member
    Join Date
    Sep 2011
    Posts
    5

    Re: Add Until [Function]

    Quote Originally Posted by formlesstree4 View Post
    You want to make that a bit faster?:

    Code:
            Public Function Add(ByVal intyouknow As Integer, ByVal intresult As Integer) As Integer
                Return intresult - intyouknow
            End Function
    That will do the exact same thing without the For loop; which if you have really big numbers, will slow it down.
    Ah that's awesome dude

    Didn't know that!

  4. #4

  5. #5
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Re: Add Until [Function]

    I'd rather one for this: X^Y=Z where X is the unknown. It's a logarithm, but I don't know how to calculate it. Let's make this thread useful!

  6. #6
    Fanatic Member BlindSniper's Avatar
    Join Date
    Jan 2011
    Location
    South Africa
    Posts
    865

    Re: Add Until [Function]

    I was looking on the internet for some examples on what minitech said and I couldn't figure out how to use logs for his problem, but then I realized that it was not needed.
    Code:
    X^7=16384
    16384^(1/7)=4
    4^7=16384

    Useful CodeBank Entries of mine
    Expand Function
    Code Compiler
    Sudoku Solver
    HotKeyHandler Class

    Read this to get Effective help on VBForums
    Hitchhiker's Guide to Getting Help at VBF

  7. #7

    Re: Add Until [Function]

    Can you verify that with logarithms by hand?

  8. #8
    Fanatic Member BlindSniper's Avatar
    Join Date
    Jan 2011
    Location
    South Africa
    Posts
    865

    Re: Add Until [Function]

    Why ? Isn't this simpler than log's ? In any case I haven't done logs at school yet so I don't really know how.

    Useful CodeBank Entries of mine
    Expand Function
    Code Compiler
    Sudoku Solver
    HotKeyHandler Class

    Read this to get Effective help on VBForums
    Hitchhiker's Guide to Getting Help at VBF

  9. #9
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Re: Add Until [Function]

    @formlesstree4: It would be more complicated, but sure; log(4, 16384) = 7. And now, from W|A I know that log(1/y, z) = ln(z) / ln(y). Thanks!

  10. #10
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: Add Until [Function]

    You don't need to use logs, that's just silly. It boils down to the same thing in the end anyway (of course), you just use the root...

    Suppose a and b are given and you need to solve for x:
    x^a = b

    Then x = b^(1/a).

    In case a = 2, everyone knows this. If x^2 = b, then x = sqrt(b) = b^(1/2). Give or take a minus sign.
    It's the same when a is not 2, you just take the 'a'th root instead of the square root.


    Now logs...
    You correctly state that
    log(b) / log(x) = a.

    But how does that help? Let's calculate...


    And we get exactly the same result... So, useless, don't use logs



    As for x^7 = 16384, the answer x = 4 is not the only answer, there are 6 more answers, but they are all complex:

  11. #11

    Re: Add Until [Function]

    Quote Originally Posted by NickThissen View Post
    You don't need to use logs, that's just silly. It boils down to the same thing in the end anyway (of course), you just use the root...

    Suppose a and b are given and you need to solve for x:
    x^a = b

    Then x = b^(1/a).

    In case a = 2, everyone knows this. If x^2 = b, then x = sqrt(b) = b^(1/2). Give or take a minus sign.
    It's the same when a is not 2, you just take the 'a'th root instead of the square root.


    Now logs...
    You correctly state that
    log(b) / log(x) = a.

    But how does that help? Let's calculate...


    And we get exactly the same result... So, useless, don't use logs



    As for x^7 = 16384, the answer x = 4 is not the only answer, there are 6 more answers, but they are all complex:
    WELL LOOK AT YOU MR. MATHEMATICS PERSON! Nice proof though. I guess you had some free time eh?

    EDIT: I asked for a logarithmic proof because minitech's question involved logarithms. 'tis all.

  12. #12
    Fanatic Member BlindSniper's Avatar
    Join Date
    Jan 2011
    Location
    South Africa
    Posts
    865

    Re: Add Until [Function]

    Nick you probably didn't just find that last image on the net. Where did you get it, it looks like a cool program ?

    Useful CodeBank Entries of mine
    Expand Function
    Code Compiler
    Sudoku Solver
    HotKeyHandler Class

    Read this to get Effective help on VBForums
    Hitchhiker's Guide to Getting Help at VBF

  13. #13
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: Add Until [Function]

    Quote Originally Posted by formlesstree4 View Post
    WELL LOOK AT YOU MR. MATHEMATICS PERSON! Nice proof though. I guess you had some free time eh?

    EDIT: I asked for a logarithmic proof because minitech's question involved logarithms. 'tis all.
    10 seconds is all I needed. Come on, this is basic math isn't it?

    Quote Originally Posted by BlindSniper View Post
    Nick you probably didn't just find that last image on the net. Where did you get it, it looks like a cool program ?
    Maple. I was too lazy to type it out so I had Maple calculate it for me

    WolframAlpha can give you pretty much the same answer though:
    http://www.wolframalpha.com/input/?i...7+%3D+16384%29

  14. #14
    Fanatic Member BlindSniper's Avatar
    Join Date
    Jan 2011
    Location
    South Africa
    Posts
    865

    Re: Add Until [Function]

    Should we start a codebank entry for math functions instead of hijacking a thread ?

    Useful CodeBank Entries of mine
    Expand Function
    Code Compiler
    Sudoku Solver
    HotKeyHandler Class

    Read this to get Effective help on VBForums
    Hitchhiker's Guide to Getting Help at VBF

  15. #15
    Hyperactive Member Ram2Curious's Avatar
    Join Date
    Apr 2010
    Posts
    484

    Re: Add Until [Function]

    Quote Originally Posted by BlindSniper View Post
    Should we start a codebank entry for math functions instead of hijacking a thread ?
    Wait, am i late for the math class????

  16. #16

    Re: Add Until [Function]

    Quote Originally Posted by NickThissen View Post
    10 seconds is all I needed. Come on, this is basic math isn't it?


    Maple. I was too lazy to type it out so I had Maple calculate it for me

    WolframAlpha can give you pretty much the same answer though:
    http://www.wolframalpha.com/input/?i...7+%3D+16384%29
    It is basic math, I'm just messing with you

  17. #17
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Re: Add Until [Function]

    @NickThissen: I know logs weren't the answer to the problem, I was asking how to calculate them. I always see constants like: LOG_2_E so I assumed there wasn't a better way to calculate them than log(x, y) = log(x, e) * ln(y).

  18. #18
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: Add Until [Function]

    There's infinitely many logarithms, each with their own 'base' as you probably know. The function 'log' usually corresponds to the logarithm with base 10, often written log10(x). Sometimes it also refers to the natural logarithm or ln(x) = loge(x).

    To switch from one base to another you just divide by the log of that base:
    log3(x) = logy(x) / logy(3)
    In this case y is irrelevant, as long as you use the same in numerator and denominator.

    Judging from your formula, with log(x, y) you mean logy(x) = loge(x) / ln(y) = loge(x) / loge(y) = ln(x) / ln(y).

    Since the natural logarithm (ln, base e) is often considered 'more fundamental', logs in a different base are sometimes 'defined' as a ratio of natural logarithms as above -- logy(x) = ln(x) / ln(y).

  19. #19

    Re: Add Until [Function]

    Quote Originally Posted by NickThissen View Post
    There's infinitely many logarithms, each with their own 'base' as you probably know. The function 'log' usually corresponds to the logarithm with base 10, often written log10(x). Sometimes it also refers to the natural logarithm or ln(x) = loge(x).

    To switch from one base to another you just divide by the log of that base:
    log3(x) = logy(x) / logy(3)
    In this case y is irrelevant, as long as you use the same in numerator and denominator.

    Judging from your formula, with log(x, y) you mean logy(x) = loge(x) / ln(y) = loge(x) / loge(y) = ln(x) / ln(y).

    Since the natural logarithm (ln, base e) is often considered 'more fundamental', logs in a different base are sometimes 'defined' as a ratio of natural logarithms as above -- logy(x) = ln(x) / ln(y).
    I wish my PreCalc teacher explained it the same way you did. It's a really good and simple explanation of converting log bases, which can be very fun.

  20. #20
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Re: Add Until [Function]

    How are you doing subscripts??! sub? Oops, I guess I just got it there. Nevermind.

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