1/* 2 * Copyright (c) 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 BitopsNSieveBits { 17 static primes(isPrime: int[], n1: int, n2: int): void { 18 let i: int; 19 let m: int = n2 << n1; 20 let size: int = m + 31 >> 5; 21 22 for (i = 0; i < size; i++) { 23 isPrime[i] = -1; 24 } 25 26 for (i = 2; i < m; i++) { 27 if ((isPrime[i >> 5] & 1 << (i & 31)) != 0) { 28 for (let j: int = i + i; j < m; j += i) { 29 isPrime[j >> 5] &= ~(1 << (j & 31)); 30 } 31 } 32 } 33 } 34 35 private static sieve(n1: int, n2: int): int[] { 36 // Not parsed new int[...] 37 let isPrime: int[] = new int[(n2 << n1) + 31 >> 5]; 38 BitopsNSieveBits.primes(isPrime, n1, n2); 39 return isPrime; 40 } 41 42 n1: int = 4; 43 n2: int = 10000; 44 static readonly expected: long = -1286749544853; 45 46 public run(): void { 47 let result: int[] = BitopsNSieveBits.sieve(this.n1, this.n2); 48 let sum: long = 0; 49 for (let i: int = 0; i < result.length; ++i) { 50 sum += result[i]; 51 } 52 53 assert sum == BitopsNSieveBits.expected: "Incorrect result"; 54 } 55} 56 57function main(): void { 58 let a = new BitopsNSieveBits; 59 a.run(); 60} 61