diff --git a/codes/javascript/chapter_computational_complexity/time_complexity.js b/codes/javascript/chapter_computational_complexity/time_complexity.js index 3d3ab4e19..bda0d0c7d 100644 --- a/codes/javascript/chapter_computational_complexity/time_complexity.js +++ b/codes/javascript/chapter_computational_complexity/time_complexity.js @@ -100,7 +100,8 @@ function logRecur(n) { /* 线性对数阶 */ function linearLogRecur(n) { if (n <= 1) return 1; - let count = linearLogRecur(n / 2) + linearLogRecur(n / 2); + const half = Math.floor(n / 2); + let count = linearLogRecur(half) + linearLogRecur(half); for (let i = 0; i < n; i++) { count++; } diff --git a/codes/javascript/chapter_searching/binary_search_insertion.js b/codes/javascript/chapter_searching/binary_search_insertion.js index bc84d0e29..77a4d7601 100644 --- a/codes/javascript/chapter_searching/binary_search_insertion.js +++ b/codes/javascript/chapter_searching/binary_search_insertion.js @@ -41,22 +41,24 @@ function binarySearchInsertion(nums, target) { } /* Driver Code */ -// 无重复元素的数组 -let nums = [1, 3, 6, 8, 12, 15, 23, 26, 31, 35]; -console.log('\n数组 nums = ' + nums); -// 二分查找插入点 -for (const target of [6, 9]) { - const index = binarySearchInsertionSimple(nums, target); - console.log('元素 ' + target + ' 的插入点的索引为 ' + index); -} +if (require.main === module) { + // 无重复元素的数组 + let nums = [1, 3, 6, 8, 12, 15, 23, 26, 31, 35]; + console.log('\n数组 nums = ' + nums); + // 二分查找插入点 + for (const target of [6, 9]) { + const index = binarySearchInsertionSimple(nums, target); + console.log('元素 ' + target + ' 的插入点的索引为 ' + index); + } -// 包含重复元素的数组 -nums = [1, 3, 6, 6, 6, 6, 6, 10, 12, 15]; -console.log('\n数组 nums = ' + nums); -// 二分查找插入点 -for (const target of [2, 6, 20]) { - const index = binarySearchInsertion(nums, target); - console.log('元素 ' + target + ' 的插入点的索引为 ' + index); + // 包含重复元素的数组 + nums = [1, 3, 6, 6, 6, 6, 6, 10, 12, 15]; + console.log('\n数组 nums = ' + nums); + // 二分查找插入点 + for (const target of [2, 6, 20]) { + const index = binarySearchInsertion(nums, target); + console.log('元素 ' + target + ' 的插入点的索引为 ' + index); + } } module.exports = { diff --git a/codes/typescript/chapter_computational_complexity/time_complexity.ts b/codes/typescript/chapter_computational_complexity/time_complexity.ts index 9b7bc4481..d3553e146 100644 --- a/codes/typescript/chapter_computational_complexity/time_complexity.ts +++ b/codes/typescript/chapter_computational_complexity/time_complexity.ts @@ -100,7 +100,8 @@ function logRecur(n: number): number { /* 线性对数阶 */ function linearLogRecur(n: number): number { if (n <= 1) return 1; - let count = linearLogRecur(n / 2) + linearLogRecur(n / 2); + const half = Math.floor(n / 2); + let count = linearLogRecur(half) + linearLogRecur(half); for (let i = 0; i < n; i++) { count++; } diff --git a/codes/typescript/chapter_searching/binary_search_insertion.ts b/codes/typescript/chapter_searching/binary_search_insertion.ts index 05b3a632c..d3e3d7983 100644 --- a/codes/typescript/chapter_searching/binary_search_insertion.ts +++ b/codes/typescript/chapter_searching/binary_search_insertion.ts @@ -4,6 +4,8 @@ * Author: Gaofer Chou (gaofer-chou@qq.com) */ +import { pathToFileURL } from 'node:url'; + /* 二分查找插入点(无重复元素) */ function binarySearchInsertionSimple( nums: Array, @@ -44,22 +46,24 @@ function binarySearchInsertion(nums: Array, target: number): number { } /* Driver Code */ -// 无重复元素的数组 -let nums = [1, 3, 6, 8, 12, 15, 23, 26, 31, 35]; -console.log('\n数组 nums = ' + nums); -// 二分查找插入点 -for (const target of [6, 9]) { - const index = binarySearchInsertionSimple(nums, target); - console.log('元素 ' + target + ' 的插入点的索引为 ' + index); -} +if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) { + // 无重复元素的数组 + let nums = [1, 3, 6, 8, 12, 15, 23, 26, 31, 35]; + console.log('\n数组 nums = ' + nums); + // 二分查找插入点 + for (const target of [6, 9]) { + const index = binarySearchInsertionSimple(nums, target); + console.log('元素 ' + target + ' 的插入点的索引为 ' + index); + } -// 包含重复元素的数组 -nums = [1, 3, 6, 6, 6, 6, 6, 10, 12, 15]; -console.log('\n数组 nums = ' + nums); -// 二分查找插入点 -for (const target of [2, 6, 20]) { - const index = binarySearchInsertion(nums, target); - console.log('元素 ' + target + ' 的插入点的索引为 ' + index); + // 包含重复元素的数组 + nums = [1, 3, 6, 6, 6, 6, 6, 10, 12, 15]; + console.log('\n数组 nums = ' + nums); + // 二分查找插入点 + for (const target of [2, 6, 20]) { + const index = binarySearchInsertion(nums, target); + console.log('元素 ' + target + ' 的插入点的索引为 ' + index); + } } export { binarySearchInsertion }; diff --git a/en/codes/javascript/chapter_computational_complexity/time_complexity.js b/en/codes/javascript/chapter_computational_complexity/time_complexity.js index 35e72576f..d80698ad1 100644 --- a/en/codes/javascript/chapter_computational_complexity/time_complexity.js +++ b/en/codes/javascript/chapter_computational_complexity/time_complexity.js @@ -100,7 +100,8 @@ function logRecur(n) { /* Linearithmic complexity */ function linearLogRecur(n) { if (n <= 1) return 1; - let count = linearLogRecur(n / 2) + linearLogRecur(n / 2); + const half = Math.floor(n / 2); + let count = linearLogRecur(half) + linearLogRecur(half); for (let i = 0; i < n; i++) { count++; } diff --git a/en/codes/javascript/chapter_searching/binary_search_insertion.js b/en/codes/javascript/chapter_searching/binary_search_insertion.js index c4be595f3..069eec272 100644 --- a/en/codes/javascript/chapter_searching/binary_search_insertion.js +++ b/en/codes/javascript/chapter_searching/binary_search_insertion.js @@ -41,22 +41,24 @@ function binarySearchInsertion(nums, target) { } /* Driver code */ -// Array with no duplicate elements -let nums = [1, 3, 6, 8, 12, 15, 23, 26, 31, 35]; -console.log('\nArray nums = ' + nums); -// Find an insertion point using binary search -for (const target of [6, 9]) { - const index = binarySearchInsertionSimple(nums, target); - console.log('Element ' + target + ' has insertion index ' + index); -} +if (require.main === module) { + // Array with no duplicate elements + let nums = [1, 3, 6, 8, 12, 15, 23, 26, 31, 35]; + console.log('\nArray nums = ' + nums); + // Find an insertion point using binary search + for (const target of [6, 9]) { + const index = binarySearchInsertionSimple(nums, target); + console.log('Element ' + target + ' has insertion index ' + index); + } -// Array containing duplicate elements -nums = [1, 3, 6, 6, 6, 6, 6, 10, 12, 15]; -console.log('\nArray nums = ' + nums); -// Find an insertion point using binary search -for (const target of [2, 6, 20]) { - const index = binarySearchInsertion(nums, target); - console.log('Element ' + target + ' has insertion index ' + index); + // Array containing duplicate elements + nums = [1, 3, 6, 6, 6, 6, 6, 10, 12, 15]; + console.log('\nArray nums = ' + nums); + // Find an insertion point using binary search + for (const target of [2, 6, 20]) { + const index = binarySearchInsertion(nums, target); + console.log('Element ' + target + ' has insertion index ' + index); + } } module.exports = { diff --git a/en/codes/typescript/chapter_computational_complexity/time_complexity.ts b/en/codes/typescript/chapter_computational_complexity/time_complexity.ts index 3ba646bb1..41d03af09 100644 --- a/en/codes/typescript/chapter_computational_complexity/time_complexity.ts +++ b/en/codes/typescript/chapter_computational_complexity/time_complexity.ts @@ -100,7 +100,8 @@ function logRecur(n: number): number { /* Linearithmic complexity */ function linearLogRecur(n: number): number { if (n <= 1) return 1; - let count = linearLogRecur(n / 2) + linearLogRecur(n / 2); + const half = Math.floor(n / 2); + let count = linearLogRecur(half) + linearLogRecur(half); for (let i = 0; i < n; i++) { count++; } diff --git a/en/codes/typescript/chapter_searching/binary_search_insertion.ts b/en/codes/typescript/chapter_searching/binary_search_insertion.ts index 54ce12f9d..2cf962d94 100644 --- a/en/codes/typescript/chapter_searching/binary_search_insertion.ts +++ b/en/codes/typescript/chapter_searching/binary_search_insertion.ts @@ -4,6 +4,8 @@ * Author: Gaofer Chou (gaofer-chou@qq.com) */ +import { pathToFileURL } from 'node:url'; + /* Find an insertion point using binary search (no duplicates) */ function binarySearchInsertionSimple( nums: Array, @@ -44,22 +46,24 @@ function binarySearchInsertion(nums: Array, target: number): number { } /* Driver code */ -// Array with no duplicate elements -let nums = [1, 3, 6, 8, 12, 15, 23, 26, 31, 35]; -console.log('\nArray nums = ' + nums); -// Find an insertion point using binary search -for (const target of [6, 9]) { - const index = binarySearchInsertionSimple(nums, target); - console.log('Element ' + target + ' has insertion index ' + index); -} +if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) { + // Array with no duplicate elements + let nums = [1, 3, 6, 8, 12, 15, 23, 26, 31, 35]; + console.log('\nArray nums = ' + nums); + // Find an insertion point using binary search + for (const target of [6, 9]) { + const index = binarySearchInsertionSimple(nums, target); + console.log('Element ' + target + ' has insertion index ' + index); + } -// Array containing duplicate elements -nums = [1, 3, 6, 6, 6, 6, 6, 10, 12, 15]; -console.log('\nArray nums = ' + nums); -// Find an insertion point using binary search -for (const target of [2, 6, 20]) { - const index = binarySearchInsertion(nums, target); - console.log('Element ' + target + ' has insertion index ' + index); + // Array containing duplicate elements + nums = [1, 3, 6, 6, 6, 6, 6, 10, 12, 15]; + console.log('\nArray nums = ' + nums); + // Find an insertion point using binary search + for (const target of [2, 6, 20]) { + const index = binarySearchInsertion(nums, target); + console.log('Element ' + target + ' has insertion index ' + index); + } } export { binarySearchInsertion }; diff --git a/ja/codes/javascript/chapter_computational_complexity/time_complexity.js b/ja/codes/javascript/chapter_computational_complexity/time_complexity.js index 0b08a5cc9..d9ee1f1b5 100644 --- a/ja/codes/javascript/chapter_computational_complexity/time_complexity.js +++ b/ja/codes/javascript/chapter_computational_complexity/time_complexity.js @@ -100,7 +100,8 @@ function logRecur(n) { /* 線形対数オーダー */ function linearLogRecur(n) { if (n <= 1) return 1; - let count = linearLogRecur(n / 2) + linearLogRecur(n / 2); + const half = Math.floor(n / 2); + let count = linearLogRecur(half) + linearLogRecur(half); for (let i = 0; i < n; i++) { count++; } diff --git a/ja/codes/javascript/chapter_searching/binary_search_insertion.js b/ja/codes/javascript/chapter_searching/binary_search_insertion.js index d051112ac..6b7cc6aa4 100644 --- a/ja/codes/javascript/chapter_searching/binary_search_insertion.js +++ b/ja/codes/javascript/chapter_searching/binary_search_insertion.js @@ -41,22 +41,24 @@ function binarySearchInsertion(nums, target) { } /* 実行用コード */ -// 重複要素のない配列 -let nums = [1, 3, 6, 8, 12, 15, 23, 26, 31, 35]; -console.log('\n配列 nums = ' + nums); -// 二分探索による挿入位置の探索 -for (const target of [6, 9]) { - const index = binarySearchInsertionSimple(nums, target); - console.log('要素 ' + target + ' の挿入位置のインデックスは ' + index); -} +if (require.main === module) { + // 重複要素のない配列 + let nums = [1, 3, 6, 8, 12, 15, 23, 26, 31, 35]; + console.log('\n配列 nums = ' + nums); + // 二分探索による挿入位置の探索 + for (const target of [6, 9]) { + const index = binarySearchInsertionSimple(nums, target); + console.log('要素 ' + target + ' の挿入位置のインデックスは ' + index); + } -// 重複要素を含む配列 -nums = [1, 3, 6, 6, 6, 6, 6, 10, 12, 15]; -console.log('\n配列 nums = ' + nums); -// 二分探索による挿入位置の探索 -for (const target of [2, 6, 20]) { - const index = binarySearchInsertion(nums, target); - console.log('要素 ' + target + ' の挿入位置のインデックスは ' + index); + // 重複要素を含む配列 + nums = [1, 3, 6, 6, 6, 6, 6, 10, 12, 15]; + console.log('\n配列 nums = ' + nums); + // 二分探索による挿入位置の探索 + for (const target of [2, 6, 20]) { + const index = binarySearchInsertion(nums, target); + console.log('要素 ' + target + ' の挿入位置のインデックスは ' + index); + } } module.exports = { diff --git a/ja/codes/typescript/chapter_computational_complexity/time_complexity.ts b/ja/codes/typescript/chapter_computational_complexity/time_complexity.ts index ad5335a5e..3a0369601 100644 --- a/ja/codes/typescript/chapter_computational_complexity/time_complexity.ts +++ b/ja/codes/typescript/chapter_computational_complexity/time_complexity.ts @@ -100,7 +100,8 @@ function logRecur(n: number): number { /* 線形対数オーダー */ function linearLogRecur(n: number): number { if (n <= 1) return 1; - let count = linearLogRecur(n / 2) + linearLogRecur(n / 2); + const half = Math.floor(n / 2); + let count = linearLogRecur(half) + linearLogRecur(half); for (let i = 0; i < n; i++) { count++; } diff --git a/ja/codes/typescript/chapter_searching/binary_search_insertion.ts b/ja/codes/typescript/chapter_searching/binary_search_insertion.ts index daf8d56b7..cd32214e3 100644 --- a/ja/codes/typescript/chapter_searching/binary_search_insertion.ts +++ b/ja/codes/typescript/chapter_searching/binary_search_insertion.ts @@ -4,6 +4,8 @@ * Author: Gaofer Chou (gaofer-chou@qq.com) */ +import { pathToFileURL } from 'node:url'; + /* 二分探索による挿入位置の探索(重複要素がない場合) */ function binarySearchInsertionSimple( nums: Array, @@ -44,22 +46,24 @@ function binarySearchInsertion(nums: Array, target: number): number { } /* 実行用コード */ -// 重複要素のない配列 -let nums = [1, 3, 6, 8, 12, 15, 23, 26, 31, 35]; -console.log('\n配列 nums = ' + nums); -// 二分探索による挿入位置の探索 -for (const target of [6, 9]) { - const index = binarySearchInsertionSimple(nums, target); - console.log('要素 ' + target + ' の挿入位置のインデックスは ' + index); -} +if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) { + // 重複要素のない配列 + let nums = [1, 3, 6, 8, 12, 15, 23, 26, 31, 35]; + console.log('\n配列 nums = ' + nums); + // 二分探索による挿入位置の探索 + for (const target of [6, 9]) { + const index = binarySearchInsertionSimple(nums, target); + console.log('要素 ' + target + ' の挿入位置のインデックスは ' + index); + } -// 重複要素を含む配列 -nums = [1, 3, 6, 6, 6, 6, 6, 10, 12, 15]; -console.log('\n配列 nums = ' + nums); -// 二分探索による挿入位置の探索 -for (const target of [2, 6, 20]) { - const index = binarySearchInsertion(nums, target); - console.log('要素 ' + target + ' の挿入位置のインデックスは ' + index); + // 重複要素を含む配列 + nums = [1, 3, 6, 6, 6, 6, 6, 10, 12, 15]; + console.log('\n配列 nums = ' + nums); + // 二分探索による挿入位置の探索 + for (const target of [2, 6, 20]) { + const index = binarySearchInsertion(nums, target); + console.log('要素 ' + target + ' の挿入位置のインデックスは ' + index); + } } export { binarySearchInsertion }; diff --git a/ko/codes/javascript/chapter_computational_complexity/time_complexity.js b/ko/codes/javascript/chapter_computational_complexity/time_complexity.js index 2d86b5e41..d01f00307 100644 --- a/ko/codes/javascript/chapter_computational_complexity/time_complexity.js +++ b/ko/codes/javascript/chapter_computational_complexity/time_complexity.js @@ -100,7 +100,8 @@ function logRecur(n) { /* 선형 로그 차수 */ function linearLogRecur(n) { if (n <= 1) return 1; - let count = linearLogRecur(n / 2) + linearLogRecur(n / 2); + const half = Math.floor(n / 2); + let count = linearLogRecur(half) + linearLogRecur(half); for (let i = 0; i < n; i++) { count++; } diff --git a/ko/codes/javascript/chapter_searching/binary_search_insertion.js b/ko/codes/javascript/chapter_searching/binary_search_insertion.js index d1a989983..177c9b273 100644 --- a/ko/codes/javascript/chapter_searching/binary_search_insertion.js +++ b/ko/codes/javascript/chapter_searching/binary_search_insertion.js @@ -41,22 +41,24 @@ function binarySearchInsertion(nums, target) { } /* 실행 코드 */ -// 중복 원소가 없는 배열 -let nums = [1, 3, 6, 8, 12, 15, 23, 26, 31, 35]; -console.log('\n배열 nums = ' + nums); -// 삽입 위치의 이진 탐색 -for (const target of [6, 9]) { - const index = binarySearchInsertionSimple(nums, target); - console.log('원소 ' + target + ' ; 삽입 위치의 인덱스: ' + index); -} +if (require.main === module) { + // 중복 원소가 없는 배열 + let nums = [1, 3, 6, 8, 12, 15, 23, 26, 31, 35]; + console.log('\n배열 nums = ' + nums); + // 삽입 위치의 이진 탐색 + for (const target of [6, 9]) { + const index = binarySearchInsertionSimple(nums, target); + console.log('원소 ' + target + ' ; 삽입 위치의 인덱스: ' + index); + } -// 중복 원소를 포함하는 배열 -nums = [1, 3, 6, 6, 6, 6, 6, 10, 12, 15]; -console.log('\n배열 nums = ' + nums); -// 삽입 위치의 이진 탐색 -for (const target of [2, 6, 20]) { - const index = binarySearchInsertion(nums, target); - console.log('원소 ' + target + ' ; 삽입 위치의 인덱스: ' + index); + // 중복 원소를 포함하는 배열 + nums = [1, 3, 6, 6, 6, 6, 6, 10, 12, 15]; + console.log('\n배열 nums = ' + nums); + // 삽입 위치의 이진 탐색 + for (const target of [2, 6, 20]) { + const index = binarySearchInsertion(nums, target); + console.log('원소 ' + target + ' ; 삽입 위치의 인덱스: ' + index); + } } module.exports = { diff --git a/ko/codes/typescript/chapter_computational_complexity/time_complexity.ts b/ko/codes/typescript/chapter_computational_complexity/time_complexity.ts index 09c154d8c..a1cc49eb8 100644 --- a/ko/codes/typescript/chapter_computational_complexity/time_complexity.ts +++ b/ko/codes/typescript/chapter_computational_complexity/time_complexity.ts @@ -100,7 +100,8 @@ function logRecur(n: number): number { /* 선형 로그 차수 */ function linearLogRecur(n: number): number { if (n <= 1) return 1; - let count = linearLogRecur(n / 2) + linearLogRecur(n / 2); + const half = Math.floor(n / 2); + let count = linearLogRecur(half) + linearLogRecur(half); for (let i = 0; i < n; i++) { count++; } diff --git a/ko/codes/typescript/chapter_searching/binary_search_insertion.ts b/ko/codes/typescript/chapter_searching/binary_search_insertion.ts index a25e0addd..393fb018c 100644 --- a/ko/codes/typescript/chapter_searching/binary_search_insertion.ts +++ b/ko/codes/typescript/chapter_searching/binary_search_insertion.ts @@ -4,6 +4,8 @@ * Author: Gaofer Chou (gaofer-chou@qq.com) */ +import { pathToFileURL } from 'node:url'; + /* 삽입 위치의 이진 탐색(중복 원소 없음) */ function binarySearchInsertionSimple( nums: Array, @@ -44,22 +46,24 @@ function binarySearchInsertion(nums: Array, target: number): number { } /* 실행 코드 */ -// 중복 원소가 없는 배열 -let nums = [1, 3, 6, 8, 12, 15, 23, 26, 31, 35]; -console.log('\n배열 nums = ' + nums); -// 삽입 위치의 이진 탐색 -for (const target of [6, 9]) { - const index = binarySearchInsertionSimple(nums, target); - console.log('원소 ' + target + ' ; 삽입 위치의 인덱스: ' + index); -} +if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) { + // 중복 원소가 없는 배열 + let nums = [1, 3, 6, 8, 12, 15, 23, 26, 31, 35]; + console.log('\n배열 nums = ' + nums); + // 삽입 위치의 이진 탐색 + for (const target of [6, 9]) { + const index = binarySearchInsertionSimple(nums, target); + console.log('원소 ' + target + ' ; 삽입 위치의 인덱스: ' + index); + } -// 중복 원소를 포함하는 배열 -nums = [1, 3, 6, 6, 6, 6, 6, 10, 12, 15]; -console.log('\n배열 nums = ' + nums); -// 삽입 위치의 이진 탐색 -for (const target of [2, 6, 20]) { - const index = binarySearchInsertion(nums, target); - console.log('원소 ' + target + ' ; 삽입 위치의 인덱스: ' + index); + // 중복 원소를 포함하는 배열 + nums = [1, 3, 6, 6, 6, 6, 6, 10, 12, 15]; + console.log('\n배열 nums = ' + nums); + // 삽입 위치의 이진 탐색 + for (const target of [2, 6, 20]) { + const index = binarySearchInsertion(nums, target); + console.log('원소 ' + target + ' ; 삽입 위치의 인덱스: ' + index); + } } export { binarySearchInsertion }; diff --git a/ru/codes/javascript/chapter_computational_complexity/time_complexity.js b/ru/codes/javascript/chapter_computational_complexity/time_complexity.js index e2c6ef6ba..ed0368fae 100644 --- a/ru/codes/javascript/chapter_computational_complexity/time_complexity.js +++ b/ru/codes/javascript/chapter_computational_complexity/time_complexity.js @@ -100,7 +100,8 @@ function logRecur(n) { /* Линейно-логарифмическая сложность */ function linearLogRecur(n) { if (n <= 1) return 1; - let count = linearLogRecur(n / 2) + linearLogRecur(n / 2); + const half = Math.floor(n / 2); + let count = linearLogRecur(half) + linearLogRecur(half); for (let i = 0; i < n; i++) { count++; } diff --git a/ru/codes/javascript/chapter_searching/binary_search_insertion.js b/ru/codes/javascript/chapter_searching/binary_search_insertion.js index 2f122ff31..bc2ce4d0b 100644 --- a/ru/codes/javascript/chapter_searching/binary_search_insertion.js +++ b/ru/codes/javascript/chapter_searching/binary_search_insertion.js @@ -41,22 +41,24 @@ function binarySearchInsertion(nums, target) { } /* Driver Code */ -// Массив без повторяющихся элементов -let nums = [1, 3, 6, 8, 12, 15, 23, 26, 31, 35]; -console.log('\nМассив nums = ' + nums); -// Двоичный поиск точки вставки -for (const target of [6, 9]) { - const index = binarySearchInsertionSimple(nums, target); - console.log('Индекс позиции вставки элемента ' + target + ' равен ' + index); -} +if (require.main === module) { + // Массив без повторяющихся элементов + let nums = [1, 3, 6, 8, 12, 15, 23, 26, 31, 35]; + console.log('\nМассив nums = ' + nums); + // Двоичный поиск точки вставки + for (const target of [6, 9]) { + const index = binarySearchInsertionSimple(nums, target); + console.log('Индекс позиции вставки элемента ' + target + ' равен ' + index); + } -// Массив с повторяющимися элементами -nums = [1, 3, 6, 6, 6, 6, 6, 10, 12, 15]; -console.log('\nМассив nums = ' + nums); -// Двоичный поиск точки вставки -for (const target of [2, 6, 20]) { - const index = binarySearchInsertion(nums, target); - console.log('Индекс позиции вставки элемента ' + target + ' равен ' + index); + // Массив с повторяющимися элементами + nums = [1, 3, 6, 6, 6, 6, 6, 10, 12, 15]; + console.log('\nМассив nums = ' + nums); + // Двоичный поиск точки вставки + for (const target of [2, 6, 20]) { + const index = binarySearchInsertion(nums, target); + console.log('Индекс позиции вставки элемента ' + target + ' равен ' + index); + } } module.exports = { diff --git a/ru/codes/typescript/chapter_computational_complexity/time_complexity.ts b/ru/codes/typescript/chapter_computational_complexity/time_complexity.ts index 54b8501c2..0a255e5f0 100644 --- a/ru/codes/typescript/chapter_computational_complexity/time_complexity.ts +++ b/ru/codes/typescript/chapter_computational_complexity/time_complexity.ts @@ -100,7 +100,8 @@ function logRecur(n: number): number { /* Линейно-логарифмическая сложность */ function linearLogRecur(n: number): number { if (n <= 1) return 1; - let count = linearLogRecur(n / 2) + linearLogRecur(n / 2); + const half = Math.floor(n / 2); + let count = linearLogRecur(half) + linearLogRecur(half); for (let i = 0; i < n; i++) { count++; } diff --git a/ru/codes/typescript/chapter_searching/binary_search_insertion.ts b/ru/codes/typescript/chapter_searching/binary_search_insertion.ts index 914a84741..7db3bc70a 100644 --- a/ru/codes/typescript/chapter_searching/binary_search_insertion.ts +++ b/ru/codes/typescript/chapter_searching/binary_search_insertion.ts @@ -4,6 +4,8 @@ * Author: Gaofer Chou (gaofer-chou@qq.com) */ +import { pathToFileURL } from 'node:url'; + /* Двоичный поиск точки вставки (без повторяющихся элементов) */ function binarySearchInsertionSimple( nums: Array, @@ -44,22 +46,24 @@ function binarySearchInsertion(nums: Array, target: number): number { } /* Driver Code */ -// Массив без повторяющихся элементов -let nums = [1, 3, 6, 8, 12, 15, 23, 26, 31, 35]; -console.log('\nМассив nums = ' + nums); -// Двоичный поиск точки вставки -for (const target of [6, 9]) { - const index = binarySearchInsertionSimple(nums, target); - console.log('Индекс точки вставки элемента ' + target + ' = ' + index); -} +if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) { + // Массив без повторяющихся элементов + let nums = [1, 3, 6, 8, 12, 15, 23, 26, 31, 35]; + console.log('\nМассив nums = ' + nums); + // Двоичный поиск точки вставки + for (const target of [6, 9]) { + const index = binarySearchInsertionSimple(nums, target); + console.log('Индекс точки вставки элемента ' + target + ' = ' + index); + } -// Массив с повторяющимися элементами -nums = [1, 3, 6, 6, 6, 6, 6, 10, 12, 15]; -console.log('\nМассив nums = ' + nums); -// Двоичный поиск точки вставки -for (const target of [2, 6, 20]) { - const index = binarySearchInsertion(nums, target); - console.log('Индекс точки вставки элемента ' + target + ' = ' + index); + // Массив с повторяющимися элементами + nums = [1, 3, 6, 6, 6, 6, 6, 10, 12, 15]; + console.log('\nМассив nums = ' + nums); + // Двоичный поиск точки вставки + for (const target of [2, 6, 20]) { + const index = binarySearchInsertion(nums, target); + console.log('Индекс точки вставки элемента ' + target + ' = ' + index); + } } export { binarySearchInsertion }; diff --git a/zh-hant/codes/javascript/chapter_computational_complexity/time_complexity.js b/zh-hant/codes/javascript/chapter_computational_complexity/time_complexity.js index fc55ec7c1..e1ee0e74a 100644 --- a/zh-hant/codes/javascript/chapter_computational_complexity/time_complexity.js +++ b/zh-hant/codes/javascript/chapter_computational_complexity/time_complexity.js @@ -100,7 +100,8 @@ function logRecur(n) { /* 線性對數階 */ function linearLogRecur(n) { if (n <= 1) return 1; - let count = linearLogRecur(n / 2) + linearLogRecur(n / 2); + const half = Math.floor(n / 2); + let count = linearLogRecur(half) + linearLogRecur(half); for (let i = 0; i < n; i++) { count++; } diff --git a/zh-hant/codes/javascript/chapter_searching/binary_search_insertion.js b/zh-hant/codes/javascript/chapter_searching/binary_search_insertion.js index 6667f44ca..fd6fed8ff 100644 --- a/zh-hant/codes/javascript/chapter_searching/binary_search_insertion.js +++ b/zh-hant/codes/javascript/chapter_searching/binary_search_insertion.js @@ -41,22 +41,24 @@ function binarySearchInsertion(nums, target) { } /* Driver Code */ -// 無重複元素的陣列 -let nums = [1, 3, 6, 8, 12, 15, 23, 26, 31, 35]; -console.log('\n陣列 nums = ' + nums); -// 二分搜尋插入點 -for (const target of [6, 9]) { - const index = binarySearchInsertionSimple(nums, target); - console.log('元素 ' + target + ' 的插入點的索引為 ' + index); -} +if (require.main === module) { + // 無重複元素的陣列 + let nums = [1, 3, 6, 8, 12, 15, 23, 26, 31, 35]; + console.log('\n陣列 nums = ' + nums); + // 二分搜尋插入點 + for (const target of [6, 9]) { + const index = binarySearchInsertionSimple(nums, target); + console.log('元素 ' + target + ' 的插入點的索引為 ' + index); + } -// 包含重複元素的陣列 -nums = [1, 3, 6, 6, 6, 6, 6, 10, 12, 15]; -console.log('\n陣列 nums = ' + nums); -// 二分搜尋插入點 -for (const target of [2, 6, 20]) { - const index = binarySearchInsertion(nums, target); - console.log('元素 ' + target + ' 的插入點的索引為 ' + index); + // 包含重複元素的陣列 + nums = [1, 3, 6, 6, 6, 6, 6, 10, 12, 15]; + console.log('\n陣列 nums = ' + nums); + // 二分搜尋插入點 + for (const target of [2, 6, 20]) { + const index = binarySearchInsertion(nums, target); + console.log('元素 ' + target + ' 的插入點的索引為 ' + index); + } } module.exports = { diff --git a/zh-hant/codes/typescript/chapter_computational_complexity/time_complexity.ts b/zh-hant/codes/typescript/chapter_computational_complexity/time_complexity.ts index d10d25e8d..f3923c00e 100644 --- a/zh-hant/codes/typescript/chapter_computational_complexity/time_complexity.ts +++ b/zh-hant/codes/typescript/chapter_computational_complexity/time_complexity.ts @@ -100,7 +100,8 @@ function logRecur(n: number): number { /* 線性對數階 */ function linearLogRecur(n: number): number { if (n <= 1) return 1; - let count = linearLogRecur(n / 2) + linearLogRecur(n / 2); + const half = Math.floor(n / 2); + let count = linearLogRecur(half) + linearLogRecur(half); for (let i = 0; i < n; i++) { count++; } diff --git a/zh-hant/codes/typescript/chapter_searching/binary_search_insertion.ts b/zh-hant/codes/typescript/chapter_searching/binary_search_insertion.ts index a051979d2..ef310335a 100644 --- a/zh-hant/codes/typescript/chapter_searching/binary_search_insertion.ts +++ b/zh-hant/codes/typescript/chapter_searching/binary_search_insertion.ts @@ -4,6 +4,8 @@ * Author: Gaofer Chou (gaofer-chou@qq.com) */ +import { pathToFileURL } from 'node:url'; + /* 二分搜尋插入點(無重複元素) */ function binarySearchInsertionSimple( nums: Array, @@ -44,22 +46,24 @@ function binarySearchInsertion(nums: Array, target: number): number { } /* Driver Code */ -// 無重複元素的陣列 -let nums = [1, 3, 6, 8, 12, 15, 23, 26, 31, 35]; -console.log('\n陣列 nums = ' + nums); -// 二分搜尋插入點 -for (const target of [6, 9]) { - const index = binarySearchInsertionSimple(nums, target); - console.log('元素 ' + target + ' 的插入點的索引為 ' + index); -} +if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) { + // 無重複元素的陣列 + let nums = [1, 3, 6, 8, 12, 15, 23, 26, 31, 35]; + console.log('\n陣列 nums = ' + nums); + // 二分搜尋插入點 + for (const target of [6, 9]) { + const index = binarySearchInsertionSimple(nums, target); + console.log('元素 ' + target + ' 的插入點的索引為 ' + index); + } -// 包含重複元素的陣列 -nums = [1, 3, 6, 6, 6, 6, 6, 10, 12, 15]; -console.log('\n陣列 nums = ' + nums); -// 二分搜尋插入點 -for (const target of [2, 6, 20]) { - const index = binarySearchInsertion(nums, target); - console.log('元素 ' + target + ' 的插入點的索引為 ' + index); + // 包含重複元素的陣列 + nums = [1, 3, 6, 6, 6, 6, 6, 10, 12, 15]; + console.log('\n陣列 nums = ' + nums); + // 二分搜尋插入點 + for (const target of [2, 6, 20]) { + const index = binarySearchInsertion(nums, target); + console.log('元素 ' + target + ' 的插入點的索引為 ' + index); + } } export { binarySearchInsertion };