-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSuperPerfectNumChallenge.cs
More file actions
51 lines (42 loc) · 1.38 KB
/
SuperPerfectNumChallenge.cs
File metadata and controls
51 lines (42 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
//https://www.sololearn.com/Discuss/646305/?ref=app
namespace SoloLearn
{
class Program
{
static void Main(string[] zephyr_koo)
{
var input = Console.ReadLine();
int number;
if (Int32.TryParse(input, out number))
{
Console.WriteLine($"Superperfect number(s) within { number } : { string.Join(", ", GetSuperPerfectNumber(number)) }");
}
}
static IEnumerable<int> GetSuperPerfectNumber(int limit)
{
var perfNumberList = new List<int>();
for (int n = 1; n <= limit; n++)
{
if (GetSumOfFactors(GetSumOfFactors(n)) == n * 2)
{
perfNumberList.Add(n);
}
}
return perfNumberList;
}
static int GetSumOfFactors(int number)
{
var sqrt = Math.Sqrt(number);
return Enumerable
.Range(1, (int)sqrt)
.Where(n => (number % n == 0))
.Sum(n => n + (number / n)) -
(Math.Ceiling(sqrt) == Math.Floor(sqrt) ? (int)sqrt : 0); // remove double count for perfect square
}
}
}