จาก Palindromes

Dec 28 2020

เลขพาลินโดรมิกเป็นตัวทบทวนคือตัวเลขใด ๆ ที่อ่านไปข้างหน้าเหมือนกับข้างหลัง อย่างไรก็ตาม Palindromes ในฐานอื่น ๆ ล่ะ?

อินพุต

จำนวนเต็มใด ๆที่bb > 1

เอาต์พุต

จำนวนเต็มฐาน 10 ทั้งหมดตั้งแต่ 0 ถึง 1,000 รวมที่เป็น palindromes ในฐาน b ผลลัพธ์อาจเป็นรายการของจำนวนเต็มหรือจำนวนเต็มคั่นด้วยตัวคั่นเช่นลูกน้ำหรือขึ้นบรรทัดใหม่

กรณีทดสอบ

Input->Output

10->{0,1,2,3,4,5,6,7,8,9,11,22,33,44,55,66,77,88,99,101,111,121,131,141,151,161,171,181,191,202,212,222,232,242,252,262,272,282,292,303,313,323,333,343,353,363,373,383,393,404,414,424,434,444,454,464,474,484,494,505,515,525,535,545,555,565,575,585,595,606,616,626,636,646,656,666,676,686,696,707,717,727,737,747,757,767,777,787,797,808,818,828,838,848,858,868,878,888,898,909,919,929,939,949,959,969,979,989,999}

2->{0,1,3,5,7,9,15,17,21,27,31,33,45,51,63,65,73,85,93,99,107,119,127,129,153,165,189,195,219,231,255,257,273,297,313,325,341,365,381,387,403,427,443,455,471,495,511,513,561,585,633,645,693,717,765,771,819,843,891,903,951,975}

9->{0,1,2,3,4,5,6,7,8,10,20,30,40,50,60,70,80,82,91,100,109,118,127,136,145,154,164,173,182,191,200,209,218,227,236,246,255,264,273,282,291,300,309,318,328,337,346,355,364,373,382,391,400,410,419,428,437,446,455,464,473,482,492,501,510,519,528,537,546,555,564,574,583,592,601,610,619,628,637,646,656,665,674,683,692,701,710,719,728,730,820,910,1000}

คำตอบ

12 dingledooper Dec 29 2020 at 03:01

Python 3 , 78 ไบต์

แสดงตัวเลขตามลำดับที่ลดลง1000 -> 0และลัดวงจรด้วย aZeroDivisionError

def f(b,n=1000):
 r=0;m=n
 while m:r=r*b+m%b;m//=b
 n==r==print(n);f(b,n-n//n)

ลองออนไลน์!

เกิดf(b,n-n//n) -> f(b,n-1)ซ้ำจนกว่า0และข้อผิดพลาดเนื่องจากการหารด้วยศูนย์ไม่ได้กำหนดไว้

Python 3 , 76 ไบต์

เราสามารถย่อคำตอบให้สั้นลงได้ 2 ไบต์หากอนุญาตให้ใช้เอาต์พุตทศนิยม

def f(b,n=1e3):
 r=0;m=n
 while m:r=r*b+m%b;m//=b
 n==r==print(n);f(b,n-n/n)

ลองออนไลน์!

10 EasyasPi Dec 28 2020 at 13:02

C (GCC)ส่งต่อ118 117 115 ไบต์

b[11],*p,*x,i,m;f(n){for(i=-1;i++<1e3;){for(p=x=b,m=i;m;*p++=m%n,m/=n);while(p>x)m|=*--p-*x++;m||printf("%d,",i);}}

ลองออนไลน์!

C (GCC)ย้อนกลับ115 113 ไบต์

b[11],*p,*x,i,m;f(n){for(i=1001;i--;){for(p=x=b,m=i;m;*p++=m%n,m/=n);while(p>x)m|=*--p-*x++;m||printf("%d,",i);}}

ลองออนไลน์!

คำอธิบาย

ลายเซ็น C:

// Technically implicit int with a void return
void f(int base);

วนซ้ำตัวเลขทั้งหมดตั้งแต่ 0 ถึง 1,000 แปลงเป็นฐานbaseด้วยมือจากนั้นตรวจสอบว่าเป็นพาลินโดรมหรือไม่

รุ่นถอยหลังทำสิ่งเดียวกัน แต่ย้อนกลับ

พิมพ์ตัวเลขที่ตรงกันโดยคั่นด้วยเครื่องหมายจุลภาคเป็น stdout

เวอร์ชันที่ไม่ถูกทำลาย

#include <stdio.h>
// A buffer to hold our converted integer.
// It is large enough for 1000 in binary.
int buffer[11];
// Start and end pointers for buffer
int *start, *end;
// Loop counter
int i;
// Temporary
int tmp;

void f(int base)
{
    // Loop for 0 to 1000
#ifdef BACKWARDS
    // Loop backwards
    for (i = 1001; i-- != 0;) {
#else
    // Loop forwards
    // for (i = 0; i <= 1000; i++)
    for (i = -1; i++ < 1e3; ) {
#endif
        // Convert to base in buffer, tracking the length in end.
        for(start = end = buffer, tmp = i; tmp != 0;) {
            *end++ = tmp % base;
            tmp /= base;
        }

        // Check if it is a palindrome.
        // Loop while our starting pointer is less than our ending pointer.
        // tmp will zero at the start thanks to the loop condition.
        while (end > start)
            // Assembly style comparison using subtraction.
            // If *end == *start, tmp will still be zero.
            // If not, it will be permanently set to non-zero with a binary or.
            tmp |= *--end - *start++;
        // If tmp is still zero (meaning it is a palindrome), print.
        tmp || printf("%d,", i);
    }
}

ขอบคุณ Arnauld สำหรับ -1 ไบต์!

ขอบคุณ Toby Speight สำหรับ -2 ไบต์!

10 Lyxal Dec 28 2020 at 04:54

05AB1E , 7 ไบต์

₄ÝʒIвÂQ

ลองออนไลน์!

อธิบาย

₄Ý	"Push the range [0, 1000]"\
  ʒ	"and keep the items where:"\
   Iв	"After being converted to base (input)"\
     ÂQ	"have its reverse equal to itself"\
6 cairdcoinheringaahing Dec 28 2020 at 04:42

เยลลี่ 7 ไบต์

ȷŻbŒḂ¥Ƈ

ลองออนไลน์!

มันทำงานอย่างไร

ȷŻbŒḂ¥Ƈ - Main link. Takes a base b on the left
ȷ       - 1000
 Ż      - [0, 1, 2, ..., 1000]
     ¥  - Group the previous 2 links into a dyad f(k, b):
  b     -   Convert k to base b
   ŒḂ   -   Is this a palindrome?
      Ƈ - Filter [0, 1, 2, ..., 1000], keeping those k that are true under f(k, b)
6 Shaggy Dec 28 2020 at 07:48

Japt , 11 ไบต์

A³ô fÈìU êê

ลองมัน

6 J42161217 Dec 28 2020 at 04:53

ภาษา Wolfram (Mathematica) , 44 ไบต์

Pick[r=0~Range~1000,r-r~IntegerReverse~#,0]&

ลองออนไลน์!

-13 ไบต์จาก @att

6 Arnauld Dec 28 2020 at 07:35

JavaScript (ES6),  87  86 ไบต์

ส่งคืนสตริงที่คั่นด้วยจุลภาค

n=>(g=k=>--k&&g(k)+((h=k=>a=k?[k%n,...h(k/n|0)]:[])(k)+''==a.reverse()?[,k]:''))(1001)

ลองออนไลน์!

อย่างไร?

n => (                        // n = input base
  g = k =>                    // g is a recursive function taking a counter k
    --k &&                    //   decrement k; abort if it's equal to 0
    g(k) + (                  //   otherwise do a recursive call and append the ...
      ( h = k =>              //   ... result of the recursive function h
        a = k ?               //     which builds an array a[]
          [ k % n,            //     consisting of each digit of k in base n,
            ...h(k / n | 0) ] //     dividing k by n and taking the integer part
        :                     //     for the next iteration until k = 0
          []                  //
      )(k) + ''               //   invoke h with k and coerce the result to a string
      == a.reverse() ?        //   if this is palindromic:
        [, k]                 //     append a comma followed by k to the output
      :                       //   else:
        ''                    //     just append an empty string
    )                         //
)(1001)                       // initial call to g with k = 1001
6 user Dec 28 2020 at 05:16

Scala , 62 87 ไบต์

  • แก้ไขหลังจาก Siu Ching Pong -Asuka Kenji- ชี้ให้เห็นBigIntว่าtoStringใช้ได้กับฐานที่สูงถึง 36 เท่านั้น
  • ที่บันทึกไว้ 1 ขอบคุณไบต์ผักกาดหอม @cubic
b=>0 to 1000 filter{x=>val y=Seq.unfold(x){q=>Option.when(q>0)(q%b,q/b)};y==y.reverse}

ลองออนไลน์!

นี่ค่อนข้างตรงไปตรงมา มันสร้างช่วงตั้งแต่ 0 ถึง 1,000 จากนั้นกรองโดยตรวจสอบว่ามันเท่ากับการย้อนกลับในฐานbหรือไม่ การแปลงไปยังฐานb(เป็นสตริง) BigInt's toStringวิธีการจะถูกนำมาใช้ แต่ตอนนี้Seq.unfoldถูกนำมาใช้เพื่อสร้างSeqตัวเลข

6 DominicvanEssen Dec 28 2020 at 08:04

แกลบ , 12 11 ไบต์

แก้ไข: -1 ไบต์ขอบคุณ LegionMammal978

foS=↔B⁰ŀdḋ9

ลองออนไลน์!

รหัส 'based palindrome' จริงคือ 7 ไบต์ ( foS=↔B⁰) แต่การระบุ 0 ... 1,000 มีค่าใช้จ่าย5 4 (ขอบคุณ LegionMammal978) ไบต์เพิ่มเติม
เราสามารถบันทึกไบต์ได้หากเป็นไปได้ที่จะส่งออก palindromes ตามอีกสองสามตัวที่มีค่าสูงสุด 1024 ( foS=↔B⁰ŀ□32)

f               # output the truthy values of
       ŀdḋ9     # series from zero up to one less than 1001
                # (decimal interpretation of binary digits of '9')
 o              # based on combination of 2 functions:
  S=↔           # 1. is it equal to reverse of itself?
     B⁰         # 2. digits in base given by argument
5 Neil Dec 28 2020 at 05:33

ถ่าน 14 ไบต์

NθIΦ⊕φ⁼↨ιθ⮌↨ιθ

ลองออนไลน์! ลิงก์คือรหัสเวอร์ชันที่ละเอียด คำอธิบาย:

Nθ              Input the base `b`
     φ          Predefined variable 1000
    ⊕           Incremented
   Φ            Filter on implicit range
        ι       Current value
       ↨ θ      Converted to base `b`
      ⁼         Equals
            ι   Current value
           ↨ θ  Converted to base `b`
          ⮌     Reversed
  I             Cast to string
                Implicitly print
5 xnor Dec 29 2020 at 10:59

Haskell , 63 ไบต์

f b|let 0%m=m;n%m=div n b%(m*b+mod n b)=[n|n<-[0..1000],n==n%0]

ลองออนไลน์!

ตามแนวคิดที่ดีจากคำตอบ Python ของ dingledooper : เพื่อตรวจสอบว่าnเป็นฐาน - bpalindrome อย่าสร้างรายการของbตัวเลขฐานแต่กลับnเป็นbตัวเลขฐานโดยการเรียกใช้ตัวเลขการอ่านการแปลงฐานจากจุดสิ้นสุดและ nตรวจสอบว่าผลที่ได้ยังคงเท่ากับ

รหัส|let 0%m=m;n%m=div n b%(m*b+mod n b)ซ้ำกำหนดฟังก์ชัน infix %ที่ย้อนกลับฐานn(กำหนดให้0เป็นอาร์กิวเมนต์ที่สองเริ่มต้น) การกำหนดไว้ในตัวletป้องกันช่วยให้เราสามารถเข้าถึงอาร์กิวเมนต์bของฟังก์ชันหลักได้ในขณะที่ฟังก์ชันแบบสแตนด์อโลนจะต้องส่งผ่านไปเรื่อย ๆ ด้วยการเรียกซ้ำแต่ละครั้ง

5 ovs Dec 28 2020 at 19:02

APL (Dyalog Extended) , 17 15 ไบต์

ขอบคุณRazetimeสำหรับ -2 ไบต์!
แก้ไขข้อผิดพลาดด้วยSiu Ching Pong !

0ต้องใช้แหล่งกำเนิดดัชนี

⍸⎕(⊤≡∘⌽⊤)¨⍳1001

ลองออนไลน์!

                 ⍝ tradfn taking the base as input
          ⍳1001  ⍝ the indices up to 1000
 ⍵(     )¨       ⍝ apply a function to each index as a right argument and the input base as a left argument:
      ⌽⊤         ⍝  the reverse of the index converted to the input base 
    ≡            ⍝  does it match 
   ⊤             ⍝  the index converted to the input base
⍸                ⍝ all truthy indices
5 TobySpeight Dec 29 2020 at 00:06

C - 76 ไบต์

i=1001,a,z;f(b){for(;i--;i-z||printf("%d ",i))for(a=i,z=0;a;a/=b)z=z*b+a%b;}

คำอธิบาย

แตกต่างจากคำตอบก่อนหน้านี้อย่างเพียงพอที่จะรับประกันการโพสต์แยกกัน คราวนี้เรากลับเลขทั้งหมดแล้วเปรียบเทียบกับตัวเลขเดิม 0ดังนั้นเราจึงไม่จำเป็นต้องกำจัดศูนย์ต่อท้ายหรือกรณีพิเศษ

void fun(int b)
{
    for (int i = 1001; i--;) {
        int z = 0;
        for (int a = i; a != 0; a /= b) {
            z = z*b + a%b;
        }
        if (i==z) {
            printf("%d ",i);
        }
    }
}

วิธีนี้ใช้งานได้อย่างน่าเชื่อถือสำหรับiup to INT_MAX/bและbup to INT_MAXหรือเทียบเท่าที่เหมาะสมหากเราเปลี่ยนประเภทจำนวนเต็มที่ใช้ สำหรับประเภทที่ไม่ได้ลงนาม (หรือมีgcc -fwrapv) ควรใช้งานได้เต็มรูปแบบของi.

4 TobySpeight Dec 28 2020 at 23:28

C, 100 ไบต์

i=1001,a,z;f(b){for(;--i;)for(a=i,z=0;i%b*a;a/=b)if(a==z||a==(z=z*b+a%b))printf("%d ",i);puts("0");}

ลองออนไลน์

รหัสที่ไม่ได้รับการแก้ไข

void fun(int b)
{
    for (int i = 1001; --i;) {
        if (i%b) {              /* no leading/trailing zeros */
            for (int a = i, z = 0; a != 0; a /= b) {
                if (a==z) {
                    printf("%d ",i);
                }
                z = z*b + a%b;
                if (a==z) {
                    printf("%d ",i);
                }
            }
        }
    }
    puts("0");
}

คำอธิบาย

ผลลัพธ์นี้จะแสดงตัวเลขสูงสุดก่อนเนื่องจากไม่มีการระบุลำดับใด ๆ สำหรับหมายเลขผู้สมัครแต่ละหมายเลขเราจะลดจำนวน (เป็นa) โดยหารด้วยฐานอย่างต่อเนื่องโดยใช้ส่วนที่เหลือเพื่อสร้างหมายเลขย้อนกลับ (in z) ถ้าaเท่ากับแสดงzว่าเรามีพาลินโดรม ปกติเราจะหยุดอยู่แค่นั้น ( a >= zในสภาพที่ห่วง) a==0แต่สำหรับการเล่นกอล์ฟเรายังคงไปทาง

เราจำเป็นต้องทดสอบความเท่าเทียมกันทั้งก่อนและหลังการถ่ายโอนส่วนที่เหลือzเพื่อยอมรับทั้งคู่คี่และคู่ที่มีความยาว

สุดท้ายเราพิมพ์0ซึ่งมักจะเป็นพาลินโดรมและง่ายกว่ากรณีพิเศษกว่าการรวมไว้ในลูป

วิธีนี้ใช้ได้กับจำนวนเต็มถึงINT_MAXถ้าเราคลายเงื่อนไขi%b*aกลับไปi%b&&aและยังใช้ได้กับจำนวนเต็มประเภทอื่น ๆ

4 coltim Dec 28 2020 at 23:57

K (ngn / k) 18 ไบต์

{&{x~|x}'x\'!1001}

ลองออนไลน์!

  • x\'!1001 แปลงการแทนค่า 0..1000 ให้เป็นฐาน -x
  • {x~|x}' ตรวจสอบว่าแต่ละตัวแทนเป็นพาลินโดรมหรือไม่
  • & รับดัชนีของการจริง
4 Danis Dec 28 2020 at 12:59

Python 3.8 (ก่อนวางจำหน่าย) , 92 85 ไบต์

lambda b:[i for i in range(1001)if(f:=lambda n:n*[0]and[n%b]+f(n//b))(i)==f(i)[::-1]]

ลองออนไลน์!

ขอบคุณdingledooperสำหรับการประหยัด 7 ไบต์!

4 DanielWagner Dec 29 2020 at 01:45

Haskell, 67 ไบต์

b&n=take n$mod n b:b&div n b
f b=[n|n<-[0..1000],reverse(b&n)==b&n]

fคือฟังก์ชั่นที่น่าสนใจ ลองออนไลน์!

บางทีบิตที่ชาญฉลาดเพียงอย่างเดียวในที่นี้คือการใช้take nเพื่อสร้างเคสพื้นฐานสำหรับฟังก์ชันการขยายตัวเลข เมื่อn=0ใดที่take nเพิกเฉยต่อข้อโต้แย้งของมันดังนั้นการเรียกซ้ำจะหยุดลงด้วยความเกียจคร้าน เมื่อn>0มีจะไม่แน่นอนมากขึ้นกว่าตัวเลขจึงปลอดภัยที่จะให้เพียงครั้งแรกn nคำจำกัดความต่อไปนี้เทียบเท่า (และยาวเท่ากัน):

b&0=[]
b&n=mod n b:b&div n b

... แต่take nเวอร์ชั่นสนุกกว่าเพราะงงกว่า ^ _ ^

4 Jonah Dec 28 2020 at 13:16

J , 27 ไบต์

((-:|.)@(#.inv)"0#])i.@1001

อย่างไร

  • (...) i.@1001 - สิ่งทั้งหมดคือ J hook ซึ่งหมายความว่าอาร์กิวเมนต์จะเป็นอาร์กิวเมนต์ด้านซ้ายสำหรับทุกอย่างใน parens และ arg ที่ถูกต้องจะเป็นจำนวนเต็มตั้งแต่ 0 ถึง 1,000: i.@1001
  • ...#]วลีภายใน parens ใช้สำเนา#เพื่อกรองอาร์กิวเมนต์ที่ถูกต้อง]โดยบูลีนมาสก์ซึ่งเป็นผลมาจากวลีทางด้านซ้ายของ#:
  • (-:|.)@(#.inv)"0- อันดับ 0 "0ทำให้แน่ใจว่าวลีนี้ใช้กับหมายเลขของอาร์กิวเมนต์ที่ถูกต้องแต่ละตัว วลีนั้นจะแปลงตัวเลขแต่ละตัวให้เป็นรายการหลักในฐานที่ระบุโดยอาร์กิวเมนต์ด้านซ้าย(#.inv)จากนั้นตรวจสอบว่ารายการนั้นเท่ากับกลับ(-:|.)@กันหรือไม่ ดังนั้นวลีทั้งหมดจะคืนค่า 1 เมื่อเป็นจริงและเป็น 0 มิฉะนั้นมาสก์บูลีนนี้จะกรองอาร์กิวเมนต์ที่ถูกต้องตามต้องการ

ลองออนไลน์!

3 vrintle Dec 28 2020 at 11:13

Ruby 2.7 , 74 ไบต์

->b{(0..1e3).select{(a=(g=->k,r=[]{k>0?g[k/b,r<<k%b]:r})[_1])==a.reverse}}

ลองออนไลน์!

TIO ใช้ Ruby เวอร์ชันเก่ากว่าในขณะที่ Ruby 2.7 เราได้กำหนดหมายเลขพารามิเตอร์ซึ่งจะช่วยประหยัดสองไบต์


ทับทิม 48 ไบต์

->b{(0..1e3).select{|k|(k=k.to_s b)==k.reverse}}

ลองออนไลน์!

ใช้ไม่ได้กับฐานที่มากกว่า 64 เนื่องจากข้อ จำกัด ใน.to_sวิธีการ

3 NinaLisitsinskaya Dec 29 2020 at 02:07

JavaScript (V8) , 77 89 ไบต์

คงที่สำหรับฐานที่มากกว่า 36

b=>{for(i=-1;i<1e3;){j=[],k=++i;while(k|=0)j.push(k%b),k/=b;''+j==j.reverse()&&print(i)}}

ลองออนไลน์!

3 ZaelinGoodman Dec 29 2020 at 03:26

PowerShell , 102 100 98 95 87 75 ไบต์

-14 ไบต์ขอบคุณ mazzy!

param($u)0..1e3|?{for($b=@();$_=($_-($b+=$_%$u)[-1])/$u){}"$b"-eq$b[11..0]}

ลองออนไลน์!

2 DominicvanEssen Dec 28 2020 at 19:44

R , 82 81 ไบต์

(หรือ79 ไบต์โดยใช้ตัวคั่นที่ค่อนข้างซับซ้อนของ " \n[1] ")

แก้ไข: -1 ไบต์ขอบคุณ caird coinheringaahing

function(b)for(i in 0:1e3)if(!i||all((a=i%/%b^(0:log(i,b))%%b)==rev(a)))cat(i,'')

ลองออนไลน์!

คำนวณตัวเลขด้วยตนเองในการแทนค่าฐานใหม่และตรวจสอบว่าเหมือนกันหรือไม่

function(b)
 for(i in 0:1000)               # loop i through zero to 1000
  if(!i                         # if i is zero (always a palindrome),
   ||                           # or
   all(                         # if all the digits of
    (a=i%/%b^(0:log(i,b))%%b)   # a = the representation of i in base b
    ==rev(a))                   # are the same as themselves reversed
  )cat(i,'')                    # output this i
1 2x-1 Jan 03 2021 at 10:09

jq , 66 ไบต์

. as$a|range(1001)|select([while(.>0;./$a|floor)|.%$a]|reverse==.)

ลองออนไลน์!

คำอธิบาย

. as $a |                # Assign the input to $a. range(1001) | # For every item in [0..1000]: select ( # Filter out all items where: [ while(. > 0; # The list of quotients from repeatedly . / $a | floor)     #     short-dividing by $a |. % $a]           #     And then modulo-ing by $a
  | reverse == .)        # is equal to its reverse
```
1 hakr14 Jan 03 2021 at 10:47

Pyth , 11 ไบต์

f_IjTQUh^T3

ลองออนไลน์!


f_IjTQUh^T3 | Explanation
------------+---------------------------------------
f           | filter
      Uh^T3 | the range [0, 1001)
   jTQ      | on whether each number in base <input>
 _I         | equals itself reversed
1 KevinCruijssen Jan 07 2021 at 15:21

Java 10, 118 ไบต์

b->{for(int i=-1;i++<1e3;){var s=b.toString(i,b);if(s.contains(new StringBuffer(s).reverse()))System.out.println(i);}}

ลองออนไลน์

คำอธิบาย:

b->{                           // Method with Integer parameter and no return-type
  for(int i=-1;i++<1e3;){      //  Loop `i` in the range [0,1000]:
    var s=b.toString(i,b);     //   Convert `i` to base-`b` as String
    if(s.contains(new StringBuffer(s).reverse()))
                               //   If this String is a palindrome:
      System.out.println(i);}} //    Print `i` with trailing newline