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.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend4days leftRegister
VulpesRank #55 · 29.2K reputationPosted Oct 14, 2012, 5:55 AM EDT
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.
VulpesRank #55 · 29.2K reputationPosted Aug 10, 2012, 10:30 AM EDT
Posted Aug 10, 2012, 12:45 PM EDT
Posted Oct 14, 2013, 5:46 AM EDT
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();
}
}
VulpesRank #55 · 29.2K reputationPosted Oct 14, 2013, 8:00 AM EDT
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 15, 2013, 6:55 AM EDT
VulpesRank #55 · 29.2K reputationPosted Oct 15, 2013, 9:54 AM EDT
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 16, 2013, 6:03 AM EDT
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();
}
}
VulpesRank #55 · 29.2K reputationPosted Oct 16, 2013, 6:24 AM EDT
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, 8:34 AM EDT
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
VulpesRank #55 · 29.2K reputationPosted Oct 16, 2013, 8:37 AM EDT
Posted Oct 18, 2013, 9:25 AM EDT
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");
}
}
}
VulpesRank #55 · 29.2K reputationPosted Oct 18, 2013, 2:28 PM EDT
Posted Oct 19, 2013, 6:44 AM EDT
Posted Oct 19, 2013, 6:45 AM EDT