ความหลากหลายแบบดิจิทัล


16

1 <= b < infเป็นจำนวนเต็มบวกอาจจะเป็นตัวแทนในการเป็นฐานจำนวนเต็ม

เมื่อแปลงเป็นฐานนั้นจะมีจำนวนหลักที่แตกต่างกัน

จำนวนเต็มบวกใด ๆ ในฐาน1มี1หลักที่แตกต่างกัน

จำนวนเต็มบวกมากที่สุดในฐาน2มี2ตัวเลขที่แตกต่างกันข้อยกเว้นที่เป็นผู้ที่อยู่ในรูปแบบที่มีเฉพาะ2^n - 11

ดังนั้นจำนวนเต็มบวกครั้งแรกที่อาจจะเป็นตัวแทนในการเป็นฐานจำนวนเต็มกับ1หลักที่เป็นเอกลักษณ์1และเป็นครั้งแรกที่อาจจะแทนด้วยตัวเลขที่แตกต่างกันคือ22

เราสามารถพูดได้ว่า1เป็นจำนวนเต็มแรกที่มีความหลากหลายทางดิจิตอล1และเป็นจำนวนเต็มแรกที่มีความหลากหลายทางดิจิตอล22

ท้าทาย:

ป.ร. ให้ไว้เป็นจำนวนเต็มบวกnกลับจำนวนเต็มบวกเป็นครั้งแรก (ในฐานสิบ *) nที่มีความหลากหลายทางดิจิตอล

* หากภาษาของคุณรองรับเฉพาะฐานเท่านั้น (เช่น unary หรือไบนารี) จากนั้นคุณอาจส่งออกในฐานนั้น

อัลกอริทึมของคุณจะต้องทำงานในทางทฤษฎีสำหรับการป้อนจำนวนเต็มบวกใด ๆ : มันอาจล้มเหลวเนื่องจากความแม่นยำของจำนวนเต็มของภาษาของคุณเล็กเกินไปสำหรับการส่งออก; แต่อาจไม่ล้มเหลวเนื่องจากการแปลงฐานถูกกำหนดไว้ถึงบางขีด จำกัด เท่านั้น

กรณีทดสอบ

input  output
   1     1
   2     2
   3     11
   4     75
   5     694
   6     8345
   7     123717
  17     49030176097150555672
  20     5271200265927977839335179
  35     31553934355853606735562426636407089783813301667210139
  63     3625251781415299613726919161860178255907794200133329465833974783321623703779312895623049180230543882191649073441
 257     87678437238928144977867204156371666030574491195943247606217411725999221158137320290311206746021269051905957869964398955543865645836750532964676103309118517901711628268617642190891105089936701834562621017362909185346834491214407969530898724148629372941508591337423558645926764610261822387781382563338079572769909101879401794746607730261119588219922573912353523976018472514396317057486257150092160745928604277707892487794747938484196105308022626085969393774316283689089561353458798878282422725100360693093282006215082783023264045094700028196975508236300153490495688610733745982183150355962887110565055971546946484175232

นี่คือซึ่งเป็นทางออกที่สั้นที่สุดในหน่วยไบต์

OEIS: A049363 - ยังเป็นตัวเลข pandigital ที่เล็กที่สุดในฐาน n

คำตอบ:


11

เยลลี่ 4 ไบต์

ṖaWḅ

ลองออนไลน์! หรือตรวจสอบกรณีทดสอบทั้งหมด

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

ṖaWḅ  Main link. Argument: n

Ṗ     Pop; yield [1, 2, 3, ..., n-1].
  W   Wrap; yield [n].
 a    Logical AND; yield [n, 2, 3, ..., n-1].
   ḅ  Convert the result from base n to integer.

ฉันลืมค่าสถานที่อาจล้นเต้นหมัด 7 ของฉัน :)
— โจนาธานอัลลัน

ฉันหวังว่าจะมีตัวแทน vs ไบต์ใช้แผนภูมิต่อผู้ใช้ใน codegolf อาจเป็นพล็อตของจำนวนไบต์ทั้งหมดที่ใช้กับตัวแทนปัจจุบัน
— Filip Haglund

