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 Mathew-John number. 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.
Output Format:
For valid input, print Mth number from formed sequence between T1 and T2(inclusive) or no number is present at this index. For invalid input, print invalid input.
Constraints:
0 < T1 < T2 < 1000000
Example
Input:
90 150 2
Output:
120
Solution: In C
#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");
return 0;
}
Solution: In Java
import java.util.*;
class JumbleWithNumbers {
public static void main(String[] args) {
int lower = 0;
int upper = 0;
int n = 0;
Scanner sc = new Scanner(System.in);
String[] s=sc.nextLine().trim().split(" ");
try {
boolean flag = false;
try {
lower = Integer.parseInt(s[0].trim());
} catch (Exception e) {
flag = true;
}
try {
upper = Integer.parseInt(s[1].trim());
} catch (Exception e) {
flag = true;
}
try {
n = Integer.parseInt(s[2].trim());
} catch (Exception e) {
flag = true;
}
if (flag) {
throw new Exception();
}
if (lower > upper) {
throw new Exception();
}
int i = 1;
flag = false;
int count = 0;
while (true) {
int nu = i * (2 * i - 1);
if (nu < lower) {
i++;
continue;
}
if (getMathew(lower,upper,nu)) {
count++;
if (count == n) {
System.out.println(nu);
break;
}
}
if (nu > upper) {
flag = true;
break;
}
i++;
}
if (flag) {
System.out.println("No number is present at this index");
}
} catch (Exception e) {
System.out.println("Invalid Input");
}
}
static boolean getMathew(int lower,int upper,int num) {
int i = 1;
while (true) {
int nu = i * (i + 1);
nu /= 2;
if (nu < lower) {
i++;
continue;
}
if (nu == num) {
return true;
}
if (nu > upper) {
break;
}
i++;
}
return false;
}
}
Solution: In Python 3
t1, t2, m = [int(x) for x in input().split()]
k = 0
th = []
if t1 > 0 and t2 > 0 and m > 0:
for i in range(1,int(t2/2)):
a = i*((2*i)-1)
for j in range(1,int(t2/2)):
b=j*(j+1)/2;
if a==b and a >= t1 and a <= t2:
th.append(a);
k = k + 1
if k < m:
print("No number is present at this index")
else:
print(th[m-1]);
else:
print("Invalid Input");
Warning: Undefined array key "icon" in /home/u997094728/domains/codewindow.in/public_html/wp-content/plugins/elementor/includes/widgets/icon-box.php on line 695
Warning: Undefined array key "icon" in /home/u997094728/domains/codewindow.in/public_html/wp-content/plugins/elementor/includes/widgets/icon-box.php on line 708
Warning: Undefined array key "icon" in /home/u997094728/domains/codewindow.in/public_html/wp-content/plugins/elementor/includes/widgets/icon-box.php on line 695
Warning: Undefined array key "icon" in /home/u997094728/domains/codewindow.in/public_html/wp-content/plugins/elementor/includes/widgets/icon-box.php on line 708
Also Checkout