143 lines
3.8 KiB
C#
143 lines
3.8 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Program
|
|
{
|
|
class Program
|
|
{
|
|
static void Main(string[] args)
|
|
{
|
|
List<int> primes = new List<int>();
|
|
|
|
for (int i = 1000; i < 10000; i++)
|
|
{
|
|
if (IsPrime(i))
|
|
{
|
|
primes.Add(i);
|
|
string result = string.Join("", GetSortedDigits(i));
|
|
Console.WriteLine($"({i},{result}),");
|
|
}
|
|
}
|
|
|
|
List<int[]> primesWithDecimalValuesOrdered = new List<int[]>();
|
|
|
|
foreach (int i in primes)
|
|
{
|
|
primesWithDecimalValuesOrdered.Add(GetSortedDigits(i));
|
|
}
|
|
|
|
var keyValuePairs = primesWithDecimalValuesOrdered
|
|
.GroupBy(x => x, new IntArrayComparer())
|
|
.Select(g => new KeyValuePair<int[], int>(g.Key, g.Count()))
|
|
.Where(kvp => kvp.Value >= 6)
|
|
.ToList().Count();
|
|
|
|
// Print the key-value pairs
|
|
/*
|
|
foreach (var kvp in keyValuePairs)
|
|
{
|
|
Console.WriteLine($"Key: {string.Join("", kvp.Key)}, Value: {kvp.Value}");
|
|
}
|
|
*/
|
|
|
|
Console.WriteLine(keyValuePairs);
|
|
|
|
|
|
|
|
|
|
|
|
int[] digits = { 0, 1, 2, 2, 3, 3, 4, 5, 6, 7, 8, 9 };
|
|
int length = 4;
|
|
|
|
PermutationGenerator generator = new PermutationGenerator();
|
|
generator.GeneratePermutations(digits, length);
|
|
//generator.PrintGroupedPermutations();
|
|
|
|
// Get groups with at least 6 primes
|
|
var groupsWithAtLeast6Primes = generator.GroupedPermutations
|
|
.Where(group => group.Value.Count(permutation => IsPrime(int.Parse(string.Join("", permutation)))) >= 6)
|
|
.ToList();
|
|
|
|
// Print the groups with at least 6 primes
|
|
foreach (var group in groupsWithAtLeast6Primes)
|
|
{
|
|
Console.WriteLine($"Group: {group.Key}");
|
|
foreach (var permutation in group.Value)
|
|
{
|
|
Console.WriteLine(string.Join("", permutation));
|
|
}
|
|
Console.WriteLine();
|
|
}
|
|
|
|
|
|
}
|
|
|
|
static int[] GetSortedDigits(int number)
|
|
{
|
|
// Convert the number to a string to access each digit
|
|
string numberString = number.ToString();
|
|
|
|
// Convert each character to an integer
|
|
int[] digits = numberString.Select(c => int.Parse(c.ToString())).ToArray();
|
|
|
|
// Sort the array of integers
|
|
Array.Sort(digits);
|
|
|
|
return digits;
|
|
}
|
|
|
|
static bool IsPrime(int number)
|
|
{
|
|
if (number <= 1) return false;
|
|
if (number == 2) return true;
|
|
if (number % 2 == 0) return false;
|
|
|
|
int boundary = (int)Math.Floor(Math.Sqrt(number));
|
|
|
|
for (int i = 3; i <= boundary; i += 2)
|
|
{
|
|
if (number % i == 0) return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
}
|
|
|
|
class IntArrayComparer : IEqualityComparer<int[]>
|
|
{
|
|
public bool Equals(int[] x, int[] y)
|
|
{
|
|
if (x == null || y == null)
|
|
return false;
|
|
|
|
if (x.Length != y.Length)
|
|
return false;
|
|
|
|
for (int i = 0; i < x.Length; i++)
|
|
{
|
|
if (x[i] != y[i])
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
public int GetHashCode(int[] obj)
|
|
{
|
|
if (obj == null)
|
|
return 0;
|
|
|
|
int hash = 17;
|
|
foreach (int i in obj)
|
|
{
|
|
hash = hash * 31 + i;
|
|
}
|
|
|
|
return hash;
|
|
}
|
|
}
|
|
}
|