8
\$\begingroup\$

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
\$\endgroup\$
0

7 Answers 7

5
\$\begingroup\$

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
\$\endgroup\$
1
  • 2
    \$\begingroup\$ I love your for loop workaround :D \$\endgroup\$
    – Beta Decay
    Jul 20, 2015 at 5:21
4
\$\begingroup\$

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)
\$\endgroup\$
4
  • \$\begingroup\$ as per this meta ruling, you can define globals with the constants outside: meta.codegolf.stackexchange.com/questions/5532/… \$\endgroup\$
    – Maltysen
    Jul 19, 2015 at 3:20
  • \$\begingroup\$ @Maltysen: Oh nice, I had forgotten about that. Thanks! \$\endgroup\$
    – Alex A.
    Jul 19, 2015 at 3:27
  • \$\begingroup\$ I don't think you can define globals unless the function is named. \$\endgroup\$
    – feersum
    Jul 19, 2015 at 14:57
  • \$\begingroup\$ @feersum: Okay, no problem. Made them local at the cost of 2 bytes. \$\endgroup\$
    – Alex A.
    Jul 19, 2015 at 17:53
3
\$\begingroup\$

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
\$\endgroup\$
2
\$\begingroup\$

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.

\$\endgroup\$
2
  • \$\begingroup\$ @AlexA. "You can go from 0 or 1 to the input" \$\endgroup\$
    – Maltysen
    Jul 19, 2015 at 3:19
  • \$\begingroup\$ Wow, clearly I'm oblivious. Sorry, false alarm! \$\endgroup\$
    – Alex A.
    Jul 19, 2015 at 3:22
2
\$\begingroup\$

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.

\$\endgroup\$
1
\$\begingroup\$

Jelly, 10 bytes

bF¥€Ø½ḤN¤Ḍ

Try it online!

How it works

bF¥€Ø½ḤN¤Ḍ - Main link. Takes an integer as left argument
    ؽḤN¤  - Yield the list [-2, -4]:
        ¤  -   Group the previous commands into a constant:
    ؽ     -     Start with [1, 2] (builtin constant)
      Ḥ    -     Double     [2, 4]
       N   -     Negate     [-2, -4]
  ¥€       - Generate the range 1...n and do the following to each:
b          -   Convert to bases -2 and -4
           -   For example, 5b[-2, -4] => [[1, 0, 1], [1, 3, 1]]
 F         -   Flatten the lists          [1, 0, 1, 1, 3, 1]
         Ḍ -   Convert to a number        101131
\$\endgroup\$
0
\$\begingroup\$

Japt, 12 bytes

Output is an array of digit arrays.

õìJÑ)ícUõì4n

Try it

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.