0% found this document useful (0 votes)
40 views

Module 1

This document contains the code for a Visual Basic .NET program that finds perfect numbers. It defines functions to check if a number is perfect, get the nth digit of a number, check properties of digits, and more. The main method gets user input for the maximum number to check, then loops through numbers up to that max, checking if each is perfect and adding it to a list if so. It outputs the final list of perfect numbers and timing information.

Uploaded by

Greg Mavhunga
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views

Module 1

This document contains the code for a Visual Basic .NET program that finds perfect numbers. It defines functions to check if a number is perfect, get the nth digit of a number, check properties of digits, and more. The main method gets user input for the maximum number to check, then loops through numbers up to that max, checking if each is perfect and adding it to a list if so. It outputs the final list of perfect numbers and timing information.

Uploaded by

Greg Mavhunga
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

1 C:\Users\Gregory Mavhunga\Documents\New ...\getPerfectNumbers\Module1.

vb
Module Module1
Sub Main()
Dim key As String = Nothing
'While (Console.
Console.WriteLine("Hello world")
Dim startTime As Date, endtime As Date
Dim max As Integer
Dim s As List(Of Integer)
s = New List(Of Integer)
Console.WriteLine("Enter search max")
max = CInt(Console.ReadLine())
startTime = Now
Console.WriteLine(startTime.Ticks)
For i As Integer = 1 To max
If IsPerfectNum(i) = True Then
s.Add(i)
End If
If ((8191 Mod i + 1) = 0) Then Console.WriteLine(i)
For lev As Integer = 0 To CInt(Math.Floor(Math.Log10(max)))
If i = Math.Pow(10, lev) Then
Console.WriteLine(Math.Pow(10, lev) & vbTab & ((Now.Ticks - startTime.
Ticks)) & " ticks") ' & vbTab & DateDiff(DateInterval.Second, startTime, endtime) & "
seconds"
End If
If (Math.Log10(i) > 6) Then
If (Math.Log10(i) = Math.Log10(5 * Math.Pow(10, Math.Log10(i)))) Then
Console.WriteLine(i & vbTab & ((Now.Ticks - startTime.Ticks)) & "
ticks") '& vbTab & DateDiff(DateInterval.Second, startTime, endtime) & " seconds")
End If
End If
Next
Next
endtime = Now
For Each num As Integer In s
Console.WriteLine("The number " & vbTab & num & vbTab & " is a perfect number"
)
Next
Dim ss(0 To 10) As String
Console.WriteLine(endtime.Ticks)
Console.WriteLine("Time elapsed = : " & (endtime.Ticks - startTime.Ticks) & "
ticks" & vbTab & DateDiff(DateInterval.Second, startTime, endtime) & " seconds")
Console.WriteLine("Run program again, press [Y] to exit?")
Console.ReadKey(False)

End Sub
Public Function IsPerfectNum(testNum As Integer) As Boolean
'testNum = 33550336
If (testNum = 6) Then Return True
If (testNum Mod 2 = 1) Then Return False 'eliminate all odd numbers
If (((testNum - 6) Mod 10) > 2) Then Return False 'eliminate all numbers not
ending in 6 or 8
'If (FirstDigit(testNum) <> 8) Then
' If (FirstDigit(testNum) <> 6) Then
' Return False
' End If
'End If
If (digitN(1, testNum) = 8) Then
If (digitN(2, testNum) <> 2) Then Return False
End If
'If (testNum Mod 100) <> 28 Then
' If (digitN(1, testNum) <> 6) Then
' Return False
' End If
'End If
If (Not checkDigits(testNum)) Then Return False
2 C:\Users\Gregory Mavhunga\Documents\New ...\getPerfectNumbers\Module1.vb
Dim sumOfDivisors As Integer, i As Integer
sumOfDivisors = 0
' For i = 1 To CInt(testNum / 2)
' If (sumOfDivisors > testNum) Then
' Return False
' Else
' If (testNum Mod i) = 0 Then
' sumOfDivisors += i
' End If
'
' End If
' Next
' For i = 1 To CInt(testNum / 2)
For i = 1 To CInt(Math.Sqrt(testNum))
If (sumOfDivisors > 2*testNum) Then
Return False
Else
If (testNum Mod i) = 0 Then
sumOfDivisors += i
sumOfDivisors += testNum\i
End If
End If
Next
IsPerfectNum = (sumOfDivisors = 2*testNum)
End Function
Public Function isPerfect(ByVal testNum As Integer) As Boolean
If (testNum = 6) Then Return True
If (testNum Mod 2 = 1) Then Return False 'eliminate all odd numbers
If (((testNum - 6) Mod 10) > 2) Then Return False 'eliminate all numbers not
ending in 6 or 8
'If (FirstDigit(testNum) <> 8) Then
' If (FirstDigit(testNum) <> 6) Then
' Return False
' End If
'End If
If (digitN(1, testNum) = 8) Then
If (digitN(2, testNum) <> 2) Then Return False
End If
'If (testNum Mod 100) <> 28 Then
' If (digitN(1, testNum) <> 6) Then
' Return False
' End If
'End If
If (Not checkDigits(testNum)) Then Return False
Dim i As Integer = 1
Dim sum As Integer = 0
While (testNum > i)
If ((testNum Mod i) = 0) Then sum += i
i += 1
End While
'while ( n > i ) {
' if ( n % i == 0 ) {
' sum = sum + i;
' }
' i++;
'}
Return (sum = testNum)
End Function
Public Function digitN(i As Integer, num As Integer) As Integer
If i = 1 Then Return num Mod 10
Dim d As Double = Math.Pow(10, CInt(i - 1))
Dim f As Double = Math.Floor(num / d)
3 C:\Users\Gregory Mavhunga\Documents\New ...\getPerfectNumbers\Module1.vb
Return CInt(f Mod 10)
End Function
Public Function checkDigits(num As Integer) As Boolean
'If (((nModk(num, 9)) = 0) And (num <> 0)) Then
' Return False
'Else
' Return ((nModk(num, 9) = 1))
'End If
If (((num Mod 9) = 0) And (num <> 0)) Then
Return False
Else
Return ((num Mod 9) = 1)
End If
'Dim tt As u
'Return (num - (9 * ((num - 1) / 9)) = 1)
'num = 496
'If (num < 10) Then Return (num = 1)
'Dim sumOfDigits As Integer = 0
'While num >= 10
' sumOfDigits = 0
' For i As Integer = 1 To CInt(Math.Floor(Math.Log10(num)) + 1)
' 'sumOfDigits += digitN(i, num)
' sumOfDigits += num Mod 10
' num = (num \ 10)
' 'num = 496 \ 10
' Next1
' 'While (num > 0)
' ' 'sumOfDigits += digitN(i, num)
' ' sumOfDigits += num Mod 10
' ' num = (num \ 10)
' ' 'num = 496 \ 10
' 'End While
' num = sumOfDigits
'End While
'Return (num = 1)

End Function
Public Function nModk(ByVal n As Integer, ByVal k As Integer) As Integer
While (n > k)
n -= k
End While
Return n 'Mod k
End Function
Public Function FirstDigit(num As Integer) As Integer
While (num > 10)
num -= 10
End While
Return num
End Function

Public Function IsPrimeNum(n As Integer) As Boolean
Dim status As Boolean = True
Dim i As Integer = 2
While i<n
If n Mod i <> 0 Then
status = True
Else
status = False
End If
i+=1
End While
If (n=1) Then Return False
4 C:\Users\Gregory Mavhunga\Documents\New ...\getPerfectNumbers\Module1.vb

Return status
End Function




End Module

You might also like