19 November 2012

Fibonacci Calculator

 Today, I'm going to give you a code to add a requested number of Fibonacci numbers into a ListBox.

Components needed :
  • 1 List Box
  • 1 Text Box
  • 1 Label
  • 1 Button


Let the Fibonacci Sequence be  : 1, 1, 2, 3, 5, 8, 13, 24, ...
Let n be the number of the Fibonacci numbers to be added to the list List1.
Let l be the number of loops to be conducted.


Private Sub Command1_Click()
Dim a, b, c As Double
Dim l, n As Integer
a = 0
b = 1
n = Val(Text1.Text)
For l = 1 To n
a = b
b = c
c = a + b
List1.AddItem (c)
Next
End Sub


The idea here is to swap between a,b, and c to obtain the next number in the Fibonacci sequence.
It's as simple as that.

Here's a preview of what it looks like:




Note:
The Fibonacci sequence is an interesting set of numbers. You might want to do some research on it.



Happy Coding,
Cyber Frost

01 October 2012

Project Euler - Problem 6 (Java)

This time, I'm giving out the solution of Problem 6 in the form of Java code.

Without wasting more time, here's the code:

class euler6 {
public static void main (String [] args) {
int x = 0;
int y = 0;
for(int i=1;i<101;i++) {
x += i;
}

for(int j=1;j<101;j++) {
y += Math.pow(j, 2);
}

System.out.println("Square of the sum of the first one hundred natural numbers : " + Math.pow(x, 2));
System.out.println("Sum of the squares of the first one hundred natural numbers : " + y);
System.out.println("Hence, the difference of both numbers is " + Math.pow(x, 2) + " - " + y + " = " + (Math.pow(x, 2) - y));
}
}

And, here's a preview of its output.

As we can see, The answer doesn't match the one calculated in Visual Basic.
It is 2.516415E7.

What is E? E is a representation of exponential values. Let's take the answer above as an example.

We've got 2.516415E7, what does that mean? It actually means multiplying 2.516415 by E7(which is mathematically written as 10^7). Therefore, the answer could be written as :

 2.516415 x 10 ^ 7 

which equals to 25164150, the correct answer.

24 September 2012

Project Euler - Problem 6

The sum of the squares of the first ten natural numbers is,
12 + 22 + ... + 102 = 385
The square of the sum of the first ten natural numbers is,
(1 + 2 + ... + 10)2 = 552 = 3025
Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is 3025 - 385 = 2640.
Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.

Answer:
This is a simple problem. Again, I'm using VB.NET to solve this problem. But this time, it is not the Windows App, it's the Console Application. The reason is that we do not need any User Interface here.

So, let's just get right to the code.

Code:

Module Module1

    Sub Main()
        Dim x, y As Double

        For i As Integer = 1 To 100
            x = x + i
        Next

        For j As Integer = 1 To 100
            y = y + (j ^ 2)
        Next

        Console.WriteLine("Square of the sum of the first one hundred natural numbers : " & x ^ 2)
        Console.WriteLine("Sum of the squares of the first one hundred natural numbers : " & y)
        Console.WriteLine("Hence, the difference of both numbers is " & (x ^ 2) & " - " & y & " = " & (x ^ 2) - y)
    End Sub

End Module


Here's what it looks like :













From the picture above, we can see that the answer is 25164150. 

Good luck trying !

Cyber  Frost








Search