Skip to content

Instantly share code, notes, and snippets.

@bashmohandes
Last active February 28, 2018 20:33
Show Gist options
  • Save bashmohandes/0c1a9f1be213e9a0712530039ff0d932 to your computer and use it in GitHub Desktop.
Save bashmohandes/0c1a9f1be213e9a0712530039ff0d932 to your computer and use it in GitHub Desktop.
Video coding session: https://youtu.be/HU_lpuK1-0E
using System;
using System.Linq;
using System.Collections.Generic;
public class SumSquaredDivisors
{
public static string listSquared(long m, long n)
{
var result = new List<string>();
for (var i = m; i <= n; i++)
{
var sumOfSquaredDivs = SumSquaredDivisors.Divisors(i).Select(d => d * d).Sum();
if (IsSquared(sumOfSquaredDivs))
{
result.Add($"[{i}, {sumOfSquaredDivs}]");
}
}
return "[" + String.Join(", ", result) + "]";
}
private static IEnumerable<long> Divisors(long input)
{
for (var i = 1; i <= input; i++)
{
if (input % i == 0)
{
yield return i;
}
}
}
private static bool IsSquared(long number)
{
var sqrt = Math.Sqrt(number);
return ((int)sqrt * (int)sqrt) == number;
}
}
@AhmedElMetwally
Copy link

@s-ff
Copy link

s-ff commented Feb 28, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment