我正在编写一个代码来检查数字是否是alendindrome。但是当我检查输出2个数字时,它的到来不同

发布于 2025-02-13 17:53:36 字数 802 浏览 1 评论 0 原文

function allPalindromicPerms(num) {
  
  // find reverse of a number and then
  // comapre the reversed number with the main number
  
  var finalNum = 0;
  var originalNum = num;
  let remainder = 0;
  while (num) {
    remainder = num % 10;
    console.log('remainder' + remainder);
    num = num - remainder;
    num = num / 10;
    finalNum = finalNum * 10 + remainder;
    console.log(finalNum);
  }
  if (finalNum == originalNum) {
    console.log("is palindrome");
  } else {
    console.log("not a palindrome");
  }
}

allPalindromicPerms(23456788888765432) // with the the output is coming fine. 

allPalindromicPerms(234567888888765432) // but this is not giving me right output.

你能帮我什么错吗?

function allPalindromicPerms(num) {
  
  // find reverse of a number and then
  // comapre the reversed number with the main number
  
  var finalNum = 0;
  var originalNum = num;
  let remainder = 0;
  while (num) {
    remainder = num % 10;
    console.log('remainder' + remainder);
    num = num - remainder;
    num = num / 10;
    finalNum = finalNum * 10 + remainder;
    console.log(finalNum);
  }
  if (finalNum == originalNum) {
    console.log("is palindrome");
  } else {
    console.log("not a palindrome");
  }
}

allPalindromicPerms(23456788888765432) // with the the output is coming fine. 

allPalindromicPerms(234567888888765432) // but this is not giving me right output.

Could you help me what is wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

森末i 2025-02-20 17:53:36

这是因为这两个数字都大于 number.max_safe_integer

const a = 23456788888765432;
const b = 234567888888765432;

console.log('                 ↓                  ↓\n', a, b)
console.log(a > Number.MAX_SAFE_INTEGER, b > Number.MAX_SAFE_INTEGER)

要使用这个数字如此大的数字,最好坚持使用字符串或使用 bigint 。无论如何,成为回文是词汇属性。

请参阅以下示例解决方案:

const isPalindrome = n => `${n}`.split('').reverse().join('') === n + '';

console.log(
  isPalindrome(23456788888765432n),
  isPalindrome(234567888888765432n),
  isPalindrome('23456788888765432'),
  isPalindrome('234567888888765432')
);

This is because both those numbers are greater than the Number.MAX_SAFE_INTEGER.

const a = 23456788888765432;
const b = 234567888888765432;

console.log('                 ↓                  ↓\n', a, b)
console.log(a > Number.MAX_SAFE_INTEGER, b > Number.MAX_SAFE_INTEGER)

To work with numbers this big, it's better to stick to strings or use the BigInt. Being a palindrome is a lexical property anyway.

Please see the below example solution:

const isPalindrome = n => `${n}`.split('').reverse().join('') === n + '';

console.log(
  isPalindrome(23456788888765432n),
  isPalindrome(234567888888765432n),
  isPalindrome('23456788888765432'),
  isPalindrome('234567888888765432')
);

旧故 2025-02-20 17:53:36

那是因为此 2345678888888888765432 大于 number.max_safe_integer 它是 9007199254740991

> number.max_safe_integer

您可以使用 bigint

That's because this 234567888888765432 is bigger than Number.MAX_SAFE_INTEGER which is 9007199254740991

You can't expect any math operators to work with numbers bigger than Number.MAX_SAFE_INTEGER

You could use BigInt or just a string

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文