# c# Efficient solution: Sequence generator for a pattern

This is an interview question to generate the below pattern of numbers in a sequence, which is: 12.34, 23.45, 45.67, 78.910, 1112.1314, 1617.1819, 2223.2425, 2930.3132, 3738.3940, 4647.4849, 5657.5859, 6768.6970, 7980.8182, 9293.9495, 106107.108109, 121122.123124 We can use starting seed number as 12 and have to generate all the 16 numbers programmatically without using strings. We can use only arrays but no other built-in collection classes or data structures.

public static void GenerateSequence()
{
int j = 1;
ArrayList sequenceList = new ArrayList();

for (int count = 0; count < 16; count++)
{
j = j + count;
int quotent = getValue(j, j + 1);
int reminder = getValue(j + 2, j + 3);
int length = getLength(reminder);
double result = Convert.ToUInt32(quotent) * getLength(reminder) + reminder;
}
Console.WriteLine(sequenceList.ToString());
}

static int getValue(int a, int b)
{
return a * getLength(b) + b;
}

static int getLength(int value)
{
int length = (int)(Math.Log10(value) + 1);
int formattedValue = 10;

for (int i = 1; i < length; i++)
formattedValue = formattedValue * 10;

return formattedValue;
}


I've arrived with above solution but this looks complex and less efficient. Looking for an alternate approach with better time/space complexity.

• 12.34, 23.45, 45.67, 78.910 - how is this even a consistent sequence? Wouldn't this go 12.34, 23.45, 34.56, 45.67? – Reinderien Jun 21 '20 at 23:49
• @Reinderien the sequence is adding 1 to each digit, then 2, then 3... not adding 1 at each step. – cliesens Jun 22 '20 at 0:01
• That seems underspecified. It isn't just adding to each digit - it's substituting each digit with all of the digits of the resulting sum. You should spell this out in the question. – Reinderien Jun 22 '20 at 0:04
• If it is an interview question then they are interested about your solution (skills) not others. – Peter Csala Jun 22 '20 at 6:54

public static void GenerateSequence() {
double tenPower(double n) => Math.Pow(10, Math.Floor(Math.Log10(n)+1));
double build(double n) => n * tenPower(n + 1) + n + 1;
double merge(double n1, double n2) => n1 + n2 / tenPower(n2);

var n = 1;
var ans = new double[16];
for (int j1 = 0; j1 < 16; ++j1) {
n += j1;

var whole = build(n);
var frac = build(n + 2);
ans[j1] = merge(whole, frac);
}

foreach (var a in ans)
Console.WriteLine(a);
}

• "ten power" might be more aptly described as order of magnitude. – Flater Oct 8 '20 at 11:45
• A code review should include, you know, a review. Why did you change the things you changed? – Flater Oct 8 '20 at 11:46