num=n
lastDigit=0;
rev=0;
while (num>rev) {
lastDigit=num%10;
rev=rev*10+lastDigit;
num /=2;
}
if (num==rev) print PALINDROME; exit(0);
num=num*10+lastDigit; // This line is required as a number with odd number of bits will necessary end up being smaller even if it is a palindrome
if (num==rev) print PALINDROME
let reverseNumber n =
let rec loop acc = function
|0 -> acc
|x -> loop (acc * 10 + x % 10) (x/10)
loop 0 n
let isPalindrome = function
| x when x = reverseNumber x -> true
| _ -> false
reverse = 0;
remainder = 0;
count = 0;
while (number > reverse)
{
remainder = number % 10;
reverse = reverse * 10 + remainder;
number = number / 10;
count++;
}
Console.WriteLine(count);
if (reverse == number)
{
Console.WriteLine("Your number is a palindrome");
}
else
{
number = number * 10 + remainder;
if (reverse == number)
Console.WriteLine("your number is a palindrome");
else
Console.WriteLine("your number is not a palindrome");
}
Console.ReadLine();
}
}
public class Numbers
{
public static void main(int givenNum)
{
int n= givenNum
int rev=0;
while(n>0)
{
//To extract the last digit
int digit=n%10;
//To store it in reverse
rev=(rev*10)+digit;
//To throw the last digit
n=n/10;
}
//To check if a number is palindrome or not
if(rev==givenNum)
{
System.out.println(givenNum+"is a palindrome ");
}
else
{
System.out.pritnln(givenNum+"is not a palindrome");
}
}
}
boolean isPalindrome(int x) {
if (x < 0)
return false;
int div = 1;
while (x / div >= 10) {
div *= 10;
}
while (x != 0) {
int l = x / div;
int r = x % 10;
if (l != r)
return false;
x = (x % div) / 10;
div /= 100;
}
return true;
}
int max =(int)(Math.random()*100001);
int i;
int num = max; //a var used in the tests
int size; //the number of digits in the original number
int opos = 0; // the oposite number
int nsize = 1;
System.out.println(max);
for(i = 1; num>10; i++)
{
num = num/10;
}
System.out.println("this number has "+i+" digits");
size = i; //setting the digit number to a var for later use
num = max;
for(i=1;i<size;i++)
{
nsize *=10;
}
while(num>1)
{
opos += (num%10)*nsize;
num/=10;
nsize/=10;
}
System.out.println("and the number backwards is "+opos);
if (opos == max )
{
System.out.println("palindrome!!");
}
else
{
System.out.println("aint no palindrome!");
}
print('!* To Find Palindrome Number')
def Palindrome_Number():
n = input('Enter Number to check for palindromee')
m=n
a = 0
while(m!=0):
a = m % 10 + a * 10
m = m / 10
if( n == a):
print('%d is a palindrome number' %n)
else:
print('%d is not a palindrome number' %n)
def isPalindrome(num):
if num < 0:
return False
if num == 0:
return True
from math import log10
length = int(log10(num))
while length > 0:
right = num % 10
left = num / 10**length
if right != left:
return False
num %= 10**length
num /= 10
length -= 2
return True
int isIntPalindrome(int x)
{
if (x < 0)
return 0;
int div = 1;
while (x / div >= 10)
{
div *= 10;
}
while (x != 0)
{
int l = x / div;
int r = x % 10;
if (l != r)
return 0;
x = (x % div) / 10;
div /= 100;
}
return 1;
}
package main
import "fmt"
func main() {
n := 123454321
r := reverse(n)
fmt.Println(r == n)
}
func reverse(n int) int {
r := 0
for {
if n > 0 {
r = r*10 + n%10
n = n / 10
} else {
break
}
}
return r
}
public static void main(String args[])
{
System.out.print("Enter a number: ");
Scanner input = new Scanner(System.in);
int num = input.nextInt();
int number = num;
int reversenum = 0;
while (num != 0)
{
reversenum = reversenum * 10;
reversenum = reversenum + num % 10;
num = num / 10;
}
if (number == reversenum)
System.out.println("The reverse number is " + reversenum + "\nThen the number is palindrome.");
else
System.out.println("The reverse number is " + reversenum + "\nThen the number is not palindrome.");
}
private static boolean isPalindrome(int n) {
if (n < 0)
return false;
int div = 1;
// find the divisor
while (n / div >= 10)
div *= 10;
// any number less than 10 is a palindrome
while (n != 0) {
int leading = n / div;
int trailing = n % 10;
if (leading != trailing)
return false;
// % with div gets rid of leading digit
// dividing result by 10 gets rid of trailing digit
n = (n % div) / 10;
// got rid of 2 numbers, update div accordingly
div /= 100;
}
return true;
}
#include<bits/stdc++.h>
using namespace std;
vector<int>digits;
stack<int>digitsRev;
int d,number;
bool isPal=1;//initially assuming the number is palindrome
int main()
{
cin>>number;
if(number<10)//if it is a single digit number than it is palindrome
{
cout<<"PALINDROME"<<endl;
return 0;
}
//if the number is greater than or equal to 10
while(1)
{
d=number%10;//taking each digit
number=number/10;
//vector and stack will pick the digits
//in reverse order to each other
digits.push_back(d);
digitsRev.push(d);
if(number==0)break;
}
int index=0;
while(!digitsRev.empty())
{
//Checking each element of the vector and the stack
//to see if there is any inequality.
//And which is equivalent to check each digit of the main
//number from both sides
if(digitsRev.top()!=digits[index++])
{
cout<<"NOT PALINDROME"<<endl;
isPal=0;
break;
}
digitsRev.pop();
}
//If the digits are equal from both sides than the number is palindrome
if(isPal==1)cout<<"PALINDROME"<<endl;
}
// Checks if our string is palindromic.
var ourString = "A Man, /.,.()^&*A Plan, A Canal__-Panama!";
isPalin(ourString);
function isPalin(string) {
// Make all lower case for case insensitivity and replace all spaces, underscores and non-words.
string = string.toLowerCase().replace(/\s+/g, "").replace(/\W/g,"").replace(/_/g,"");
for(i=0; i<=Math.floor(string.length/2-1); i++) {
if(string[i] !== string[string.length-1-i]) {
console.log("Your string is not palindromic!");
break;
} else if(i === Math.floor(string.length/2-1)) {
console.log("Your string is palindromic!");
}
}
}
/*Palindrome number*/
String sNumber = "12321";
int l = sNumber.length(); // getting the length of sNumber. In this case its 5
boolean flag = true;
for (int i = 0; i <= l; ++i) {
if (sNumber.charAt(i) != sNumber.charAt((l--) -1)) { //comparing the first and the last values of the string
System.out.println(sNumber +" is not a palindrome number");
flag = false;
break;
}
//l--; // to reducing the length value by 1
}
if (flag) {
System.out.println(sNumber +" is a palindrome number");
}
n = 332
q = n / 10 = 33
r = n - 10 * q = 2
r > 0
r != q
n = q = 33
n > r
q = n / 10 = 3
r -= q = 4294967295
r *= 10 = 4294967286
r += n = 23
r != n
r != q
n = q = 3
n > r ? No, so 332 isn't a palindromic number.
溢出也不是问题。
有两个部分是必要的,在代码(C #)中,它们是用乘法完成的。
一个 n 位数字: ~ n/2除法!
const ulong c0 = 0xcccccccdUL;
static bool isPal(uint n)
{
if (n < 10) return true;
uint q = (uint)(c0 * n >> 35);
uint r = n - 10 * q;
if (r == 0) return false;
if (r == q) return true;
n = q;
while (n > r)
{
q = (uint)(c0 * n >> 35);
r -= q;
r *= 10;
r += n;
if (r == n || r == q) return true;
n = q;
}
return false;
}
有142948个回文数 < 2 ^ 32,它们的总和是137275874705916。
using System;
class Program
{
static void Main() // take a break
{
uint n, c; var sw = System.Diagnostics.Stopwatch.StartNew();
n = ~0u; c = 0; sw.Restart(); while (n > 0) if (isPal0(n--)) c++;
Console.WriteLine(sw.Elapsed + " " + c); // 76 s
n = ~0u; c = 0; sw.Restart(); while (n > 0) if (isPal1(n--)) c++;
Console.WriteLine(sw.Elapsed + " " + c); // 42 s
n = ~0u; c = 0; sw.Restart(); while (n > 0) if (isPal2(n--)) c++;
Console.WriteLine(sw.Elapsed + " " + c); // 31 s
Console.Read();
}
static bool isPal0(uint u)
{
uint n = u, rev = 0;
while (n > 0) { uint dig = n % 10; rev = rev * 10 + dig; n /= 10; }
return u == rev;
}
static bool isPal1(uint u)
{
uint n = u, r = 0;
while (n >= 10) r = n + (r - (n /= 10)) * 10;
return u == 10 * r + n;
}
static bool isPal2(uint n)
{
if (n < 10) return true;
uint q = n / 10, r = n - 10 * q;
if (r == 0 || r == q) return r > 0;
while ((n = q) > r)
{
q /= 10; r -= q; r *= 10; r += n;
if (r == n || r == q) return true;
}
return false;
}
}
这个似乎更快。
using System;
class Program
{
static void Main()
{
uint n, c; var sw = System.Diagnostics.Stopwatch.StartNew();
n = ~0u; c = 0; sw.Restart(); while (n > 0) if (isPal(n--)) c++;
Console.WriteLine(sw.Elapsed + " " + c); // 21 s
Console.Read();
}
static bool isPal(uint n)
{
return n < 100 ? n < 10 || n % 11 == 0 :
n < 1000 ? /* */ n / 100 == n % 10 :
n < 10000 ? n % 11 == 0 && n / 1000 == n % 10 && isP(n) :
n < 100000 ? /* */ n / 10000 == n % 10 && isP(n) :
n < 1000000 ? n % 11 == 0 && n / 100000 == n % 10 && isP(n) :
n < 10000000 ? /* */ n / 1000000 == n % 10 && isP(n) :
n < 100000000 ? n % 11 == 0 && n / 10000000 == n % 10 && isP(n) :
n < 1000000000 ? /* */ n / 100000000 == n % 10 && isP(n) :
n % 11 == 0 && n / 1000000000 == n % 10 && isP(n);
}
static bool isP(uint n)
{
uint q = n / 10, r = n - 10 * q;
do { n = q; q /= 10; r -= q; r *= 10; r += n; } while (r < q);
return r == q || r == n;
}
}
用一个几乎平衡的二叉搜索意大利面条树。
using System;
class Program
{
static void Main()
{
uint n, c; var sw = System.Diagnostics.Stopwatch.StartNew();
n = c = 0; sw.Restart(); while (n < ~0u) if (isPal(n++)) c++;
Console.WriteLine(sw.Elapsed + " " + c); // 17 s
Console.Read();
}
static bool isPal(uint n)
{
return n < 1000000 ? n < 10000 ? n < 1000 ? n < 100 ?
n < 10 || n % 11 == 0 : n / 100 == n % 10 :
n / 1000 == n % 10 && isP(n) : n < 100000 ?
n / 10000 == n % 10 && isP(n) :
n / 100000 == n % 10 && isP(n) :
n < 100000000 ? n < 10000000 ?
n / 1000000 == n % 10 && isP(n) :
n % 11 == 0 && n / 10000000 == n % 10 && isP(n) :
n < 1000000000 ? n / 100000000 == n % 10 && isP(n) :
n % 11 == 0 && n / 1000000000 == n % 10 && isP(n);
}
static bool isP(uint n)
{
uint q = n / 10, r = n - 10 * q;
do { n = q; q /= 10; r -= q; r *= 10; r += n; } while (r < q);
return r == q || r == n;
}
}
上下颠倒。
using System;
class Program
{
static void Main()
{
uint n, c; var sw = System.Diagnostics.Stopwatch.StartNew();
n = c = 0; sw.Restart(); while (n < ~0u) if (isPal(n++)) c++;
Console.WriteLine(sw.Elapsed + " " + c); // 16 s
Console.Read();
}
static bool isPal(uint n)
{
return
n > 999999999 ? n % 11 == 0 && n / 1000000000 == n % 10 && isP(n) :
n > 99999999 ? n / 100000000 == n % 10 && isP(n) :
n > 9999999 ? n % 11 == 0 && n / 10000000 == n % 10 && isP(n) :
n > 999999 ? n / 1000000 == n % 10 && isP(n) :
n > 99999 ? n % 11 == 0 && n / 100000 == n % 10 && isP(n) :
n > 9999 ? n / 10000 == n % 10 && isP(n) :
n > 999 ? n % 11 == 0 && n / 1000 == n % 10 && isP(n) :
n > 99 ? n / 100 == n % 10 :
n < 10 || n % 11 == 0;
}
static bool isP(uint n)
{
uint q = n / 10, r = n - 10 * q;
do { n = q; q /= 10; r -= q; r *= 10; r += n; } while (r < q);
return r == q || r == n;
}
}
下面是迅速的答案。
它从左边和右边读取数字,并比较它们是否相同。
这样做,我们将永远不会面临整数溢出 < em > (逆序数法可能出现的情况) 的问题,因为我们没有创建另一个数字。
步骤:
得到数字的长度
循环长度 + 1(第一个)—— > 0
得到数字,得到最后一个数字
如果两个数字不相等,返回 false,因为数字不是回文
我..
从 num 中丢弃最后一个数字(num = num/10)
厕所结束返回真
func isPalindrom(_ input: Int) -> Bool {
if input < 0 {
return false
}
if input < 10 {
return true
}
var num = input
let length = Int(log10(Float(input))) + 1
var i = length
while i > 0 && num > 0 {
let ithDigit = (input / Int(pow(10.0, Double(i) - 1.0)) ) % 10
let r = Int(num % 10)
if ithDigit != r {
return false
}
num = num / 10
i -= 1
}
return true
}
var palindromCheck(nums) = () => {
let str = x.toString()
// + before str is quick syntax to cast String To Number.
return nums === +str.split("").reverse().join("")
}