Welcome Guest!
 VB Helper
 Previous Message All Messages Next Message 
VB Helper Newsletter  Rod Stephens
 Mar 06, 2009 13:25 PST 

Sorry for the long hiatus. My latest book is keeping me WAY busier than
I would like. Unfortunately that's likely to continue for a while.

But I wanted to send something out for a couple of reasons:

    - DevX posted a special report called "Move to the Future with
Multicore Code" (http://www.devx.com/SpecialReports/Door/40893). It
includes links to 9 DevX articles about multicore programming including
my two "Getting Started with the .NET Task Parallel Library" and
"Getting Started with the .NET Task Parallel Library: Multicore Case
Studies."

    - I've added a new section in BooksToKeep.com for Christopher Moore.
I was amazed to find that I had forgotten his books when I recently
finished "Island of the Sequined Love Nun." It's absolutely hilarious!
My favorite of his so far.

    - This week also features some kind of interesting stories that came
out of out local elementary school’s fifth grade Math Olympiad group (in
VB 6 and .NET versions). They're pretty optional but include a useful
factorization function.

Have a great week and thanks for subscribing!

Rod
RodSte-@vb-helper.com

Books To Keep: http://www.BooksToKeep.com
----------
*** Now Available ***

Beginning Database Design Solutions
http://www.amazon.com/exec/obidos/ASIN/0470385499/vbhelper/

Visual Basic 2008 Programmer's Reference
http://www.amazon.com/exec/obidos/ASIN/0470182628/vbhelper/
==========

    VB6 Contents:
1. New HowTo: Calculate the prime factors of a product of primes plus or
minus 1 in Visual Basic 6
2. New HowTo: Calculate the prime factors of numbers of the form 999997
in Visual Basic 6

    VB.NET Contents:
3. New HowTo: Restore a window to a known position if you can't find it
in Visual Basic 2005
4. New HowTo: Calculate the prime factors of a product of primes plus or
minus 1 in Visual Basic 2005
5. New HowTo: Calculate the prime factors of numbers of the form 999997
in Visual Basic 2005
6. New HowTo: Start a program from Sub Main in Visual Basic 2005 and
later

    Both Contents:
7. New Links
==========
++++++++++
   <VB6>
++++++++++
==========
1. New HowTo: Calculate the prime factors of a product of primes plus or
minus 1 in Visual Basic 6
http://www.vb-helper.com/howto_prime_products.html
http://www.vb-helper.com/HowTo/howto_prime_products.zip

In around 300 BC, Euclid proved that there are an infinite number of
primes using this argument:

    Suppose there are a finite number of primes and they are p1, p2,
..., pn. Then none of can divide the value:

        p1 * p2 * ... * Pn + 1

    So this new number is also prime, contradicting the assumption that
p1, p2, ..., pn are all of the primes.

Recently someone suggested a similar technique for finding a big prime.
The idea is to take the product of the first N primes and then add or
subtract 1.

Unfortunately this method doesn't work. While the new number is not
divisible by the primes you use to build the number, there may be other
primes beyond those (but still smaller than the square root of the new
number) that divide it.

This program demonstrates this by calculating products of the first N
primes and then finding their prime factors.

The following code shows the program's Form_Load event handler. It
calculates products of the first 1, 2, ..., 9 primes and calls function
Factors to display their prime factors.

Private Sub Form_Load()
Dim primes() As String
Dim result As String
Dim i As Integer
Dim j As Integer
Dim num As Long
Dim txt As String

    primes = Split("2,3,5,7,11,13,17,19,23", ",")
    result = ""
    
    For i = LBound(primes) To UBound(primes)
        num = 1
        txt = ""
        For j = LBound(primes) To i
            txt = txt & " x " & primes(j)
            num = num * CLng(primes(j))
        Next j
        txt = Mid$(txt, 3)
        num = num - 1
        result = result & txt & " - 1 = " & Format$(num) & " = " &
