Given two integer numbers M and n, write a program to print the integers from M to N in python

Input

The first line of input contains T, denoting the number of testcases.

Then T testcase follow. First line of each testcase contains two integer m and n separated by space.

Output:

For each testcase in new line, print lesser, greater or equal for m!=n, or m=n respectively.

If inputs not in constraints, print Input not in constraints

Constraints:

1 <= T <= 10

-100 <= m <= 100

-100 <= n <= 100

Example:

Sample InputSample Output
34 84 4

-2 -5

lesserequal

greater

Code:

import java.util.*; public class Arjun_Pinpoint { public static void main(String args[]) { Scanner scan=new Scanner(System.in); int T=scan.nextInt(); if(T<1 || T>10) { System.out.println("Input not in constraints"); System.exit(0); } for(int i=0;i<T;i++) { int m=scan.nextInt(); int n=scan.nextInt(); if(m<-100 || m>100) { System.out.println("Input not in constraints"); System.exit(0); } if(n<-100 || n>100) { System.out.println("Input not in constraints"); System.exit(0); } if(m>n) System.out.println("greater"); else if(m<n) System.out.println("lesser"); else System.out.println("equal"); } } }

A positive integer greater than 1 which has no other factors except 1 and the number itself is called a prime number.

2, 3, 5, 7 etc. are prime numbers as they do not have any other factors. But 6 is not prime (it is composite) since, 2 x 3 = 6.

Source Code

# Python program to display all the prime numbers within an interval lower = 900 upper = 1000 print("Prime numbers between", lower, "and", upper, "are:") for num in range(lower, upper + 1): # all prime numbers are greater than 1 if num > 1: for i in range(2, num): if (num % i) == 0: break else: print(num)

Output

Prime numbers between 900 and 1000 are: 907 911 919 929 937 941 947 953 967 971 977 983 991 997

Here, we store the interval as lower for lower interval and upper for upper interval, and find prime numbers in that range. Visit this page to learn how to check whether a number is prime or not.

Convert a number m to n with minimum operations. The operations allowed are : 
 

  1. Multiply by 2, i.e., do m = 2 * m
  2. Subtract 1, i.e., do m = m – 1

Print -1 if it is not possible to convert.
Examples : 
 



Input : m = 3, n = 11 Output : 3 1st operation: *2 = 3*2 = 6 2nd operation: *2 = 6*2 = 12 3rd operation: -1 = 12-1 = 11 Input : m = 15, n = 20 Output : 6 1st operation: -1 '5' times = 15 + (-1*5) = 10 2nd operation: *2 = 10*2 = 20 Input : m = 0, n = 8 Output : -1 Using the given set of operations 'm' cannot be converted to 'n' Input : m = 12, n = 8 Output : 4

The idea is based on below facts. 1) If m is less than 0 and n is greater than 0, then not possible. 2) If m is greater than n, then we can reach n using subtractions only. 3) Else (m is less than n), we must do m*2 operations. Following two cases arise. ……a) If n is odd, we must do a -1 operation to reach it. ……b) If n is even, we must do a *2 operation to reach it.

Algorithm: 


 

int convert(m,n) if (m == n) return 0; // not possible if (m <= 0 && n > 0) return -1; // m is greater than n if (m > n) return m-n; // n is odd if (n % 2 == 1) // perform '-1' return 1 + convert(m, n+1); // n is even else // perform '*2' return 1 + convert(m, n/2);

Note: The list of operations so generated should be applied in reverse order. For example: 

m = 3, n = 11 convert(3,11) | --> n is odd: operation '-1' convert(3,12) | --> n is even: operation '*2' convert(3,6) | --> n is even: operation '*2' convert(3,3) | --> m == n return Therefore, the sequence of operations is '*2', '*2', '-1'.

int convert(int m, int n)

        return 1 + convert(m, n + 1);

        return 1 + convert(m, n / 2);

    cout << "Minimum number of operations : "

    static int convert(int m, int n)

            return 1 + convert(m, n + 1);

            return 1 + convert(m, n / 2);

    public static void main (String[] args)

        System.out.println("Minimum number of " +

        return 1 + convert(m, n + 1)

        return 1 + convert(m, n / 2)

print("Minimum number of operations :",

    static int convert(int m, int n)

            return 1 + convert(m, n + 1);

            return 1 + convert(m, n / 2);

    public static void Main ()

        Console.Write("Minimum number of " +

        return 1 + convert($m, $n + 1);

        return 1 + convert($m, $n / 2);

    echo "Minimum number of ".

        return 1 + convert(m, n + 1);

        return 1 + convert(m, n / 2);

document.write("Minimum number of " +

Output : 
 

Minimum number of operations : 3

References : 
http://tech.queryhome.com/112705/convert-number-with-minimum-operations-operations-allowed
This article is contributed by Ayush Jauhari. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to . See your article appearing on the GeeksforGeeks main page and help other Geeks.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.


Article Tags :