เอาฉันไปคิดดูว่าทำไมสิ่งนี้ถึงได้ผล ... ทำอย่างลื่น!
— Greg Martin

9

Python ขนาด 40 ไบต์

f=lambda n,k=1:n*(n<k+2)or-~f(n,k+1)*n-k

ทดสอบบนIdeone

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

จำนวนที่มีnตัวเลขที่แตกต่างกันอย่างชัดเจนจะต้องแสดงในฐานข≥ n เนื่องจากเป้าหมายของเราคือการลดจำนวนลงbจึงควรมีขนาดเล็กที่สุดเท่าที่จะเป็นไปได้ดังนั้นb = nจึงเป็นตัวเลือกเชิงตรรกะ

นั่นทำให้เรามีการจัดเรียงตัวเลข0, …, n-1เพื่อสร้างตัวเลขให้เล็กที่สุดเท่าที่จะทำได้ซึ่งหมายความว่าตัวเลขที่สำคัญที่สุดจะต้องถูกเก็บไว้ให้เล็กที่สุดเท่าที่จะทำได้ เนื่องจากตัวเลขแรกไม่สามารถเป็น0ในการแทนแบบบัญญัติซึ่งเป็นเลขที่เล็กที่สุดคือ
(1) (0) (2) ... (n-2) (n-1) n = n n-1 + 2n n-3 + … + (n-2) n + (n-1)ซึ่งfคำนวณซ้ำ ๆ


6

Python 2, 54 46 ไบต์

นี่เป็นสิ่งที่ดี มากๆ ! รวดเร็วโซลูชั่นซ้ำ

n=r=input();k=2
while k<n:r=r*n+k;k+=1
print r

ลองออนไลน์

ไม่มีการเรียกซ้ำดังนั้นจึงใช้งานได้กับอินพุตขนาดใหญ่ นี่คือผลลัพธ์ของn = 17000(ใช้เวลา 1-2 วินาที):

http://pastebin.com/UZjgvUSW


อินพุต 17000 ใช้เวลานานเท่าใด มันใช้เวลา 26 วินาทีในเครื่องของฉันซึ่งดูเหมือนว่าจะช้าเมื่อเทียบกับ 0.9 วินาทีของ Jelly ...
— Dennis

ที่คล้ายกัน แต่วิธีอื่นรอบน้อยสามไบต์:lambda n:n**~-n+sum(i*n**(n+~i)for i in range(2,n))
— Jonathan Allan

2
46 ไบต์และเร็วขึ้นมาก :n=r=input();k=2\nwhile k<n:r=r*n+k;k+=1\nprint r
— เดนนิส

ใช่มันยอดเยี่ยมมากในขณะที่ลูปนั้นเร็วกว่าความเข้าใจใน Python
— Jonathan Allan

@JanathanAllan นั่นไม่ใช่เหตุผล การคำนวณกำลังช้ามากในขณะที่ลูปใช้การคูณและการเพิ่มเท่านั้น
— เดนนิส


5

J, 9 ไบต์

#.],2}.i.

อิงจาก @Dennis ' วิธีการ

การใช้

   f =: #.],2}.i.
   (,.f"0) >: i. 7
1      1
2      2
3     11
4     75
5    694
6   8345
7 123717
   f 17x
49030176097150555672

คำอธิบาย

#.],2}.i.  Input: n
       i.  Get range, [0, 1, ..., n-1]
    2}.    Drop the first 2 values, [2, 3, ...., n-1]
  ]        Get n
   ,       Prepend it, [n, 2, 3, ..., n-1]
#.         Convert that to decimal from a list of base-n digits and return

มีวิธีแก้ปัญหาทางเลือกขึ้นอยู่กับการใช้ดัชนีการเปลี่ยนแปลง ได้รับการป้อนข้อมูลn , สร้างรายการของตัวเลข[0, 1, ..., n]และหาการเปลี่ยนแปลงโดยใช้ดัชนีของn ! และแปลงที่เป็นรายการของ base- nตัวเลข โซลูชันที่สอดคล้องกันใน J เป็นเวลา12 ไบต์

#.]{.!A.i.,]  Input: n
        i.    Make range [0, 1, ..., n-1]
           ]  Get n
          ,   Join, makes [0, 1, ..., n-1, n]
     !        Factorial of n
      A.      Permutation index using n! into [0, 1, ..., n]
  ]           Get n
   {.         Take the first n values of that permutation
              (This is to handle the case when n = 1)
#.            Convert that to decimal from a list of base-n digits and return

มันจะสั้นกว่า[1,0,2,3,...,n-1]ไหมในการสร้าง?
— Jonathan Allan

1
@JanathanAllan ฉันหาทางไม่ได้ แต่ฉันสังเกตเห็นว่าดัชนีการเรียงลำดับของสิ่งเหล่านั้นจะเป็น ( n -1)!
— ไมล์

4

Ruby, 37 35 34 ไบต์

->n{s=n;(2...n).map{|d|s=s*n+d};s}

คำตอบสำหรับการรับnแบบฟอร์มที่ใช้ในฐาน10234...(n-1) nใช้n=10เป็นตัวอย่าง:

เริ่มต้นด้วยn:10

คูณด้วยnและเพิ่ม 2:102

ร่วมกันโดยnเพิ่ม 3:1023

และอื่น ๆ

แก้ไข: สั้นลงเพื่อใช้แผนที่ดูเหมือนว่า

แก้ไข 2: ขอบคุณสำหรับเคล็ดลับ m-chrzan!


(2...n)จะเป็นไบต์ที่สั้นกว่า
— m-chrzan


3

CJam (9 ไบต์)

qi_,X2$tb

การสาธิตออนไลน์

การผ่า

เห็นได้ชัดว่าจำนวนที่น้อยที่สุดที่มีความหลากหลายทางดิจิตอลnถูกพบโดยฐานการแปลง ฐาน[1 0 2 3 ... n-1] อย่างไรก็ตามทราบว่าการแปลงฐานในตัวไม่ต้องใช้ตัวเลขไปอยู่ในช่วงn0 .. n-1

qi    e# Read integer from stdin
_,    e# Duplicate and built array [0 1 ... n-1]
X2$t  e# Set value at index 1 to n
b     e# Base conversion

ทราบว่าในกรณีพิเศษn = 1ที่เราจะ1 [0] 1 1 tbให้ซึ่งเป็น1 [0 1] b1


3

Haskell, 31 ไบต์

f n=foldl((+).(*n))n[2..n-1]

แปลงรายการ[n,2,3,...,n-1]เป็นเบสnโดยใช้วิธีของฮอร์เนอร์ผ่านการพับ รุ่นแข็งแรงเล่นกอล์ฟน้อยกว่านี้จะได้รับในหน้า OEIS

ขอบคุณ nimi สำหรับ 3 ไบต์!


ฉันไม่ทราบว่า Haskell ดีเกินไปพับต้องใช้ชื่อฟังก์ชั่น ( f?) จะถูกต้องเป็นโซลูชั่นกอล์ฟหรือไม่? (เป็นเพียงfไม่อ้างอิงในรหัส)
— Jonathan Allan

@JonathanAllan The lambda function form in Haskell is \n->fold1..., which is just as long as naming it. You can write a point-free function where the input variable isn't named by combining sub-functions, but that would be awful here with three references to n.
— xnor

Cool, thanks for the explanation. Haskell syntax confuses me somewhat.
— Jonathan Allan

You can use foldl and start with n: f n=foldl((+).(*n))n[2..n-1]
— nimi

3

05AB1E, 9 bytes

DL¦¨v¹*y+

Try it online!

Explanation

n = 4 used for example.

D           # duplicate input
            # STACK: 4, 4
 L          # range(1, a)
            # STACK: 4, [1,2,3,4]
  ¦¨        # remove first and last element of list
            # STACK: 4, [2,3]
    v       # for each y in list
     ¹*     # multiply current stack with input
       y+   # and add y
            # STACK, first pass: 4*4+2 = 18
            # STACK, second pass: 18*4+3 = 75

2

C++ - 181 55

Was about to post that real cool solution using <numeric>:

#import <vector>
#import <numeric>
using namespace std;int f(int n){vector<int> v(n+1);iota(v.begin(),v.end(),0);swap(v[0],v[1]);return accumulate(v.begin(),v.end()-1,0,[n](int r,int a){return r*n+a;});}

and then i realized it is way easier:

int g(int n){int r=n,j=2;for(;j<n;)r=r*n+j++;return r;}

2

Perl 6,  34 31  30 bytes

Translated from the Haskell example on the OEIS page.

{(1,0,|(2..^$^n)).reduce: $n×*+*}        # 34
{(1,0,|(2..^$^n)).reduce: $n* *+*}       # 34

{reduce $^n×*+*,1,0,|(2..^$n)}           # 31
{[[&($^n×*+*)]] 1,0,|(2..^$n)}           # 31

{reduce $_×*+*,1,0,|(2..^$_)}            # 30
  • [&(…)] turns … into an in-place infix operator
  • The […] shown above turns an infix op into a fold (left or right depending on the operator associativity)

Expanded:

{
  reduce

    # declare the blocks only parameter 「$n」 ( the 「^」 twigil )
    # declare a WhateverCode lambda that takes two args 「*」
    $^n × * + *

    # a list that always contains at least (1,0)
    1, 0,
    # with a range slipped in
    |(
      2 ..^ $n # range from 2 up-to and excluding 「$n」
               # it is empty if $n <= 2
    )
}

Usage:

my &code = {reduce $_×*+*,1,0,|(2..^$_)}

say code 1; # 1
say code 2; # 2
say code 3; # 11
say code 4; # 75
say code 7; # 123717

# let's see how long it takes to calculate a largish value:

my $start-time = now;
$_ = code 17000;
my $calc-time = now;
$_ = ~$_; # 25189207981120412047...86380901260421982999
my $display-time = now;

say "It takes only { $calc-time - $start-time } seconds to calculate 17000";
say "but { $display-time - $calc-time } seconds to stringify"

# It takes only 1.06527824 seconds to calculate 17000
# but 5.3929017 seconds to stringify

2

Brain-Flak, 84 76 bytes

Thanks to Wheat Wizard for golfing 8 bytes

(({})<>){(({}[()]))}{}(<{}{}>)((())){{}({<({}[()])><>({})<>}{}{})([][()])}{}

Try it online!

Explanation

The program pushes the values from 0 to n-1 to the stack replaces the top 0 and 1 with 1 and 0. Then it multiplies the top of the stack by n and adds the value below it until there is only one value remaining on the stack.

Essentially it finds the digits for the smallest number in base n that contains n different digits (for n > 1 it's always of the form 1023...(n-1)). It then calculates the number given the digits and the base.

Annotated Code

(({})<>)       # Pushes a copy of n to the right stack and switches to right stack
{(({}[()]))}{} # While the top of the stack != 0 copy the top of the stack-1
               #   and push it
(<{}{}>)       # Discard the top two values (0 and 1 for n > 1) and push 0
((()))         # Push 1 twice (second time so that the loop is works properly)
{{}            # Loop while stack height > 1
  (            #   Push...
    {<({}[()])><>({})<>}{} # The top value of the stack * n
    {}         #     Plus the value below the top of the stack
  )            #   End push
([][()])}{}    # End loop

You can replace {}{}(()(<()>))([][()]) with (<{}{}>)([(())][]) to save four bytes
— Post Rock Garf Hunter

You could then replace that with (<{}{}>)((())) to save four more bytes
— Post Rock Garf Hunter



1

PHP, 78 Bytes

for(;$i<$a=$argn;)$s=bcadd($s,bcmul($i<2?1-$i:$i,bcpow($a,$a-1-$i++)));echo$s;

Online Version

60 Bytes works only till n=16 with the precision in the testcases

For n=144 INF

n=145 NAN

for(;$j<$a=$argn;)$t+=($j<2?1-$j:$j)*$a**($a-1-$j++);echo$t;


โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.