Factors(num) & vbCrLf
        num = num + 2
        result = result & txt & " + 1 = " & Format$(num) & " = " &
Factors(num) & vbCrLf
    Next i

    txtResult.Text = result
    txtResult.SelStart = 0
    txtResult.SelLength = 0
End Sub

The Factors function is the only part of this program that is really
generally useful. It returns a string listing a number's prime factors.

The function starts by pulling factors of 2 out of the number. As long
as the number is divisible by 2, the program adds a 2 to the output and
divides the number by 2.

Eventually the number becomes odd (there may be remaining odd factors
or, if the number is a power of two, then this step reduces the number
to 1). At that point, the program starts pulling out odd factors.
Starting with the test factor value 3, if the test factor value divides
the number evenly, then the program adds it to the output and divides
the test factor from the number. If the test factor doesn't divide the
number evenly, then the program adds 2 to the test factor to consider
the next odd number.

The program repeats this step until the test factor is greater than the
square root of the remaining number. At that point, whatever is left of
the number is prime.

' Return the number's prime factors.
Private Function Factors(ByVal num As Long) As String
    Dim result As String
    result = ""

    ' Take out the 2s.
    Do While num Mod 2 = 0
        ' This is a factor.
        result = result & " x 2"
        num = num \ 2
    Loop

    ' Take out other primes.
    Dim factor As Long
    factor = 3
    Do While factor * factor <= num
        If num Mod factor = 0 Then
            ' This is a factor.
            result = result & " x " & Format$(factor)
            num = num \ factor
        Else
            factor = factor + 1
        End If
    Loop

    If num > 1 Then result = result & " x " & Format$(num)
    If Len(result) = 0 Then
        result = "1"
    Else
        result = Mid$(result, 3)
    End If

    Factors = result
End Function

Incidentally, the first example that p1 * p2 * ... * pn - 1 is non prime
is:

    2 x 3 x 5 x 7 - 1 = 209 = 11 x 19

The first example that p1 * p2 * ... * pn + 1 is non prime is:

    2 x 3 x 5 x 7 x 11 x 13 + 1 = 30031 = 59 x 509

Also go to
ncenews.org/view/generic/id/36979/title/Largest_known_prime_number_found
for an interesting article about the largest known prime number (as of
September 28, 2008). It has around 13 million digits!
==========
2. New HowTo: Calculate the prime factors of numbers of the form 999997
in Visual Basic 6
http://www.vb-helper.com/howto_nines_prime.html
http://www.vb-helper.com/HowTo/howto_nines_prime.zip

Recently a bunch of fifth graders were asked to think of the largest
prime numbers they could. A lot of them wrote numbers of the form
99999997. It's easy to see that these are not divisible by 2 or 5.
Because the digits aren't divisible by 3, the number is also not
divisible by 3 (most of them knew that trick).

In fact, the first several numbers in this format 7, 97, and 997 are
prime, and it's not easy to tell whether larger numbers of this sort are
prime in your head.

This program writes out several numbers in this format and displays
their prime factors.

The program's Form_Load event handler loops through nine of these
numbers, calling function Factors to display their prime factors.

Private Sub Form_Load()
Dim result As String
Dim nine As String
Dim i As Integer
Dim num As Long

    result = ""
    nine = "7"
    For i = 1 To 9
        num = CLng(nine)
        result = result & vbCrLf & Format$(num) & " = " & Factors(num)
        nine = "9" & nine
    Next i

    result = Mid$(result, Len(vbCrLf) + 1)
    txtResult.Text = result
    txtResult.SelStart = 0
    txtResult.SelLength = 0
End Sub


The Factors function is the only part of this program that is really
generally useful. It returns a string listing a number's prime factors.

The function starts by pulling factors of 2 out of the number. As long
as the number is divisible by 2, the program adds a 2 to the output and
divides the number by 2.

Eventually the number becomes odd (there may be remaining odd factors
or, if the number is a power of two, then this step reduces the number
to 1). At that point, the program starts pulling out odd factors.
Starting with the test factor value 3, if the test factor value divides
the number evenly, then the program adds it to the output and divides
the test factor from the number. If the test factor doesn't divide the
number evenly, then the program adds 2 to the test factor to consider
the next odd number.

The program repeats this step until the test factor is greater than the
square root of the remaining number. At that point, whatever is left of
the number is prime.

' Return the number's prime factors.
Private Function Factors(ByVal num As Long) As String
    Dim result As String
    result = ""

    ' Take out the 2s.
    Do While num Mod 2 = 0
        ' This is a factor.
        result = result & " x 2"
        num = num \ 2
    Loop

    ' Take out other primes.
    Dim factor As Long
    factor = 3
    Do While factor * factor <= num
        If num Mod factor = 0 Then
            ' This is a factor.
            result = result & " x " & Format$(factor)
            num = num \ factor
        Else
            factor = factor + 1
        End If
    Loop

    If num > 1 Then result = result & " x " & Format$(num)
    If Len(result) = 0 Then
        result = "1"
    Else
        result = Mid$(result, 3)
    End If

    Factors = result
End Function

Incidentally, the very next number with this format (the first one
that's hard to verify in your head) is non-prime:

    9997 = 13 x 769

Nice try, kids!
==========
++++++++++
<VB.NET>
++++++++++
==========
3. New HowTo: Restore a window to a known position if you can't find it
in Visual Basic 2005
http://www.vb-helper.com/howto_net_restore_window_position.html
http://www.vb-helper.com/HowTo/howto_net_restore_window_position.zip

Recently Microsoft Excel decided to take a little vacation. I could
maximize it and minimize it but when I tried to restore it, it moved
somewhere off the screen so I couldn't find it. I needed to be able to
display Excel at less than full screen so I wrote this program to find
it and put it back on the desktop.

Enter the title of the target application and the position where you
want it, and click Go. The Go button's Click event handler uses
FindWindow to find the window based on its title. It then uses
SetWindowPos to position the target application.


Private Declare Function FindWindow Lib "user32" Alias "FindWindowA"
(ByVal lpClassName As String, ByVal lpWindowName As String) As Integer
Private Declare Function SetWindowPos Lib "user32" Alias "SetWindowPos"
(ByVal hwnd As Integer, ByVal hWndInsertAfter As Integer, ByVal x As
Integer, ByVal y As Integer, ByVal cx As Integer, ByVal cy As Integer,
ByVal wFlags As Integer) As Integer

' Find the target window and restore it.
Private Sub btnGo_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnGo.Click
    ' Find the target.
    Dim app_hwnd As Long = FindWindow(vbNullString, txtTitle.Text)
    If app_hwnd = 0 Then
        MessageBox.Show("Cannot find application " & txtTitle.Text, _
            "Application Not Found", MessageBoxButtons.OK,
MessageBoxIcon.Error)
        Exit Sub
    End If

    ' Position the window.
    SetWindowPos(app_hwnd, 0, _
        Val(txtLeft.Text), _
        Val(txtTop.Text), _
        Val(txtWidth.Text), _
        Val(txtHeight.Text), _
        0)
End Sub
==========
4. New HowTo: Calculate the prime factors of a product of primes plus or
minus 1 in Visual Basic 2005
http://www.vb-helper.com/howto_net_prime_products.html
http://www.vb-helper.com/HowTo/howto_net_prime_products.zip

In around 300 BC, Euclid proved that there are an infinite number of
primes using this argument:

Suppose there are a finite number of primes and they are p1, p2, ...,
pn. Then none of can divide the value:

    p1 * p2 * ... * Pn + 1

So this new number is also prime, contradicting the assumption that p1,
p2, ..., pn are all of the primes.

Recently someone suggested a similar technique for finding a big prime.
The idea is to take the product of the first N primes and then add or
subtract 1.

Unfortunately this method doesn't work. While the new number is not
divisible by the primes you use to build the number, there may be other
primes beyond those (but still smaller than the square root of the new
number) that divide it.

This program demonstrates this by calculating products of the first N
primes and then finding their prime factors.

The following code shows the program's Form_Load event handler. It
calculates products of the first 1, 2, ..., 9 primes and calls function
Factors to display their prime factors.

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
    Dim primes() As Long = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29}
    Dim result As String = ""
    For i As Integer = 0 To primes.Length - 1
        Dim num As Long = 1
        Dim txt As String = ""
        For j As Integer = 0 To i
            txt &= " x " & primes(j).ToString()
            num *= primes(j)
        Next j
        txt = txt.Substring(3)
        num -= 1
        result &= txt & " - 1 = " & num.ToString() & " = " &
