Andries Heylen
Andries Heylen

Reputation: 383

Algorithm to produce number series

I am not sure how to attack this problem... I tried many things, and it seems to be that it shouldn't be so difficult, but not getting there...

Is it possible to create a function "series ( _x )", that produces this :

example of wanted result

The function for example should be myfunction( 11 ) => 211

Upvotes: 2

Views: 765

Answers (4)

Dmitrii Bychenko
Dmitrii Bychenko

Reputation: 186803

According to A000055

We should perform 3 steps:

  1. Convert value + 1 to base 2
  2. Remove 1st 1
  3. Add 1 to the rest digits

For instance, for 11 we have

  1. Converting 11 + 1 == 12 to binary: 1100
  2. Removing 1st 1: 100
  3. Adding 1 to the rest digits: 211

So 11 has 211 representation.

C# code:

private static String MyCode(int value) =>
  string.Concat(Convert
    .ToString(value + 1, 2)       // To Binary
    .Skip(1)                      // Skip (Remove) 1st 1
    .Select(c => (char)(c + 1))); // Add 1 to the rest digits

Demo:

var result = Enumerable
  .Range(1, 22)
  .Select(value => $"{MyCode(value),4} : {value,2}");

Console.Write(string.Join(Emvironment.NewLine, result));

Outcome:

   1 :  1
   2 :  2
  11 :  3
  12 :  4
  21 :  5
  22 :  6
 111 :  7
 112 :  8
 121 :  9
 122 : 10
 211 : 11
 212 : 12
 221 : 13
 222 : 14
1111 : 15
1112 : 16
1121 : 17
1122 : 18
1211 : 19
1212 : 20
1221 : 21
1222 : 22

Upvotes: 1

alexanderhurst
alexanderhurst

Reputation: 466

This hint should help you... It isn't quite binary, but it is close. Let me know if you need any further help

   0    ->    -     ->    -
   1    ->    -     ->    -
  10    ->    0     ->    1
  11    ->    1     ->    2
 100    ->   00     ->   11
 101    ->   01     ->   12
 110    ->   10     ->   21
 111    ->   11     ->   22
1000    ->  000     ->  111
1001    ->  001     ->  112
1010    ->  010     ->  121
1011    ->  011     ->  122
1100    ->  100     ->  211
1101    ->  101     ->  212
1110    ->  110     ->  221
1111    ->  111     ->  222

Edit: I didn't like the way I ordered the columns, so I swapped 2 and 3

Python approach

First thing that we need to do is produce binary strings
in Python this can be done with bin(number)
However this will return a number in the form 0b101
We can easily strip away the 0b from the beginning though by telling python that we dont want the first two characters, but we want all the rest of them. The code for that is: bin(number)[2:] left side of the : says start two spaces in, and since the right side is blank go to the end

Now we have the binary numbers, but we need to strip away the first number. Luckily we already know how to strip away leading characters so change that line to bin(number)[3:].

All that is left to do now is add one to every position in the number. To do that lets make a new string and add each character from our other string to it after incrementing it by one.

# we already had this
binary = bin(user_in + 1)[3:]

new = ""
for char in binary:
    # add to the string the character + 1
    new += str(int(char) + 1)

And we are done. That snippet will convert from decimal to whatever this system is. One thing you might notice is that this solution will be offset by one (2 will be 1, 3 will be 2) we can fix this by simply adding one to user input before we begin.

final code with some convenience (a while loop and print statement)

while True:
    user_in = int(input("enter number: "))
    binary = bin(user_in + 1)[3:]
    new = ""
    for char in binary:
        new += str(int(char) + 1)

    print(user_in, "\t->\t", binary, "\t->\t", new)

Upvotes: 2

nice_dev
nice_dev

Reputation: 17805

The terms become suffix for the next terms. See below picture for more clarity. The boxes with same color gets repeated. So, we could just keep prepending 1 and 2 for previous results.

enter image description here

Code(In java):

public class Solution {
    public static void main(String[] args) {
        List<String> ans = solve(10);
        for(int i=0;i<ans.size();++i) System.out.println(ans.get(i));
    }

    private static List<String> solve(int terms){
        List<String> ans = new ArrayList<>();
        String[] digits = new String[]{"1","2"};
        ans.add("1");
        if(terms == 1) return ans;
        ans.add("2");
        if(terms == 2) return ans;

        List<String> final_result = new ArrayList<>();
        final_result.addAll(ans);
        terms -= 2;//since 2 numbers are already added

        while(terms > 0){           
            List<String> temp = new ArrayList<>();
            for(String s : digits){
                 for(int j=0;j<ans.size() && terms > 0;++j){
                     temp.add(s + ans.get(j));
                     terms--;
                 }
            }
            ans = temp;
            final_result.addAll(ans);
        }       

        return final_result;
    }
}

Upvotes: 2

Andrew Morton
Andrew Morton

Reputation: 25027

In VB.NET, showing both the counting in base-3 and OEIS formula ways, with no attempts at optimisation:

Module Module1

    Function OEIS_A007931(n As Integer) As Integer
        ' From https://oeis.org/A007931

        Dim m = Math.Floor(Math.Log(n + 1) / Math.Log(2))
        Dim x = 0
        For j = 0 To m - 1
            Dim b = Math.Floor((n + 1 - 2 ^ m) / (2 ^ j))
            x += CInt((1 + b Mod 2) * 10 ^ j)
        Next

        Return x

    End Function

    Function ToBase3(n As Integer) As String
        Dim s = ""
        While n > 0
            s = (n Mod 3).ToString() & s
            n \= 3
        End While

        Return s

    End Function

    Function SkipZeros(n As Integer) As String
        Dim i = 0
        Dim num = 1
        Dim s = ""

        While i < n
            s = ToBase3(num)
            If s.IndexOf("0"c) = -1 Then
                i += 1
            End If

            num += 1

        End While

        Return s

    End Function

    Sub Main()
        Console.WriteLine("A007931  Base3   ITERATION")
        For i = 1 To 22
            Console.WriteLine(OEIS_A007931(i).ToString().PadLeft(7) & SkipZeros(i).PadLeft(7) & i.ToString().PadLeft(11))
        Next

        Console.ReadLine()

    End Sub

End Module

Outputs:

A007931  Base3   ITERATION
      1      1          1
      2      2          2
     11     11          3
     12     12          4
     21     21          5
     22     22          6
    111    111          7
    112    112          8
    121    121          9
    122    122         10
    211    211         11
    212    212         12
    221    221         13
    222    222         14
   1111   1111         15
   1112   1112         16
   1121   1121         17
   1122   1122         18
   1211   1211         19
   1212   1212         20
   1221   1221         21
   1222   1222         22

Upvotes: 0

Related Questions