Skip to content

Instantly share code, notes, and snippets.

@reuke
Last active October 3, 2017 17:23
Show Gist options
  • Save reuke/93def4b3893ba5f5a52af7f0a6e356c2 to your computer and use it in GitHub Desktop.
Save reuke/93def4b3893ba5f5a52af7f0a6e356c2 to your computer and use it in GitHub Desktop.
using System;
using System.Linq;
using System.Collections.Generic;
public class IntPart
{
public static string Part(long n)
{
List<int> prod = GetMimic((int) n);
return $"Range: {prod.Last() - 1} Average: {prod.Average():0.00} Median: {((double)(prod.ElementAt(prod.Count() / 2) + prod.ElementAt((prod.Count() - 1) / 2)))/2:0.00}";
}
private static List<int> GetMimic(int num)
{
List<int> primes = Enumerable.Range(2, Int32.MaxValue - 1).Where(n => Enumerable.Range(2, (int)Math.Sqrt(n) - 1).All(divisor => n % divisor != 0)).TakeWhile(p => p <= num).ToList();
List<int> mimic = new List<int>();
if (num > 1)
mimic.Add(1);
foreach (int prime in primes)
{
mimic.Add(prime);
AddMimic(primes, mimic, prime, prime, num - prime);
}
mimic.Sort();
return mimic;
}
private static void AddMimic(List<int> primes, List<int> mimic, int add, int from, int to)
{
foreach (int step in primes.Where(p => p >= from && p <= to))
{
int cur = add * step;
mimic.Add(cur);
if (step * 2 <= to)
AddMimic(primes, mimic, cur, step, to - step);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment