Tricky Sequence Question

Given: Sequence 0123456789101112131415161718192021222324…infinity where each digit has an index for example the digit at index 7 is 7 , digit at index 14 is 1 and so on.

To find: A function that returns the digit at a given index.

1 Like

take the input as string.Covert it to char array(for languages like Java and c#). Get the character at index i.
you dont need to convert it either just use charat[index i]

That was not a questionin fact, you are not reading anything, the sequence is concatenation of integers from 0 to infinity.

Also you cannot generate such sequence, what if I ask you to print 1.000.000.000.000th digit?

First of all realize how the sequence is composed:

  • first part is 0123456789
  • next part is 10111213…
  • next part is 100101102103…
  • and so on

for each such part you know the length of that part -> if you are given index i, find which part you are dealing with and then subtract lengths of skipped parts. Now you have new index ni in some part and I think it’s easy to find the answer now :wink:

edit: here is the link to the algorithm - http://ideone.com/VxgZXG

4 Likes

import java.io.;
import java.util.
;
public class NoatIndex
{
public static void main(String args[])
{
int no;
StringBuilder s=new StringBuilder();
System.out.println(“Enter the Limit of numbers”);
Scanner sc=new Scanner(System.in);
no=sc.nextInt();
for( int i=0;i<=no;i++)
{
s.append(i).toString();
}
System.out.println(“Enter the number at Index”);
System.out.println("The digit at the index is "+s.charAt(sc.nextInt()));
}
}

1 Like

It has the same problem as I wrote earlier - what if I want 1.000.000.000.000th digit - how long your code runs? Let us know :wink:

You also added another problem - if I want 1.000.000.000.000th digit, what should I enter for question: “Enter the Limit of numbers” ? If I know number of such numbers I’m almost done.

import java.util.Scanner;

public class digit_at_index {

	public static String index(int ind)
	{
		
		int v;
		StringBuilder st = new StringBuilder();
		
		for(v=0;v<=500;v++)
		{
			st.append(v).toString();
		}
		
		if (st.length()<=ind)
			return "index out of bound";
		System.out.println(st.charAt(ind));
		return "";
		
	}
	
	public static void main(String[] args) {
		int in;
		Scanner sc = new Scanner(System.in);
		System.out.println("Enter any index: ");
		in = sc.nextInt();
		System.out.println(index(in));
		
	}

}
1 Like

Please stop adding slow solutions again and again there are already some…

Okye… Lemme know the solution if u have better solution for the problem…

please add constraint for index, otherwise we do not know exactly if slow solutions are ok too… or even better add link to the problem statement :wink:

it’s described above :wink:

Can u code it for me rather than the procedure you explained and give 1.000.000.000.000th digit … i wanna see your program so that i can reduce the complexity of my program…

Please can u say the equal integer number for 1.000.000.000.000

In number systems, I think there is no real number as 1.000.000.000., then what is the need of asking such a question?

I was not talking about decimal number but integer number 10^12 (used dots to be easier to read).

yes, my code will work for that integer number…, it prints out a message “index out of bound”.

just ., i am pasting myn better solution…, if u know much better…, then lemme know…, such that i can learn from you…

and you call this as working code ? Your code prints that also for digits in numbers greater than 500, f.e. 1394, even @rajasekharcool’s code is working better (if I specify 1394 and 1394 as input)…

no, my code is giving ans as 3 when we take input as 501

My mistake (I’ve edited the previous comment), I meant for digit in 501st number - 1394, but that’s not making your algorithm quicker :wink: