Jumble With Numbers
Problem
In NASA, two researchers, Mathew and John, started their work on a new planet, but while practicing research they faced a mathematical difficulty. In order to save the time they divided their work.
So scientist Mathew worked on a piece and invented a number computed with the following formula:A Mathew number is computed as follows using the formula:
H(n) = n(2n-1)
And scientist John invented another number which is built by the following formula which is called John number.
T(n) = n(n+1)/2
Now Mathew and John are jumbled while combining their work. Now help them combine their research work by finding out number in a given range that satisfies both properties. Using the above formula, the first few Mathew-John numbers are:
1 6 15 28 …
Input Format:
Input consists of 3 integers T1,T2,M separated by space . T1 and T2 are upper and lower limits of the range. The range is inclusive of both T1 and T2. Find Mth number in range [T1,T2] which is actually a Mathew-John number.
Line 1 | T1 T2 M,where T1 is upper limit of the range, T2 is lower limit of the range and M ,where Mth element of the series is required |
Constraints:
0 < T1 < T2 < 1000000
Output Format:
Print Mth number from formed sequence between T1 and T2(inclusive).
Line 1 | For Valid Input,print Print Mth number from formed sequence between T1 and T2 Or No number is present at this index For Invalid Input,print Invalid Input |
Sample Input and Output:
SNo. | Input | Output |
---|---|---|
1 | 90 150 2 | 120 |
2 | 20 80 6 | No number is present at this index |
3 | -5 3 a | Invalid Input |
Program:
#include <stdio.h>
int main() {
int t1,t2,m,i,j,c=0,a,b,th[100],k=0;
scanf("%d %d %d",&t1,&t2,&m);
if(t1>0&&t2>0&&m>0)
{
for(i=1;i<=t2/2;i++)
{
a=i*((2*i)-1);
for(j=1;j<=t2/2;j++)
{
b=j*(j+1)/2;
if((a==b)&&(a>=t1&&a<=t2))
th[k++]=a;
}
}
if(k<m)
printf("No number is present at this index");
else
printf("%d",th[m-1]);
}
else
printf("Invalid Input");
else
printf("Invalid Input");
return 0;
}
Output:
90 150 2
120
You can also run this in online IDE: https://ide.geeksforgeeks.org/ZndEODl1gU
Your comments and feedbacks are welcomed! If you have any doubts you can leave it on the comment! Cheers!!
Related Links: Test vita
Related Links: Test vita
def ishappy(N):
ReplyDeletes=0
while(N>0):
s+=(N%10)*(N%10)
N=N//10
if(s==1):
return True
if(s==4):
return False
return ishappy(s)
def isprime(N):
for i in range(2,N/2):
if(N%i==0):
return False
else:
return True
def happyandprime(X,Y,N):
count=0
c=10
for i in range(X+1,Y):
if(isprime(i) and ishappy(i)):
count+=1
if(count==N):
c=10
print i
break
if(c==0):
print("No number is present at this index")
try:
X=int(input())
Y=int(input())
N=int(input())
happyandprime(X,Y,N)
except:
print("Invalid Input")
exit(0)