1/* 2 * Copyright (c) 2022-2024 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16export class AccessNSieve { 17 static readonly n1: int = 3; 18 static readonly n2: int = 10000; 19 static readonly expected: int = 14302; 20 static isPrime: boolean[] = []; 21 22 public setup(): void { 23 AccessNSieve.isPrime = new boolean[(1 << AccessNSieve.n1) * AccessNSieve.n2 + 1]; 24 } 25 26 private static nsieve(m: int): int { 27 let count: int = 0; 28 29 for (let i: int = 2; i <= m; i++) { 30 AccessNSieve.isPrime[i] = true; 31 } 32 33 for (let i: int = 2; i <= m; i++) { 34 if (AccessNSieve.isPrime[i]) { 35 for (let k: int = i + i; k <= m; k += i) { 36 AccessNSieve.isPrime[k] = false; 37 } 38 count++; 39 } 40 } 41 return count; 42 } 43 44 public static sieve(): int { 45 let sum: int = 0; 46 for (let i: int = 1; i <= AccessNSieve.n1; i++) { 47 let m: int = (1 << i) * AccessNSieve.n2; 48 sum += AccessNSieve.nsieve(m); 49 } 50 return sum; 51 } 52 53 public run(): void { 54 let ret: int = AccessNSieve.sieve(); 55 assert ret == AccessNSieve.expected: "Invalid result"; 56 } 57} 58 59function main(): void { 60 let a = new AccessNSieve; 61 a.setup(); 62 a.run(); 63} 64