There is an infinite sequence of numbers as follows :
1,2,2,3,3,3,4,4,4,4,5,5,5,5,5......(the 'i' number appears i times) . If you are given a number 'k', write a program that will return the kth number in the sequence given above.
e.g if k=4 then answer will be 3.
negative regexp
-
if you want to grep anything except foo, use grep -v or negative lookahead
echo -e 'foo\nbar\nboz'|grep -P '^(?!.*foo).*$' bar boz it is just so
beautiful ...
1 month ago
4 comments:
if kth element is to be found then
for (i=1, i<=k i++)
{
for (j=1 j<=i j++)
{
VALUE = i
k=k-1
IF ( k=0)
break;
}
}
PRINT VALUE;
there mightr be other soln. with in While loop or something like that.
ONE MORE SOLN.:-
AS N*(N+1)/2 = sum up tp N digits
if kth element is to be found then
N*(N+1)/2 = K
N2(Npower2)+ N - 2K >=0 then N is the print value.
so
for (i=1, i<=k i++)
{
TEMP = N2(Npower2)+ N - 2K
IF (TEMP >= 0)
break;
}
PRINT i
AAh Ah!!
Tech/Pogramming blog..gr8 going mate..
count=0;
for(i=1,i<=k&&count<=k,i++)
{
temp=0;
for(j=i,j>0,j--)
{
temp++;
}
count=count+temp;
}
print temp; //shud give u the value at k'th position
not sure if it works..never tried it on comp..jus on paper.lemme know if der is something wrong in it.
@Jai: Its fine Jai... :)
Post a Comment