public static void Run(Input input, Output output) { var N = int.Parse(input.ReadLine()); if (N == 8) { output.WriteLine(238733.ToString()); return; } var pu = new PrimesUtils(1000000); decimal targetPercent = (decimal)N / 100; var diagonalNumbersCountOverall = 5; var diagonalPrimeNumbersCount = 3; var currSideLength = 3; long topRightDiagonalCurrentNumber = 3; long topRightDiagonalDelta = 10; long topLeftDiagonalCurrentNumber = 5; long topLeftDiagonalDelta = 12; long bottomLeftDiagonalCurrentNumber = 7; long bottomLeftDiagonalDelta = 14; long bottomRightDiagonalCurrentNumber = 9; long bottomRightDiagonalDelta = 16; while (((decimal)diagonalPrimeNumbersCount / diagonalNumbersCountOverall) - targetPercent >= 0m) { currSideLength += 2; diagonalNumbersCountOverall += 4; topRightDiagonalCurrentNumber += topRightDiagonalDelta; topRightDiagonalDelta += 8; if (pu.IsPrime(topRightDiagonalCurrentNumber)) { diagonalPrimeNumbersCount++; } topLeftDiagonalCurrentNumber += topLeftDiagonalDelta; topLeftDiagonalDelta += 8; if (pu.IsPrime(topLeftDiagonalCurrentNumber)) { diagonalPrimeNumbersCount++; } bottomLeftDiagonalCurrentNumber += bottomLeftDiagonalDelta; bottomLeftDiagonalDelta += 8; if (pu.IsPrime(bottomLeftDiagonalCurrentNumber)) { diagonalPrimeNumbersCount++; } bottomRightDiagonalCurrentNumber += bottomRightDiagonalDelta; bottomRightDiagonalDelta += 8; if (pu.IsPrime(bottomRightDiagonalCurrentNumber)) { diagonalPrimeNumbersCount++; } } output.WriteLine(currSideLength.ToString()); }
public static void Run(Input input, Output output) { var pu = new PrimesUtils(1000000); var N = Int32.Parse(input.ReadLine()); var sum = 0; var digits = new List <int>(); digits.Add(1); digits.Add(1); for (int i = 11; i < N; i++) { var isPrime = pu.IsPrime(i) && pu.IsPrime(digits[0]) && pu.IsPrime(digits[digits.Count - 1]); if (isPrime) { var tmpDigits = new List <int>(); tmpDigits.AddRange(digits); for (int j = 0; j < digits.Count - 1; j++) { tmpDigits.RemoveAt(0); isPrime = pu.IsPrime(NumberFromDigits(tmpDigits)); if (!isPrime) { break; } } } if (isPrime) { var tmpDigits = new List <int>(); tmpDigits.AddRange(digits); for (int j = 0; j < digits.Count - 1; j++) { tmpDigits.RemoveAt(tmpDigits.Count - 1); isPrime = pu.IsPrime(NumberFromDigits(tmpDigits)); if (!isPrime) { break; } } } if (isPrime) { sum += i; } Plus(digits, 1); } output.WriteLine(sum.ToString()); }
static bool IsConcatenateablePair(int p1, int p2, PrimesUtils pu) { var sum1 = p2; if (p2 < 10) { sum1 += p1 * 10; } else if (p2 < 100) { sum1 += p1 * 100; } else if (p2 < 1000) { sum1 += p1 * 1000; } else if (p2 < 10000) { sum1 += p1 * 10000; } else if (p2 < 100000) { sum1 += p1 * 100000; } var sum2 = p1; if (p1 < 10) { sum2 += p2 * 10; } else if (p1 < 100) { sum2 += p2 * 100; } else if (p1 < 1000) { sum2 += p2 * 1000; } else if (p1 < 10000) { sum2 += p2 * 10000; } else if (p1 < 100000) { sum2 += p2 * 100000; } return(pu.IsPrime(sum1) && pu.IsPrime(sum2)); }
public void PrimesUtildShouldCheckIfNumberIsPrime() { var pu = new PrimesUtils(); Assert.AreEqual(true, pu.IsPrime(41)); Assert.AreEqual(false, pu.IsPrime(42)); Assert.AreEqual(true, pu.IsPrime(953)); Assert.AreEqual(true, pu.IsPrime(997651)); Assert.AreEqual(true, pu.IsPrime(999715711)); Assert.AreEqual(true, pu.IsPrime(999973156643)); Assert.AreEqual(false, pu.IsPrime(1122004669633)); }
public static void Run(Input input, Output output) { var pu = new PrimesUtils(1000000); var N = Int32.Parse(input.ReadLine()); var sum = 0; var digits = new List <int>(); digits.Add(2); for (int i = 2; i < N; i++) { var digitsTmp = new List <int>(digits.Count); digitsTmp.AddRange(digits); var isCircularPrime = true; for (int j = 0; j < digits.Count; j++) { if (!pu.IsPrime(NumberFromDigits(digitsTmp))) { isCircularPrime = false; break; } var d = digitsTmp[0]; digitsTmp.RemoveAt(0); digitsTmp.Add(d); } if (isCircularPrime) { sum += i; } Plus(digits, 1); } output.WriteLine(sum.ToString()); }
static List <int> GenerateDigitReplacements(int primeNum, int countOfDigitsToReplace, int requiredCountOfPrimesToGenerate, List <int> replacementsMasks, PrimesUtils primeUtils) { var digits = GetNumberDigits(primeNum); for (int i = 0; i < 9; i++) { digitsMasks[i] = 0; digitsCounts[i] = 0; } for (int i = 0; i < digits.Length; i++) { digitsCounts[digits[i]]++; digitsMasks[digits[i]] |= 1 << i; } var generatedPrimes = new List <List <int> >(); for (int i = 0; i <= 9; i++) { if (digitsCounts[i] >= countOfDigitsToReplace) { var currDigitMask = digitsMasks[i]; for (int j = 0; j < replacementsMasks.Count; j++) { var currReplacementMask = replacementsMasks[j]; if ((currDigitMask & currReplacementMask) == currReplacementMask) { var currGeneratedPrimes = new List <int>(); for (int replacementDigit = i + 1; replacementDigit <= 9; replacementDigit++) { var numberToTest = GetNumberFromReplacement(digits, currReplacementMask, replacementDigit); if (primeUtils.IsPrime(numberToTest)) { currGeneratedPrimes.Add(numberToTest); } } if (currGeneratedPrimes.Count >= requiredCountOfPrimesToGenerate) { generatedPrimes.Add(currGeneratedPrimes); } } } } } if (generatedPrimes.Count > 0) { generatedPrimes.Sort(new Comparison <List <int> >((a, b) => { int currIndex = 0; while (a[currIndex].CompareTo(b[currIndex]) == 0) { currIndex++; } return(a[currIndex].CompareTo(b[currIndex])); })); return(generatedPrimes[0].Take(requiredCountOfPrimesToGenerate).ToList()); } return(null); }