Factors(num) & vbCrLf
        num += 2
        result &= txt & " + 1 = " & num.ToString() & " = " &
Factors(num) & vbCrLf
    Next i

    txtResult.Text = result
    txtResult.Select(0, 0)
End Sub

The Factors function is the only part of this program that is really
generally useful. It returns a string listing a number's prime factors.

The function starts by pulling factors of 2 out of the number. As long
as the number is divisible by 2, the program adds a 2 to the output and
divides the number by 2.

Eventually the number becomes odd (there may be remaining odd factors
or, if the number is a power of two, then this step reduces the number
to 1). At that point, the program starts pulling out odd factors.
Starting with the test factor value 3, if the test factor value divides
the number evenly, then the program adds it to the output and divides
the test factor from the number. If the test factor doesn't divide the
number evenly, then the program adds 2 to the test factor to consider
the next odd number.

The program repeats this step until the test factor is greater than the
square root of the remaining number. At that point, whatever is left of
the number is prime.

' Return the number's prime factors.
Private Function Factors(ByVal num As Long) As String
    Dim result As String = ""

    ' Take out the 2s.
    Do While num Mod 2 = 0
        ' This is a factor.
        result &= " x 2"
        num \= 2
    Loop

    ' Take out other primes.
    Dim factor As Long = 3
    Do While factor * factor <= num
        If num Mod factor = 0 Then
            ' This is a factor.
            result &= " x " & factor.ToString()
            num \= factor
        Else
            factor += 1
        End If
    Loop

    If num > 1 Then result &= " x " & num
    If result.Length = 0 Then
        result = "1"
    Else
        result = result.Substring(3)
    End If

    Return result
End Function

Incidentally, the first example that p1 * p2 * ... * pn - 1 is non prime
is:

     2 x 3 x 5 x 7 - 1 = 209 = 11 x 19

The first example that p1 * p2 * ... * pn + 1 is non prime is:

      2 x 3 x 5 x 7 x 11 x 13 + 1 = 30031 = 59 x 509

Also see
ncenews.org/view/generic/id/36979/title/Largest_known_prime_number_found
for an interesting article about the largest known prime number (as of
September 28, 2008). It has around 13 million digits!
==========
5. New HowTo: Calculate the prime factors of numbers of the form 999997
in Visual Basic 2005
http://www.vb-helper.com/howto_net_nines_prime.html
http://www.vb-helper.com/HowTo/howto_net_nines_prime.zip

Recently a bunch of fifth graders were asked to think of the largest
prime numbers they could. A lot of them wrote numbers of the form
99999997. It's easy to see that these are not divisible by 2 or 5.
Because the digits aren't divisible by 3, the number is also not
divisible by 3 (most of them knew that trick).

In fact, the first several numbers in this format 7, 97, and 997 are
prime, and it's not easy to tell whether larger numbers of this sort are
prime in your head.

This program writes out several numbers in this format and displays
their prime factors.

The program's Form_Load event handler loops through nine of these
numbers, calling function Factors to display their prime factors.

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
    Dim result As String = ""

    Dim nine As String = "7"
    For i As Integer = 1 To 10
        Dim num As Long = CLng(nine)
        result &= vbCrLf & num.ToString() & " = " & Factors(num)
        nine = "9" & nine
    Next i

    result = result.Substring(vbCrLf.Length)
    txtResult.Text = result
    txtResult.Select(0, 0)
