Perfect number
This is in the exercise Question 15. I have no understanding about perfect numbers. Please help me with this program.
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
VulpesPosted Oct 14, 2012, 9:55 AM
It follows that 1 will always be a divisor of a perfect number and so this is why it's being added to the factors list.
Posted Oct 19, 2013, 10:45 AM
Posted Oct 19, 2013, 10:44 AM
VulpesPosted Oct 18, 2013, 6:28 PM
Posted Oct 18, 2013, 1:25 PM
using System;
class Factors
{
static void Main()
{
while (true)
{
Console.Write("Input a positive number or 0 to quit: ");
int i = int.Parse(Console.ReadLine());
if (i <= 0) return;
Console.WriteLine("The factors of {0} are as follows", i);
Console.Write(" 1");
int sum = 1;
for (int j = 2; j <= i / 2; j++)
{
if (i % j == 0)
{
Console.Write(" + {0}", j);
sum = sum + j;
}
}
Console.WriteLine(" = {0}", sum);
if (i == sum)
Console.WriteLine(i + " is a perfect number");
else
Console.WriteLine(i + " is not a perfect number");
Console.WriteLine("\n");
}
}
}
VulpesPosted Oct 16, 2013, 12:37 PM
Posted Oct 16, 2013, 12:34 PM
For example factors of number 16(=i) is within the range of 1 to 8, that is why i/2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
VulpesPosted Oct 16, 2013, 10:24 AM
The following is off the top of my head as I don't have .NET on this machine:
{
int i = int.Parse(Console.ReadLine());
if (i <= 0) return;
Console.WriteLine();
Posted Oct 16, 2013, 10:03 AM
using System;
class Program
{
static void Main(string[] args)
{
for (int i = 2; i <= 10; i++)
{
for (int j = 2; j <= i / 2; j++)
{
Console.Write(j + " ");//2, 2, 2 3 2 3 2 3 4 2 3 4 2 3 4 5
}
}
Console.Read();
}
}
VulpesPosted Oct 15, 2013, 1:54 PM
Then the remaining factor of the number would have to be somewhere between 1 and 2 which is impossible as we're only dealing with integers.
So, we can therefore conclude that all factors must be <= i/2.
Posted Oct 15, 2013, 10:55 AM
VulpesPosted Oct 14, 2013, 12:00 PM
If the number is even, the highest possible factor will be i / 2.
If the number is odd, the highest possible factor will be i / 2 - 1.
So, if we iterate from 2 up to i / 2 and test all numbers in that range, we will capture all the remaining factors (apart from 1) whether the number is odd or even.
Posted Oct 14, 2013, 9:46 AM
All perfect numbers can be divided by 1 without any remainder. Therefore adding factors.Add(1); can be understood.
Please explain the function of the highlighted one.
using System;
using System.Collections.Generic;
class Perfect
{
static void Main()
{
List
Console.WriteLine("The perfect numbers less than or equal to 1000 are :\n");
for (int i = 2; i <= 1000; i++)
{
factors.Clear();
factors.Add(1);
for (int j = 2; j <= i / 2; j++)
{
if (i % j == 0) factors.Add(j);
}
int sum = 0;
for (int k = 0; k < factors.Count; k++)
{
sum += factors[k];
}
if (sum == i)
{
for (int l = 0; l < factors.Count - 1; l++)
{
Console.Write("{0} + ", factors[l]);
}
Console.Write("{0} = {1}\n", factors[factors.Count - 1], i);
}
}
Console.Read();
}
}
Posted Aug 10, 2012, 4:45 PM
VulpesPosted Aug 10, 2012, 2:30 PM