You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用JavaScript实现1到N范围内的Extra Perfect Number查找算法?

Extra Perfect Number: Exact Definition & JavaScript Solutions

Great question! Let’s break down the exact definition and build the algorithms you need, using the examples and rules you provided.

Exact Definition of Extra Perfect Number

Based on your examples (extraPerfect(3) returns [1,3], extraPerfect(7) returns [1,3,5,7]) and the binary check rule (first and last bits are the same), here’s the precise definition:

An Extra Perfect Number is a positive integer (within 1 to N) whose binary representation has both the most significant bit (first bit) and least significant bit (last bit) equal to 1.

A quick simplification: All positive integers have a leading 1 in their binary form (binary doesn’t start with 0), so this definition boils down to all odd numbers (since only odd numbers have a trailing 1 in binary). That’s exactly why your examples only include odd values!

JavaScript Algorithms

1. Function to Check if a Single Number is Extra Perfect

First, let’s build the validation function that follows the binary first/last bit rule:

function isExtraPerfect(num) {
  const binaryStr = num.toString(2); // Convert number to binary string (e.g., 3 → "11")
  // Check if first and last characters are both '1'
  return binaryStr[0] === '1' && binaryStr.at(-1) === '1';
}

Since we know positive integers always have a leading 1, we can simplify this to just check if the number is odd (more efficient):

// Simplified version (works for all positive integers)
function isExtraPerfect(num) {
  return num % 2 === 1;
}

2. Function to Find All Extra Perfect Numbers from 1 to N

We have two straightforward approaches here:

Approach 1: Generate Odd Numbers Directly (Most Efficient)

Since Extra Perfect Numbers are just odd numbers, we can skip checking entirely and generate them directly:

function extraPerfect(N) {
  const perfectNumbers = [];
  // Start at 1, increment by 2 to get all odds up to N
  for (let i = 1; i <= N; i += 2) {
    perfectNumbers.push(i);
  }
  return perfectNumbers;
}

Testing this:

  • extraPerfect(3) → [1, 3]
  • extraPerfect(7) → [1, 3, 5, 7] — matches your examples perfectly.

Approach 2: Filter Using the Check Function (More Explicit)

If you want to strictly follow the binary check logic (for clarity or if you need to adapt to edge cases later), use the validation function to filter numbers:

function extraPerfect(N) {
  const perfectNumbers = [];
  for (let i = 1; i <= N; i++) {
    if (isExtraPerfect(i)) {
      perfectNumbers.push(i);
    }
  }
  return perfectNumbers;
}

// Use the binary check function here (or the simplified odd check)
function isExtraPerfect(num) {
  const binaryStr = num.toString(2);
  return binaryStr[0] === '1' && binaryStr.at(-1) === '1';
}

Quick Verification

Both approaches will give you the same result. The direct odd generation method is faster for large N, while the filter method is more readable if you want to explicitly tie back to the binary rule.

内容的提问来源于stack exchange,提问作者Oluwaseyi Sarumi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 06:54:09