End Sub


The Factors function is the only part of this program that is really
generally useful. It returns a string listing a number's prime factors.

The function starts by pulling factors of 2 out of the number. As long
as the number is divisible by 2, the program adds a 2 to the output and
divides the number by 2.

Eventually the number becomes odd (there may be remaining odd factors
or, if the number is a power of two, then this step reduces the number
to 1). At that point, the program starts pulling out odd factors.
Starting with the test factor value 3, if the test factor value divides
the number evenly, then the program adds it to the output and divides
the test factor from the number. If the test factor doesn't divide the
number evenly, then the program adds 2 to the test factor to consider
the next odd number.

The program repeats this step until the test factor is greater than the
square root of the remaining number. At that point, whatever is left of
the number is prime.

' Return the number's prime factors.
Private Function Factors(ByVal num As Long) As String
    Dim result As String = ""

    ' Take out the 2s.
    Do While num Mod 2 = 0
        ' This is a factor.
        result &= " x 2"
        num \= 2
    Loop

    ' Take out other primes.
    Dim factor As Long = 3
    Do While factor * factor <= num
        If num Mod factor = 0 Then
            ' This is a factor.
            result &= " x " & factor.ToString()
            num \= factor
        Else
            factor += 1
        End If
    Loop

    If num > 1 Then result &= " x " & num
    result = result.Substring(3)

    Return result
End Function

Incidentally, the very next number with this format (the first one
that's hard to verify in your head) is non-prime:

    9997 = 13 x 769

Nice try, kids!
==========
6. New HowTo: Start a program from Sub Main in Visual Basic 2005 and
later
http://www.vb-helper.com/howto_2005_sub_main.html
http://www.vb-helper.com/HowTo/howto_2005_sub_main.zip

When you create a new Windows Forms application, Visual Basic assumes
that it will start from a form and run until that form is closed. To
make the program start by running Sub Main, follow these steps:

    - Create a new Windows Forms project.
    - In the Project Explorer, double-click the "My Project" entry to
open the project's property pages.
    - Uncheck the "Enable application framework" check box (circled in
the figure http://www.vb-helper.com/howto_2005_sub_main.jpg).
    - In the "Startup object" dropdown, select "Sub Main."

The following code shows the example program's Sub Main routine in the
module MainModule.vb. It displays a message box, displays a new form
modally, and then displays another message before ending. (Try using
Show instead of ShowDialog to display the form and see what happens.)

Module MainModule
    Public Sub Main()
        MessageBox.Show("In Sub Main", "Sub Main", _
            MessageBoxButtons.OK, _
            MessageBoxIcon.Information)

        Dim frm As New Form1
        frm.ShowDialog()

        MessageBox.Show("Done", "Done", _
            MessageBoxButtons.OK, _
            MessageBoxIcon.Information)
    End Sub
End Module
==========
++++++++++
<Both>
++++++++++
==========
7. New Links
http://www.vb-helper.com/links.html

Excel Zoom
http://www.excelzoom.com
Microsoft Excel tips, templates, add-ins and help forum.
==========
Archives:
    http://www.topica.com/lists/VBHelper
    http://www.topica.com/lists/VB6Helper
    http://www.topica.com/lists/VBNetHelper

Post questions at:
    http://www.topica.com/lists/VBHelperQA
	
 Previous Message All Messages Next Message 
  Check It Out!

  Topica Channels
 Best of Topica
 Art & Design
 Books, Movies & TV
 Developers
 Food & Drink
 Health & Fitness
 Internet
 Music
 News & Information
 Personal Finance
 Personal Technology
 Small Business
 Software
 Sports
 Travel & Leisure
 Women & Family

  Start Your Own List!
Email lists are great for debating issues or publishing your views.
Start a List Today!

© 2001 Topica Inc. TFMB
Concerned about privacy? Topica is TrustE certified.
See our Privacy Policy.