Publishing System Settings Logout Login Register
Math Functions & Algorithms
TutorialCommentsThe AuthorReport Tutorial
Tutorial Avatar
Rating
Add to Favorites
Posted on October 7th, 2006
17631 views
Visual Basic
In any projects, we often need to use some functions or algorithms that are not prebuilt in VB. In this tutorial, you will learn how to use many of these functions and algorithms.

Custom Base Log (log x)

Normally, the default log in VB use a base 10, sometimes you need to set your own base (2 for example in binary). The formula is quite simple:

(log x) / (log b)

"x" is the number we want the log to be and "b" is the base.

Here's the code for a base 2:


Public Function log2(n As Double) As Double

Dim p As Double

p = Log(n) / Log(2)

log2 = p

End Function


You could of course set the base as a parameter of the function.


Factorial (!n)

This function let you multiply numbers by their previous. For example, factorial of 5 would be 1x2x3x4x5.

In VB, we simply use a For to increment the multiplication.

Here's the code:


Public Function facto(n As Double) As Double

Dim rep As Double
Dim i As Double

rep = 1
For i = 1 To n

rep = rep * i

Next i

facto = rep

End Function




The E number (e)

The number behind many things. Ever knew what the E number meant on your calculator, why it's giving 2,718...? It's actually simple, it can be calculated with the help of the above function (Factorial). Here's a sample of the formula:

e = 1/!1 + 1/!2 + 1/3! + 1/4! + 1/5! + 1/6! + 1/7! + 1/!8....

The more operations you set, the more the e number will be precise. Note that in this code, i use the above function (Factorial).
The For is used to specify the current Factorial to calculate. (!i)


Public Function facto2(n As Double) As Double

Dim rep As Double
Dim i As Double

rep = 1
For i = 1 To n

rep = rep + (1 / facto(i))

Next i

facto2 = rep

End Function



Fibonnaci

The fibonnaci sequence is generated by making an addition of the previous number and the current one, just like Fibonnaci of 5 would be 1+2+3+4+5. Of course, we could use a For loop to achieve this but why not using a recursive function? In other words, it's true that the current number is the addition of the previous Fibonnaci sequence + the other one before. So we just need to call the function inside the current one indefinitly until we reach a "n" value of 1 where it's known that the result is 1.


Public Function fibon(ByVal n As Byte) As Double

If n < 2 Then

fibon = 1

Else

fibon = fibon(n - 2) + fibon(n - 1)

End If

End Function



Root

A square root can be quite simple but when it comes to cubic or more, a function might be helpful. A square root is exactly the same thing as saying: x ^ 1/2. So we can use this to calculate any root. (Short code...)


Public Function racine_n(ByVal x As Double, ByVal n As Byte) As Double

racine_n = x ^ (1 / n)

End Function


More functions and sort algorithms (Mergesort, Fusionsort, etc.) to come in future tutorials!
Premium Publisher
Dig this tutorial?
Thank the author by sending him a few P2L credits!

Send
NGPixel

Lead Programmer at Pixel2Life

My tutorials are mostly about PHP, MySQL, XHTML, CSS and Fireworks.
View Full Profile Add as Friend Send PM
Pixel2Life Home Advanced Search Search Tutorial Index Publish Tutorials Community Forums Web Hosting P2L On Facebook P2L On Twitter P2L Feeds Tutorial Index Publish Tutorials Community Forums Web Hosting P2L On Facebook P2L On Twitter P2L Feeds Pixel2life Homepage Submit a Tutorial Publish a Tutorial Join our Forums P2L Marketplace Advertise on P2L P2L Website Hosting Help and FAQ Topsites Link Exchange P2L RSS Feeds P2L Sitemap Contact Us Privacy Statement Legal P2L Facebook Fanpage Follow us on Twitter P2L Studios Portal P2L Website Hosting Back to Top