Count from 1 to n in Negabinary and Negaquaternary

7

Instead of normal binary, you must count from 1 to an input in negabinary concatenated to negaquaternary.

Here's an example, put in the form of an array:

[11, 1102, 1113, 100130, 101131, 11010132, 11011133, 11000120, 11001121, 11110122, 11111123, 11100110, 11101111, 10010112, 10011113, 10000100, 10001101, 10110102, 10111103, 10100230, 10101231, 1101010232, 1101011233, 1101000220, 1101001221, 1101110222, 1101111223, 1101100210, 1101101211, 1100010212, 1100011213, 1100000200, 1100001201, 1100110202, 1100111203, 1100100330, 1100101331, 1111010332, 1111011333, 1111000320, 1111001321, 1111110322, 1111111323, 1111100310, 1111101311, 1110010312, 1110011313, 1110000300, 1110001301, 1110110302, 1110111303, 111010013030, 111010113031, 100101013032, 100101113033, 100100013020, 100100113021, 100111013022, 100111113023, 100110013010, 100110113011, 100001013012, 100001113013, 100000013000, 100000113001, 100011013002, 100011113003, 100010013130, 100010113131, 101101013132, 101101113133, 101100013120, 101100113121, 101111013122, 101111113123, 101110013110, 101110113111, 101001013112, 101001113113, 101000013100, 101000113101, 101011013102, 101011113103, 101010013230, 101010113231, 11010101013232, 11010101113233, 11010100013220, 11010100113221, 11010111013222, 11010111113223, 11010110013210, 11010110113211, 11010001013212, 11010001113213, 11010000013200, 11010000113201, 11010011013202, 11010011113203, 11010010013330]

Your program must produce those numbers in any of these forms:

  • spaced apart
  • in an array
  • on new lines

How do I get those numbers?

Integer n will be used as an example of input.

for i := 1 to n
    print "i in base -2" + "i in base -4"

Rules:

  • You can submit a function or a whole program
  • You can go from 0 or 1 to the input
  • Builtins are allowed

phase

Posted 2015-07-19T01:31:02.737

Reputation: 2 540

Answers

3

O, 18 bytes

j){n.2_bo4_bo' o}d

Just my own take on this. Prints numbers with a space between them.

Try it online

Explanaiton:

j)                 Increment input by 1
  {             }d Do this (input+1) times
   n.2_bo          Convert n to base -2 and output
         4_bo      Convert n to base -4 and output
             ' o   Output a space

phase

Posted 2015-07-19T01:31:02.737

Reputation: 2 540

5

Julia, 124 81 73 bytes

n->(a=2863311530;b=3435973836;[base(2,(i+a)$a)*base(4,(i+b)$b)for i=1:n])

This creates an unnamed function that accepts an integer and returns an array. Each element of the array is a successive number between 1 and n in negabinary concatenated to itself in negaquaternary.

The negabinary representation of an integer can be computed using Schroppel's shortcut:

base(2, (i + 2863311530) $ 2863311530)

For an integer i, this is i + 2863311530, XOR 2863311530, encoded in base 2. The negaquaternary shortcut is similar:

base(4, (i + 3435973836) $ 3435973836)

Alex A.

Posted 2015-07-19T01:31:02.737

Reputation: 23 761

as per this meta ruling, you can define globals with the constants outside: http://meta.codegolf.stackexchange.com/questions/5532/when-submitting-a-function-can-global-variables-be-declared-outside-the-functio

– Maltysen – 2015-07-19T03:20:17.273

@Maltysen: Oh nice, I had forgotten about that. Thanks! – Alex A. – 2015-07-19T03:27:38.370

I don't think you can define globals unless the function is named. – feersum – 2015-07-19T14:57:49.320

@feersum: Okay, no problem. Made them local at the cost of 2 bytes. – Alex A. – 2015-07-19T17:53:09.607

4

Python - 83 bytes

f=lambda i,k:i and f(-i/k,k)+`-i%k`or''
i=0;exec"i-=1;print f(i,2)+f(i,4);"*input()

f is a function that converts (the negation of) an integer i to a negative base 2 ≤ k ≤ 10. Input is taken from stdin.


Sample Usage

$ echo 30 | python nega2+4.py
11
1102
1113
100130
101131
11010132
11011133
11000120
11001121
11110122
11111123
11100110
11101111
10010112
10011113
10000100
10001101
10110102
10111103
10100230
10101231
1101010232
1101011233
1101000220
1101001221
1101110222
1101111223
1101100210
1101101211
1100010212

primo

Posted 2015-07-19T01:31:02.737

Reputation: 30 891

1I love your for loop workaround :D – Beta Decay – 2015-07-20T05:21:55.927

2

Pyth - 31 30 29 27 bytes

Uses Forumla by D. Librik (Szudzik) (look at negabinary and quaternary sections). Can probably be golfed a bit.

VQjksjVmx+NKi*8d16K"AC",2 4

Prints from zero on separate lines.

VQ                   For i in 0-Q
 jk                  Join by empty string
  s                  Sum of two arrays
   jV                Vectorized base conversion
    m       "AC"     Map of A and C
     x               Bitwise xor
      +NK            Sum of loop var and constant inline assigned
       i  16         To base 10 from 16
        *8           String repetition times 8
         d           Map var
      K              Xnor of constant
   ,2 4              Bases 2 and 4

Try it here online.

Maltysen

Posted 2015-07-19T01:31:02.737

Reputation: 25 023

@AlexA. "You can go from 0 or 1 to the input" – Maltysen – 2015-07-19T03:19:03.320

Wow, clearly I'm oblivious. Sorry, false alarm! – Alex A. – 2015-07-19T03:22:57.267

2

CJam, 34 33 32 bytes

I think this can be golfed further. Counts from 0.

li),{_[CA]{:Ba8*Gb_@+^B8-b\}/N}/

Try it online.

Andrea Biondo

Posted 2015-07-19T01:31:02.737

Reputation: 1 452