| | 1 | | // -------------------------------------------------------------------------------- |
| | 2 | | // Copyright (C) 2025 Eugene Eremeev (also known as Yevhenii Yeriemeieiv). |
| | 3 | | // All Rights Reserved. |
| | 4 | | // -------------------------------------------------------------------------------- |
| | 5 | | // This software is the confidential and proprietary information of Eugene Eremeev |
| | 6 | | // (also known as Yevhenii Yeriemeieiv) ("Confidential Information"). You shall not |
| | 7 | | // disclose such Confidential Information and shall use it only in accordance with |
| | 8 | | // the terms of the license agreement you entered into with Eugene Eremeev (also |
| | 9 | | // known as Yevhenii Yeriemeieiv). |
| | 10 | | // -------------------------------------------------------------------------------- |
| | 11 | |
|
| | 12 | | namespace LeetCode.Algorithms.CheckIfAnyElementHasPrimeFrequency; |
| | 13 | |
|
| | 14 | | public abstract class CheckIfAnyElementHasPrimeFrequencyBase : ICheckIfAnyElementHasPrimeFrequency |
| | 15 | | { |
| | 16 | | protected const int Count = 101; |
| | 17 | |
|
| 1 | 18 | | private static readonly bool[] Primes = GeneratePrimes(Count); |
| | 19 | |
|
| | 20 | | public abstract bool CheckPrimeFrequency(int[] nums); |
| | 21 | |
|
| | 22 | | protected static bool IsPrime(int number) |
| 119 | 23 | | { |
| 119 | 24 | | return Primes[number]; |
| 119 | 25 | | } |
| | 26 | |
|
| | 27 | | private static bool[] GeneratePrimes(int max) |
| 1 | 28 | | { |
| 1 | 29 | | var isPrime = new bool[max + 1]; |
| | 30 | |
|
| 1 | 31 | | if (max >= 2) |
| 1 | 32 | | { |
| 1 | 33 | | isPrime[2] = true; |
| 1 | 34 | | } |
| | 35 | |
|
| 102 | 36 | | for (var i = 3; i <= max; i += 2) |
| 50 | 37 | | { |
| 50 | 38 | | isPrime[i] = true; |
| 50 | 39 | | } |
| | 40 | |
|
| 10 | 41 | | for (var p = 3; p * p <= max; p += 2) |
| 4 | 42 | | { |
| 4 | 43 | | if (!isPrime[p]) |
| 1 | 44 | | { |
| 1 | 45 | | continue; |
| | 46 | | } |
| | 47 | |
|
| 62 | 48 | | for (var multiple = p * p; multiple <= max; multiple += p * 2) |
| 28 | 49 | | { |
| 28 | 50 | | isPrime[multiple] = false; |
| 28 | 51 | | } |
| 3 | 52 | | } |
| | 53 | |
|
| 1 | 54 | | if (max >= 2) |
| 1 | 55 | | { |
| 1 | 56 | | isPrime[2] = true; |
| 1 | 57 | | } |
| | 58 | |
|
| 1 | 59 | | if (max >= 3) |
| 1 | 60 | | { |
| 1 | 61 | | isPrime[3] = true; |
| 1 | 62 | | } |
| | 63 | |
|
| 1 | 64 | | if (max >= 0) |
| 1 | 65 | | { |
| 1 | 66 | | isPrime[0] = false; |
| 1 | 67 | | } |
| | 68 | |
|
| 1 | 69 | | if (max >= 1) |
| 1 | 70 | | { |
| 1 | 71 | | isPrime[1] = false; |
| 1 | 72 | | } |
| | 73 | |
|
| 1 | 74 | | return isPrime; |
| 1 | 75 | | } |
| | 76 | | } |