-
Apr 9th, 2018, 01:51 PM
#1
Thread Starter
New Member
Perfect Number
Hii,
Perhaps there is something you can help me with though?
I have to write an order in VBA to find the next perfect number
please help me
-
Apr 9th, 2018, 01:57 PM
#2
Thread Starter
New Member
Re: Perfect Number
My number currently is 28
-
Apr 9th, 2018, 02:02 PM
#3
Re: Perfect Number
Welcome to VBForums
Thread moved from the 'VB.Net' forum to the 'Office Development/VBA' forum.
-
Apr 9th, 2018, 05:45 PM
#4
New Member
Re: Perfect Number
Hello maor1212,
Code:
Sub PerfectNumbers()
' Algorithm by John Knoderer at The Math Forum
' Adapted to VBA by Leith Ross
' http://mathforum.org/dr.math/faq/faq.perfect.html
' // Perfect numbers derived from Mersenne prime numbers.
For Each pM In Array(2, 3, 5, 7, 13, 17, 19, 31)
pN = 2 ^ (pM - 1) * (2 ^ pM - 1)
MsgBox pN, vbOKOnly, "Perfect Numbers"
Next pM
End Sub
-
Apr 9th, 2018, 06:12 PM
#5
Re: Perfect Number
Or, the long way:
Code:
Sub findPerf()
Dim ws As Worksheet
Dim J As Long
Dim K As Long
Dim nums() As Long
Dim cnt As Long
Dim tot As Long
Dim perfects() As Long
Dim pCnt As Long
Dim myMax As Long
Set ws = ActiveSheet
With ws
myMax = 49999 'set to something like 8500 if you want
pCnt = -1
For J = 1 To myMax
tot = 0
cnt = -1
For K = 2 To J - 1
If J Mod K = 0 Then
cnt = cnt + 1
ReDim Preserve nums(cnt)
nums(cnt) = K
tot = tot + K
End If
Next K
If tot + 1 = J Then
pCnt = pCnt + 1
ReDim Preserve perfects(pCnt)
perfects(pCnt) = tot + 1
End If
Next J
For J = 0 To pCnt
.Range("b" & J + 1).Value = perfects(J)
Next J
End With
End Sub
-
Apr 9th, 2018, 07:33 PM
#6
Re: Perfect Number
Originally Posted by vbfbryce
Or, the long way:
Your "For K" loop can be improved by stopping at the greatest possible relevant value:
-
Apr 10th, 2018, 02:22 AM
#7
Thread Starter
New Member
Re: Perfect Number
Originally Posted by Leith Ross
Hello maor1212,
Code:
Sub PerfectNumbers()
' Algorithm by John Knoderer at The Math Forum
' Adapted to VBA by Leith Ross
' http://mathforum.org/dr.math/faq/faq.perfect.html
' // Perfect numbers derived from Mersenne prime numbers.
For Each pM In Array(2, 3, 5, 7, 13, 17, 19, 31)
pN = 2 ^ (pM - 1) * (2 ^ pM - 1)
MsgBox pN, vbOKOnly, "Perfect Numbers"
Next pM
End Sub
I am sorry if I didn't explain myself well
I have a range ("a1") [28] in excel, I have to find the next perfect with using VBA
-
Apr 10th, 2018, 02:23 AM
#8
Thread Starter
New Member
Re: Perfect Number
I am sorry if I didn't explain myself well
I have a range ("a1") [28] in excel, I have to find the next perfect with using VBA
-
Apr 10th, 2018, 02:39 AM
#9
Thread Starter
New Member
Re: Perfect Number
I wrote this code but it doesn't work
Sub riddle()
Dim no As Integer
Dim sum As Integer
Dim I As Integer
no = Range("a1") +1 // range ("a1") =28 so it start from the next number
yes= range("a1")
while no <> yes
For i = 1 To no
If no Mod i = 0 Then
sum = sum + i
End If
Next i
If sum / 2 = no Then
yes = no
Else
no=no+1
End If
wend
range("b1")=yes
End Sub
-
Apr 10th, 2018, 04:30 AM
#10
Re: Perfect Number
do you want to calculate the next perfect number or just add it to the cell from a list of perfect numbers, which within the range of vb variables would be quite small
you can use the code posted by leith to calculate the next perfect number
the result from his code, from which you can also have a list of perfect number
6
28
496
8128
33550336
8589869056
137438691328
2.30584300813995E+18
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Apr 10th, 2018, 05:19 AM
#11
Thread Starter
New Member
Re: Perfect Number
Originally Posted by westconn1
do you want to calculate the next perfect number or just add it to the cell from a list of perfect numbers, which within the range of vb variables would be quite small
you can use the code posted by leith to calculate the next perfect number
the result from his code, from which you can also have a list of perfect number
I need that VBA calculate the next perfect number
for example if cell a1 = 28 then cell a2= 496 or if a1 =496 then a2=8128 - by calculating
-
Apr 10th, 2018, 06:31 AM
#12
Re: Perfect Number
Well, since everyone's already posted an answer...
Code:
Public Function NextPerfect(ByVal Prev As Double) As Double
Dim CurDouble As Double
Dim CurTotal As Double
Dim CurPerfect As Double
CurDouble = 1
CurTotal = 1
Do While CurPerfect <= Prev
CurDouble = CurDouble * 2
CurTotal = CurTotal + CurDouble
If IsPrime(CurTotal) Then
CurPerfect = CurDouble * CurTotal
End If
Loop
NextPerfect = CurPerfect
End Function
Public Function IsPrime(ByVal Number As Double) As Boolean
Dim i As Double
If Number <= 1 Or (Number - Int(Number)) <> 0 Then
Exit Function
ElseIf Number <= 3 Then
IsPrime = True
Exit Function
ElseIf (Number Mod 2 = 0) Or (Number Mod 3 = 0) Then
Exit Function
End If
i = 5
Do While i * i <= Number
If (Number Mod i = 0) Or (Number Mod (i + 2) = 0) Then
Exit Function
End If
i = i + 6
Loop
IsPrime = True
End Function
-
Apr 10th, 2018, 10:28 AM
#13
Re: Perfect Number
Good catch! I had that in there at one point, somehow didn't end up in my post!
(Responding to post 6)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|