正の整数をパラメータとして受け取り、それ以下のすべての素数の合計を表示する関数を作成します。
// Define a function named addPrimeSum that takes a single parameter 'number' function addPrimeSum(number) { // Initialize a variable 'result' to store the sum of prime numbers, starting from 0 let result = 0; // Define an inner function named isPrime that takes a single parameter 'num' function isPrime(num) { // If 'num' is less than 2, it is not prime, so return nothing (undefined) if (num 1) { // Check if 'number' is prime using the isPrime function if (isPrime(number)) { // If it is prime, add it to 'result' result = number; } // Decrement 'number' by 1 to check the next lower number number--; } // Return the total sum of all prime numbers found return result; } console.log(addPrimeSum(5)); console.log(addPrimeSum(21)); console.log(addPrimeSum(100)); console.log(addPrimeSum(239)); console.log(addPrimeSum(956));
> 10 > 77 > 1060 > 5589 > 70241
免責事項: 提供されるすべてのリソースの一部はインターネットからのものです。お客様の著作権またはその他の権利および利益の侵害がある場合は、詳細な理由を説明し、著作権または権利および利益の証拠を提出して、電子メール [email protected] に送信してください。 できるだけ早く対応させていただきます。
Copyright© 2022 湘ICP备2022001581号-3