Re: Challenge: Factors + Perfect (Easy - Intermediate)

Posted 29 November 2011 - 02:13 PM

Here ya go. I went for creative over speed, and it turns out the larger perfect number you go, the faster mine gets.

Spoiler

'Is Perfect Number:
Private Function isPerfectNumberViaBinary(num As Long) As Boolean
'Cause why not
Dim binNum As String = Convert.ToString(num, 2)
Dim length As Integer = binNum.Length
'Perfect numbers in binary form have P 1's and P-1'0s, where P is prime
'6 is smallest perfect number aka "110"
If length Mod 2 = 0 OrElse length < 3 Then
Return False
End If
Dim count As Integer = length \ 2 + 1
'make sure all the 1's are on the left and all the 0's on the right
Dim zeroCheck As Integer = binNum.IndexOf("0", 0, count)
Dim onesCheck As Integer = binNum.IndexOf("1", count)
If zeroCheck = -1 AndAlso onesCheck = -1 AndAlso isPrime(count) Then
Return True
End If
Return False
End Function
Private Function isPrime(num As Long) As Boolean
If num = 2 Then
Return True
End If
If num Mod 2 = 0 Then
Return False
End If
For i As Integer = 3 To Math.Sqrt(num) Step 2
If num Mod i = 0 Then
Return False
End If
Next
Return True
End Function'
'Perfect Factors:
Private Shared Function allPerfectFactors(num As Long) As List(Of Long)
Dim factors As New List(Of Long)()
For i As Long = 1 To (num \ 2) - 1
If num Mod i = 0 Then
If isPerfectNumberViaBinary(i) Then
factors.Add(i)
End If
End If
Next
factors.Add(num)
Return factors
End Function

This post has been edited by SwiftStriker00: 29 November 2011 - 02:18 PM

A routine to calculate (or check for) the kind of special number you want would normally be the preferred solution to this type of problem. However, perfect numbers are so sparse that this approach is actually very practical.

Turns out it scales very well too. I thought it would be a huge list if I changed to the Long data type but actually, there are only 8 perfect numbers that fit into the 19 Long decimal digits. According to Wikipedia, there are only 47 known perfect numbers, and the longest is 25956377 digits long (that's the length, not the number itself) so this approach would still be feasible (and the only practical one) if there was no limit to the number of digits.

One shortcoming is that it is not known if the list if perfect numbers has any gaps. In particular, it is not known if there are any odd perfect numbers. This fact hints at how infeasibly slow an approach that algorithmically checks the input number would be for large numbers.

Re: Challenge: Factors + Perfect (Easy - Intermediate)

Posted 29 November 2011 - 03:11 PM

A wee addition:

Spoiler

There is a formula to generate perfect numbers if you know the right prime number to feed them. You'd generate the larger ones using the formula. It's not practical to type or even paste a 25956377 digit number.

Re: Challenge: Factors + Perfect (Easy - Intermediate)

With respect to Part 1 of the Challenge, should Integer be interpreted as any Integer, any Whole Number, any Integer up to the limit of the Int32 datatype or any other subset of all Integers?

Also, if I may, due to the unnerving degree to which mathematicians disagree on the definition of various terms (as well my own OCD and mal-appropriate perfectionism) and I suppose with some risk of creating more confusion in an effort of disambiguity:

A Factor of any given Integer is commonly (and disturbingly) interpreted to mean Prime Factor. This is to such a degree that the term is effectively ambiguous. If one means Factor, one is better off specifying either All Factors or Prime Factors (or whatever other precisely descriptive term is appropriate to the situation). Further, the term Divisor is preferred over Factor because of the common interpretation of Factor to mean Prime Factor. All of this despite Divisor of an integer and Factor of an integer having the same meaning.

A Proper Divisor of a given positive Integer is a positive Divisor of the Integer other than the Integer itself which leaves a Remainder of zero. The terms Perfect Divisor and Perfect Factor are not widely used. However, in determining if a number is a Perfect Number, one evaluates the sum of the Proper Divisors in comparison to the number.

Re: Challenge: Factors + Perfect (Easy - Intermediate)

Posted 06 December 2011 - 08:31 AM

So I suppose I could have just submitted the functions alone days ago, but I proceeded with creating an application (that has the wrong connotation though, little program more appropriate) which might have been a mistake since working my way around the publishing wizard is like throwing darts at a board.

Note: There is a proof that the smallest odd perfect number is at least 10 E 300.
I think 300 zeros might be in the area of super polynomial time to factor. If not it's close. Of course I didn't know that until after I spent a couple of days trying to remember some formula I though I forgot from high school that, as it turns out, doesn't yet exist.
Last week I stumbled across a paper that endeavors to demonstrate that the smallest odd perfect number is at least 10 E 1500. I'd comment on it, but it may as well have been written in Ancient Greek.