Skip to content

Instantly share code, notes, and snippets.

@kashwaa
Last active August 29, 2015 14:02
Show Gist options
  • Save kashwaa/e35870e04761e0a16c9e to your computer and use it in GitHub Desktop.
Save kashwaa/e35870e04761e0a16c9e to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace euler268
{
class Program
{
static void Main(string[] args)
{
Dictionary<UInt64, List<int>> numbers = new Dictionary<UInt64, List<int>>();
var primes = PrimesLessThan100();
List<int> ld = new List<int>();
int result = 0;
for (UInt64 i = (UInt64)Math.Pow(10, 0); i < (UInt64)Math.Pow(10, 4); i++)
{
var k=IsDevisableBy4Primes(primes,i);
if (k!=null)
{
result++;
numbers.Add(i,k);
}
}
var test = GetPermutationsMultip(primes,(long) Math.Pow(10, 4));
double r = 0;// test.Count();
List<int> multiples = new List<int>();
//foreach (var item in GetPermutations(primes,4))
//{
// long multipvalue = 1;
// foreach (var i in item)
// {
// multipvalue *= i;
// }
// if (multipvalue>100000)
// {
// continue;
// }
// long g = (long)(Math.Pow(10, 4) / multipvalue)-1;
// if (g >= item.Min()) g-= 1;
// r += g;
//}
foreach (var item in test)
{
if (item > Math.Pow(10, 4)) break;
long g =(long) (Math.Pow(10, 4) / item);
//if (g >1) g -= 1;
//for (int i = 2; i < g; i++)
//{
// if (IsPrime(i) && item % i == 0)
// {
// g -= 1;
// break;
// }
//}
r += g;
for (int i = 1; i <= g; i++)
{
// if(IsPrime(i)&& item%i==0)
multiples.Add(i * item);
}
}
//multiples.Sort();
// multiples.Reverse();
Console.WriteLine(r+primes.Count());
}
public static List<int> GetPermutationsMultip(List<int> input,long limit)
{
List<int> res = new List<int>();
foreach (var item in GetPermutations(input, 4))
{
int result=1;
foreach (var i in item)
{
result *= i;
}
res.Add(result);
}
return res.OrderBy(c=>c).Where(c=>c<limit).ToList();
}
static IEnumerable<IEnumerable<T>> GetPermutations<T>(IEnumerable<T> items, int count)
{
int i = 0;
foreach (var item in items)
{
if (count == 1)
yield return new T[] { item };
else
{
foreach (var result in GetPermutations(items.Skip(i + 1), count - 1))
yield return new T[] { item }.Concat(result);
}
++i;
}
}
public static List<int> IsDevisableBy4Primes(List<int> primes,UInt64 n)
{
List<int> divisables = new List<int>();
int numOfDevisables = 0;
foreach (var item in primes)
{
if (n % (UInt64)item == 0)
{
numOfDevisables++;
divisables.Add(item);
}
if (numOfDevisables==4)
{
return divisables;
}
}
return null;
}
public static List<int> PrimesLessThan100()
{
List<int> result = new List<int>();
for (int i = 2; i < 100; i++)
{
if (IsPrime(i))
{
result.Add(i);
}
}
return result;
}
public static bool IsPrime(int n)
{
int result = 0;
int start = 2;
int end = n;
for (int i = start; i < end; i += 1)
{
if (n % i == 0)
{
start = i;
end = n / i;
result += start == end ? 1 : 2;
}
}
return result == 0;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment