기본 바이너리에 관한 모든 것


29

퍼니 제목을 변명하십시오.

이 질문은 82000의 흥미로운 속성에서 영감을 얻은 것 입니다. 이 글에서 저자는 82000은 2, 3, 4, 5의 이진수라고 설명합니다. 그러면 게시물은 "2, 3, 4, 5, 6의 이진수 인 숫자가 있습니까?"라는 질문을 제기합니다. "? (호기심 많은 사람들을 위해 최대 10 ^ 1,000,000까지의 값을 확인했으며 지금까지는 대답이 아니오입니다.)

이것은 나를 생각하게했습니다 : 숫자가 주어지면 바이너리 어떤베이스 에 있습니까?

우리의 호기심 많은 숫자 인 82000은 실제로 6 개의 이진수입니다.

Base 2 = 10100000001010000
Base 3 = 11011111001
Base 4 = 110001100
Base 5 = 10111000
Base 81999 = 11
Base 82000 = 10

모든 숫자에 순차적 인 이진 염기가있는 것은 아닙니다. 숫자 83521을 고려하십시오. 2, 17, 289, 83520 및 83521의 이진수입니다.

문제는 숫자가 이진수 인 염기를 결정하고 표시하는 것입니다.

규칙

  • 해당 기준의 표현이 0과 1로만 구성된 경우 주어진 기준에서 숫자는 "이진"으로 간주됩니다. 110110이진 값이지만 12345그렇지 않은 경우A380F 는 않습니다.
  • 전화 번호는 표준 입력으로 제공됩니다. 2에서 2 ^ 32-1 사이의 정수 값이며 10 진법으로 제공됩니다.
  • 오름차순으로 숫자가 2 진인 1보다 큰 각 밑을 표시하십시오. 각 밑은 자체 행에 있어야합니다. 해당 기준에 이진 값을 포함하는 경우 (아래 보너스 점수 참조) 기준과 이진 값을 공백으로 구분하십시오. 표준 출력으로의 출력 만 판단되고 표준 오류 및 기타 소스는 무시됩니다.

채점

점수는 프로그램의 크기 (바이트)입니다. 점수가 낮을수록 좋습니다.

보너스 :
프로그램이 또한 발견 된 밑에서 이진 값을 출력한다면, 점수에 0.75를 곱하십시오.
표시된 이진 값에는 추가 구두점, 불필요한 0, 소수점, 0과 1이 없어야합니다.

입력:

82000

출력 (보너스 받기) :

2 10100000001010000
3 11011111001
4 110001100
5 10111000
81999 11
82000 10

입력:

1234321

출력 (보너스 없음) :

2
1111
1234320
1234321

줄 바꿈으로 입력을 끝낼 수 있습니까?
LegionMammal978

@ LegionMammal978-으 ... 내 의도는 간단한 fgets, readline 또는 이와 유사한 것으로 입력 번호를 얻을 수 있어야한다는 것입니다.
Mr. Llama 2016 년

1
일반적으로, n항상 적어도 기지에서 이진 1(계산되지 않음), 2, n-1,와 n.
mbomb007

1
"표준 입력시 숫자가 제공됩니다"라고 말하면 STDIN만을 의미합니까, 아니면 사이트의 표준과 같이 함수 인수로 숫자를 사용할 수 있습니까?
Alex A.

보너스 부분의 이진 표현이 특정 형식을 가져야합니까? 특히 [1, 0, 1, 1, 0]괜찮을 것 10110입니까 , 아니면 숫자가 같이 연결되어야 합니까?
Jakube

답변:


14

피스, 14 13

jbf!-jQTU2tSQ

새로운 S기능 을 지적한 Jakube에게 감사드립니다 .

여기에서 시도하십시오.

온라인 버전이 너무 느립니다 1234321. 이것은 단순히 입력 값을 2에서 자신으로 변환하고 0과 1 이외의 값을 포함하는 결과를 버립니다.

설명:

                           : Q=eval(input) (implicit)
jb                         : join on newlines the list...
  f!                       : filter away nonempty values (impliticly named T)
    -jQTU2                 : sewtise difference of number converted to base and range(2)
     jQT                   : convert Q to base T
        U2                 : range(2)
          tSQ              : over the range [2 Q+1)

또한 이것은 ( Jakube 덕분에 다시 골프를 잘하지 못합니다) 보너스 버전 (20 * .75 = 15)입니다.

VQI!-JjQK+2NU2pdKjkJ

여기 사용해보십시오


Pyth just got updated. So you can link to actual solutions.
Jakube

And here's a 20 * 0.75 = 15 solution: VQI!-JjQK+2NU2pdKjkJ Sometimes functional programming is not the best approach.
Jakube

10

Julia, 72 70 bytes

It's actually longer with the bonus, so no bonus here.

n=int(readline());for j=2:n all(i->i0:1,digits(n,j))&&println(j)end

This reads a line from STDIN, converts it to an integer, and prints the result. Despite being a brute force method, the input 1234321 took less than 1 second for me.

Ungolfed + explanation:

# Read n from STDIN and convert to integer
n = int(readline())

# For every potential base from 2 to n
for j = 2:n
    # If all digits of n in base j are 0 or 1
    if all(i -> i0:1, digits(n, j))
        # Print the base on its own line
        println(j)
    end
end

Examples:

julia> n=int(readline());for j=2:n all(i->i0:1,digits(n,j))&&println(j)end
1234321
2
1111
1234320
1234321

julia> n=int(readline());for j=2:n all(i->i0:1,digits(n,j))&&println(j)end
82000
2
3
4
5
81999
82000

NOTE: If input can be taken as a function argument rather than from STDIN (awaiting confirmation from the OP), the solution is 55 bytes.


7

CJam, 20 bytes (or 27 bytes * 0.75 = 20.25)

Here is the no bonus version, 20 bytes:

ri:X,2f+{X\b2,-!},N*

Try this here.

Just for fun, here is the bonus version, 27 bytes:

ri:X{X\)b2,-!},1>{)SX2$bN}/

Try it online here


Sure. Once I am done a bit of golfing.
Optimizer

1
ri_,f{2+S@2$bN}4/{2=2,-!}, (19.5 bytes)
Dennis

7

Mathematica, 59 bytes

Print/@Select[1+Range[n=Input[]],Max@IntegerDigits[n,#]<2&]

Ugh... IntegerDigits D:

There isn't really much to explain about the code... 12 bytes are wasted by the requirement for using STDIN and STDOUT.

I don't think I can claim the bonus. The best I've got is 84 bytes (which yields a score over 60):

Print@@@Select[{b=#+1," ",##&@@n~IntegerDigits~b}&/@Range[n=Input[]],Max@##3<2&@@#&]

7

Python 2, 88 86 80

Fairly straightforward, no bonus. Python is nice and lenient with global variables.

N=input();g=lambda n:n<1or(n%b<2)*g(n/b)
for b in range(2,N+1):
 if g(N):print b

Best I've managed to get for the bonus is 118*.75 = 87.75:

N=input();g=lambda n:0**n*" "or" "*(n%b<2)and(g(n/b)+`n%b`)*(g(n/b)>'')
for b in range(2,N+1):
 if g(N):print`b`+g(N)

Nice solution, beat me to it with much shorter code.
Kade

It would be shorter to just do g(N) instead of n=N.
feersum

@feersum Oh yeah (it used to be g(N,b) so the comma made the two equal), but what do you mean I wouldn't need a variable for N?
KSab

@KSab I deleted that second part; never mind it.
feersum

I may be wrong but couldn't you get the bonus by just changing g(n/b) to (g(n/b)+'n%b') where ' represents a backtick?
feersum

4

Python 2, 90 * 0.75 = 67.5

n=input();b=1
while b<n:
 b+=1;s="";c=k=n
 while k:s=`k%b`+s;c*=k%b<2;k/=b
 if c:print b,s

Pretty straightforward iterative approach.

Without the bonus, this is 73 bytes:

n=input();b=1
while b<n:
 b+=1;c=k=n
 while k:c*=k%b<2;k/=b
 if c:print b

4

SQL (PostgreSQL), 247.5 255 230.25 (307 * .75)

Since SQL is known to be wonderful at these sorts of challenges, I thought I better put one together :) The bonus was really worthwhile for this one.
It should comply with spec, but I have no easy way to test the COPY I FROM STDIN.
Edit Fixed order. Changed the way column R is handled to use an array.

CREATE TABLE IF NOT EXISTS I(I INT);TRUNCATE TABLE I;COPY I FROM STDIN;WITH RECURSIVE R AS(SELECT n,I/n I,ARRAY[I%n] R FROM generate_series(2,(SELECT I FROM I))g(n),(SELECT I FROM I)I(I)UNION ALL SELECT n,I/n,I%n||R FROM R WHERE I>0)SELECT n||' '||array_to_string(R,'')FROM R WHERE 2>ALL(R)and i=0ORDER BY n

As a test I just used straight inserts into the I table. Test run expanded and commented.

-- Create the table to accept the input from the copy command
CREATE TABLE IF NOT EXISTS I(I INT);
-- Make sure that it is empty
TRUNCATE TABLE I;
-- Popoulate it with a value from STDIN
--COPY I FROM STDIN;
INSERT INTO I VALUES(82000); -- Testing
--Using a recursive CTE query
WITH RECURSIVE R AS (
    -- Recursive anchor
    SELECT n,                -- base for the row
       I/n I,                -- integer division
       ARRAY[I%n] R   -- put mod value in an array
    FROM generate_series(2,(SELECT I FROM I))g(n), -- series for the bases
         (SELECT I FROM I)I(I) -- Cross joined with I,  saves a few characters
    UNION ALL 
    -- for each row from r recursively repeat the division and mod until i is 0
    SELECT n,
        I/n,
        I%n||R -- Append mod to beginning of the array
    FROM R WHERE I>0
    )
-- return from r where i=0 and r has 1's and 0's only
SELECT n||' '||array_to_string(R,'')
FROM R 
WHERE 2 > ALL(R)and i=0
ORDER BY n -- Ensure correct order

2 10100000001010000
3 11011111001
4 110001100
5 10111000
81999 11
82000 10


So close! The output bases needs to be in ascending order. +1 for using an unconventional language though.
Mr. Llama

@Mr.Llama fixed it with an order by. Now to see if I can get those characters back
MickyT

3

Haskell 109*0.75 = 81.75 bytes

0#x=[]
n#x=n`mod`x:div n x#x 
f n=[show x++' ':(n#x>>=show)|x<-[2..n+1],all(<2)$n#x]
p=interact$unlines.f.read

Usage example (note: binary values are lsb first):

p 82000

2 00001010000000101
3 10011111011
4 001100011
5 00011101
81999 11
82000 01

Without input/output restrictions, i.e input via function argument, output in native format via REPL):

Haskell, 67*0.75 = 50.25 bytes

0#x=[]
n#x=n`mod`x:div n x#x
f n=[(x,n#x)|x<-[2..n+1],all(<2)$n#x]

Returns a list of (base,value) pairs. The values are lsb first, e.g. (newlines/spaces added for better display):

 f 82000
 [ (2,[0,0,0,0,1,0,1,0,0,0,0,0,0,0,1,0,1]),
   (3,[1,0,0,1,1,1,1,1,0,1,1]),
   (4,[0,0,1,1,0,0,0,1,1]),
   (5,[0,0,0,1,1,1,0,1]),
   (81999,[1,1]),
   (82000,[0,1]) ] 

2

R, 111

Probably a lot of room to improve this at the moment

i=scan();b=2:i;R=i%%b;I=rep(i,i-1);while(any(I<-I%/%b))R=cbind(I%%b,R);for(x in b)if(all(R[x-1,]<2))cat(x,'\n')

Runs with warnings

> i=scan();b=2:i;R=i%%b;I=rep(i,i-1);while(any(I<-I%/%b))R=cbind(I%%b,R);for(x in b)if(all(R[x-1,]<2))cat(x,'\n')
1: 82000
2: 
Read 1 item
There were 17 warnings (use warnings() to see them)
2 
3 
4 
5 
81999 
82000
>

@AlexA. Warnings caused by coercing the I%/%b to a logical in the any() clause. `
MickyT

2

Java, 181 155.25(207 * .75) 151.5(202 * .75) bytes

class I{public static void main(String[]a){a:for(long b=new java.util.Scanner(System.in).nextLong(),c,d=1;d++<b;){String e="";for(c=b;c>0;e=c%d+e,c/=d)if(c%d>1)continue a;System.out.println(d+" "+e);}}}

Expanded with explanation:

class I {
    public static void main(String[]a){
        a:for(long b=new java.util.Scanner(System.in).nextLong(),c,d=1; //b = input(), d = base
              d++<b;) {                                           //For all bases in range(2,b+1)
            String e="";
            for(c = b;c > 0; e = c % d + e,c /= d)                //Test all digits of base-d of b
                           //e = c % d + e                        //Append digits to string
                if (c % d > 1)                                    //Reject base if the digit is greater than 1
                    continue a;
            System.out.println(d+" "+e);                          //Print base and digits.
        }
    }
}

Original (without bonus):

class I{public static void main(String[]a){long b=new java.util.Scanner(System.in).nextLong(),c,d=1;a:for(;d++<b;){c=b;while(c>0){if(c%d>1)continue a;c/=d;}System.out.println(d);}}}

3.75 bytes thanks to Ypnypn :)


2

R, 94 83 79

n=scan();cat((2:n)[!sapply(2:n,function(x){while(n&n%%x<2)n=n%/%x;n})],sep="\n")

Usage:

> n=scan();cat((2:n)[!sapply(2:n,function(x){while(n&n%%x<2)n=n%/%x;n})],sep="\n")
1: 82000
2: 
Read 1 item
2
3
4
5
81999
82000
> n=scan();cat((2:n)[!sapply(2:n,function(x){while(n&n%%x<2)n=n%/%x;n})],sep="\n")
1: 1234321
2: 
Read 1 item
2
1111
1234320
1234321

The core of the function is !sapply(2:n,function(x){while(n&n%%x<2)n=n%/%x;n}) which, for each base x from 2 to n, keep the quotient n/x as long as the remainder is either 0 and 1. It then outputs the result (which is 0 if all remainders were either 1 or 0) and negates it (0 negates to TRUE, everything else negates to FALSE). Thanks to function scope, there is no need to make a dummy variable for n. The resulting vector of booleans is then used to index 2:n and therefore outputs only the bases for which it worked.


1

TI-Basic, 45 bytes

Input N
For(B,2,N
If prod(seq(BfPart(iPart(N/B^X)/B),X,0,log(N)/log(B))<2
Disp B
End

Explanation

  • Input N
  • For every B from 2 to N
    • If N is just 0 and 1 in base B
      • Display B
  • End loop

The complicated part

The second line works as follows:

  • For every X from 0 to logB N
  • B × fPart(iPart(N / BX) / B) is the Nth digit in base B, counting backwards
  • Consider this as a list
  • For each element, if the digit is less than 2, yield 1 (true), else 0 (false)
  • Take the product: 1 iff all elements are 1

Note

The program runs significantly faster if a closing parenthesis ) is placed at the end of the second line. See here for more about this.


1

TI-BASIC, 31 29

For(B,2,Ans
If 2>round(Bmax(fPart(Ans/B^randIntNoRep(1,32
Disp B
End

This is probably optimal for TI-BASIC.

Explanation:

randIntNoRep(1,32) returns a random permutation of the numbers from 1 to 32 (All we need is those numbers in some order; TI-BASIC doesn't have anything like APL's iota command). 32 elements is enough because the smallest possible base is 2 and the largest number is 2^32-1. B^randIntNoRep(1,31) raises that list to the Bth power, which results in the list containing all of B^1,B^2,...,B^32 (in some order).

Then the input (in the Answer variable, which is input to in the form [number]:[program name]) is divided by that number. If your input is 42 and the base is 2, the result will be the list 21,10.5,5.25,...,42/32,42/64,[lots of numbers less than 1/2], again in some order.

Taking the fractional part and multiplying the number by your base gives the digit at that position in the base-b representation. If all digits are less than 2, then the greatest digit will be less than 2.

As Ypnypn stated, a closing parenthesis on the For statement speeds this up due to a parser bug.

31->31: Saved a byte but fixed rounding errors which added the byte again.

31->29: Saved two bytes by using RandIntNoRep() instead of cumSum(binomcdf()).


Does TI-BASIC have a sequence function?
Mr. Llama

Yes, the command is seq(expression, variable, start, end[, step]). If no step is given, it defaults to 1. However, cumSum(binomcdf(31,0 is 8 bytes whereas seq(X,X,1,32 is 9 bytes.
lirtosiast

Ah, that explains it. I'm not familiar with scoring works in TI-Basic.
Mr. Llama

1

Jelly, 9 bytes

³bṀỊµÐfḊY

Try it online!

Done alongside caird coinheringaahing in chat.

How it works

³bṀỊµÐfḊY    Full program.

     Ðf      Filter the implicitly generated range [1, input].
    µ        Starts a new monadic chain.
³b           Convert the input to the base of the current number, as a list.
  Ṁ          Maximum.
   Ị         Insignificant. Checks if abs(Z) ≤ 1.
       Ḋ     Dequeue; Removes the first element of the list (to drop base 1).
        Y    Join by newlines.

0

Javascript, ES6, 118*.75 = 88.5 110*.75 = 82.5

f=x=>{res={};for(b=2;b<=x;++b)if(!+(res[b]=(c=x=>x%b<2?x?c(x/b|0)+""+x%b:"":"*")(x)))delete res[b];return res}

Previous version:

f=x=>{res={};for(q=2;q<=x;++q)if(!+(res[q]=(c=(x,b)=>x%b<2?x?c(x/b|0,b)+""+x%b:"":"*")(x,q)))delete res[q];return res}

Check:

f(82000)
Object { 2: "10100000001010000", 3: "11011111001", 4: "110001100", 5: "10111000", 81999: "11", 82000: "10" }

Here you have no input and no output.
edc65

0

JavaScript (ES6) 65

68 bytes for a function with a parameter and console output.

f=n=>{s=n=>n%b>1||b<n&&s(n/b|0);for(b=1;b++<n;)s(n)||console.log(b)}

65 bytes with I/O via popup

n=prompt(s=n=>n%b>1||b<n&&s(n/b|0));for(b=1;b++<n;)s(n)||alert(b)

Claiming the bonus: 88*0.75 => 66

n=prompt(s=n=>n%b>1?9:(b<=n?s(n/b|0):'')+n%b);for(b=1;b++<n;)s(n)<'9'&&alert(b+' '+s(n))

0

Mathematica, 76 * 0.75 = 57

n=Input[];G=#~IntegerDigits~b&;If[Max@G@n<2,Print[b," ",Row@G@n]]~Do~{b,2,n}

Initially forgot about the input requirements... Fortunately, those didn't add too much extra.



0

Perl 5, 63 bytes

map{$t=$n;1while($f=$t%$_<2)&&($t=int$t/$_);say if$f}2..($n=<>)

Try it online!

No bonus on this because it scores very slightly better than my version with the bonus:

Perl 5, 85 bytes * 0.75 = 63.75

map{my$r;$t=$n;$r=$/.$r while($/=$t%$_)<2&&($t=int$t/$_);say"$_ 1$r"if$/<2}2..($n=<>)

Try it online!

당사 사이트를 사용함과 동시에 당사의 쿠키 정책개인정보 보호정책을 읽고 이해하였음을 인정하는 것으로 간주합니다.
Licensed under cc by-sa 3.0 with attribution required.