Given integers N and M, find the number of ordered pairs (a, b) such that 1≤a <b≤N and ((M mod a) mod b) = ((M mod b) mod a).
Input
The first line contains an integer T, the number of test cases. Then the test cases follow.
The only line of each test case contains two integers N, M.
output
For each test case, output in a single line the answer to the problem.
Constraints
1≤T≤1000
2≤N≤10 ^ 6
1≤M≤5⋅10 ^ 5
The sum of N over all test cases does not exceed 10 ^ 6
I have tried O (N ^ 2) approach but it gives TLE. Need a new approach or any idea
import java.util.Scanner;
class test {
public static void main(String[] args) {
try {
Scanner scn = new Scanner(System.in);
int testCase = scn.nextInt();
for (int i = 0; i < testCase; i++) {
int n = scn.nextInt();
int m = scn.nextInt();
int count = 0;
for (int j = 1; j < n; j++) {
for (int k = j + 1; k <= n; k++) {
if (((m % j) % k) == ((m % k) % j)) {
// System.out.print(j+" "+k);
// System.out.println();
count++;
}
}
}
System.out.println(count);
}
} catch (Exception e) {
return;
}
}
}
I can see some improvements. First of all, since a<b, (M mob a) mod b = M mod a
So we have to check whether M mod a = (M mod b) mod a, but this means that a divides (M - (M mod b))=((M div b) * b)
To sum up, we have to iterate over all b=1,...,n and for each of them we have to count the divisors of ((M div b) * b) which are less than b.
The improvement is the fact that ((M div b) * b) is comparable with b, and when we are looking for the divisor of ((M div b) * b) we can stop at its square root.
A possible implementation (in which I have removed the tests cases) is this one
class Test {
public static void main(String[] args) {
try {
Scanner scn = new Scanner(System.in);
int n = scn.nextInt();
int m = scn.nextInt();
int count = 0;
for (int j = 1; j < n; j++) {
for (int k = j + 1; k <= n; k++) {
if (((m % j) % k) == ((m % k) % j)) {
//System.out.print(j+" "+k);
//System.out.println();
count++;
}
}
}
System.out.println(count);
count=0;
for (int b = 1; b <= n; b++) {
//System.out.println((m/b)*b);
count += countDivisors(m, b);
}
System.out.println(count);
} catch (Exception e) {
return;
}
}
private static int countDivisors(int m, int b) {
int count=0;
int val = ((m/b)*b);
int mi = Math.min((int)Math.sqrt(val), b-1);
if(mi == 0)
return (b-1);
for(int a=1; a<=mi; a++) {
if(val % a == 0) {
count++;
int aa=val/a;
if(aa>a && aa < b) {
count++;
}
}
}
return count;
}
}
I've been having some issues trying to solve this code that my professor sent to me for studying purposes. I usually can solve the isPrime and getPrime no problem but my issue lies with the gcd class. Would love to get some input on this. Thanks :) Sorry for the formatting since i'm new to the site
import java.util.*;
public class Util3
{
public ??? getSmaller(???)
{
int smaller;
if (???)
smaller = ???;
else
smaller = ???;
return smaller;
}
public ??? gcd(??? a, ??? b)
{
int g, smaller;
smaller = ???(a, b);
g = smaller;
for (int i = ???; i >= 1; i++) { // from smaller to 1
if (a % i == 0 ??? ???) {
g = i;
???;
}
}
return g;
}
public ??? isPrime(int p)
{
if (p < 2)
return false;
int i;
for (i = ???; i <= ???; i++) // from 2 to p-1
if (??? == 0) // if p is divisible by i
break;
if (i == ???)
return ???;
else
return ???;
}
public ??? getPrime(int n)
{
boolean b;
int p;
for (p = n+1; ; p++) { // n+1, n+2, ...
b = ???(p); // check if p is a prime number
if (???)
break;
}
return p;
}
}
You can use a naive solution:
public static int gcd(int a, int b) {
int current_gcd = 1;
for(int d = 2; d <= a && d <= b; ++d) {
if (a % d == 0 && b % d == 0) {
if (d > current_gcd) {
current_gcd = d;
}
}
}
return current_gcd;
}
Or a recursive one:
public static int GCD(int a, int b) {
if (b == 0) {
return a;
}
}
return GCD(b, a % b);
}
I'm trying to add the gcd() function to the NumericFunctions class and include code in main to compute gcd(m,n).
However, I keep getting an error:
Exception in thread "main" java.lang.StackOverflowError
at NumericFunctions.gcd(NumericFunctions.java:14)
Source code:
public class NumericFunctions {
public static long factorial(int n) {
long result = 1;
for (int i = 2; i <= n; i++) {
result *= i;
}
return result;
}
public static int gcd (int n, int m) {
if ((m % n) == 0)
return n;
else
return gcd(n, m % n);
}
public static void main(String[] args) {
for (int n = 1; n <= 10; n++)
for (int m = 1; m <= 10; m++){
System.out.println(gcd(n,m));
System.out.println(" ");
System.out.println(factorial(n));
}
}
}
Check out the following corrections in gcd() method:
public static int gcd (int n, int m) {
if ((n % m) == 0)
return m; // <-- first correction
else
return gcd(m, n % m); // <-- second correction
}
i need to take 2 inputted numbers and calculate variable 1 to the power of variable 2 is without using math.pow and using a for loop. This is what i have now
Scanner in = new Scanner(System.in);
System.out.print("Please enter your base: ");
int base = in.nextInt();
System.out.print("Please enter your exponent: ");
int power = in.nextInt();
int result = mathPower(base, power);
System.out.println(base + " to the power of " + power + " is " + result + ".");
}
public static int mathPower(int a, int b)
{
int result = a;
if (b == 0) {
result = 1;
}
if (b < 0) {
a = (1 / a);
b = -b;
}
for (a = 1; a < b; a++) {
result = result * a;
return result;
}
return result;
}
}
It only seems to work if the exponent is 0, otherwise it just displays the a value. I need both positive and negative exponents. Thanks in advance!
The case with b<0 only makes sense with floating point numbers, so I changed the type of a and the return value to double.
public static double mathPower(double a, int b)
{
double result = 1;
if (b < 0) {
a = 1.0 / a;
b = -b;
}
for (int i = 0; i < b; i++) {
result = result * a;
}
return result;
}
You have three main problems:
The return statement inside the loop is breaking it in the first repetition.
You're using your a variable as the loop variable.
If you allow negative exponents then the return value should be a double.
public static double mathPower(double a, int b)
{
double result = 1.0;
if (b == 0)
{
result = 1.0;
}
if (b < 0)
{
a = (1.0 / a);
b = -b;
}
for (int i = 0; i < b; i++)
{
result = result * a;
}
return result;
}
suppose you have numb1=2 and numb2=6.
then
temp=1;
if (numb2 < 0) {
numb1 = 1 / numb1;
numb2 = -numb2;
}
for(int n = 1; n<=numb2; n++){
temp=temp*numb1;
}
public double power(double base,int pow)
{
double result = 1;
if(pow==0)
return 1;
if(base == 0)
return 0;
if(pow>0)
{
for(int i = 0;i<pow;i++)
{
result *= base;
}
}
else
{
for(int i = pow;i<0;i++)
{
result *= base;
}
result = 1/result;
}
return result;
}
I am trying to do the following program in Java where I'm writing a recursive and an iterative method to compute the sum of all odd numbers from n to m
import java.util.Scanner;
public class AssignmentQ7 {
public static int recursivesum(int n, int m){
if (n < m){
int s = n;
s += recursivesum(n+2, m);
} else{
if(m < n){
int s = m;
s += recursivesum(m+2, n);
}
}
return s;
}
public static int iterativesum(int n, int m){
if(n < m){
int s = n;
for(int i = n; i <= m; i += 2){
s += i;
return s;
}
} else
if(m < n){
int s = m;
for(int i = m; i <= n; i += 2){
s += i;
return s;
}
}
}
public static void main(String args[]){
int n,m;
Scanner in = new Scanner(System.in);
System.out.println("Enter two numbers: ");
n = in.nextInt();
m = in.nextInt();
while(n%2 == 0){
System.out.println("Enter the first number again: ");
n = in.nextInt();
}
while(m%2 == 0){
System.out.println("Enter the second number again: ");
m = in.nextInt();
}
System.out.println("The answer of the recursive sum is: " + recursivesum(n,m));
System.out.println("The answer of the iterative sum is: " + iterativesum(n,m));
}
}
I'm getting an error cannot find symbol - variable enter code heres. I don't know what's wrong! Can anyone help please?
This method is the problem:
public static int recursivesum(int n, int m) {
if (n < m) {
int s = n; // Variable declared here... scoped to the if
s += recursivesum(n+2, m);
} else {
if (m < n) {
int s = m; // Variable declared here... scoped to this if
s += recursivesum(m+2, n);
}
}
return s; // No such variable in scope!
}
You could use:
public static int recursivesum(int n, int m) {
int s = 0; // See note below
if (n < m) {
s = n + recursivesum(n+2, m);
} else {
if (m < n) {
s = m + recursivesum(m+2, n);
}
}
return s;
}
We have to give s an explicit initial value, because you currently don't have any code handling the case where n and m are equal. It's not clear what you want to do then.
Another alternative is to return from the if statements, just as you do in iterativesum... although you'll again need to think about what to do if m == n:
public static int recursivesum(int n, int m) {
if (n < m) {
// You don't even need an s variable!
return n + recursivesum(n+2, m);
} else if (m < n) {
// Simplified else { if { ... } } to else if { .... }
return m + recursivesum(m+2, n);
} else {
// What do you want to return here?
}
}
Note that you've got the same problem in iterativesum - the compiler should be complaining at you at the moment that not all paths return a value. What do you expect iterativesum(3, 3) to do?
in recursivesum(int n, int m) method, you have declared s within if condition, but, you tried to access it in else part.
public static int recursivesum(int n, int m){
int s = n; // Now s having method local scope
if (n < m){
s += recursivesum(n+2, m);
} else{
if(m < n){
int s = m;
s += recursivesum(m+2, n);
}
}
return s;
}
In the recursivesum(int n,int m) function the scope of variable s is inside the if and else block. When your returning s it is out of scope.
Try using some IDE's like eclipse. So that you can debug these errors instantly
Try this:
int s;
if (n < m){
s = n;
s += recursivesum(n+2, m);
} else{
if(m < n){
int s = m;
s += recursivesum(m+2, n);
}
}
return s;
You are declaring variable s inside if statement, that is why you get such error. Start from declaration int s outside if statement.
your s is out of scope in recursivesum(int n, int m) method
Declare s outside the if-else block
It's scope problem. You're declaring variable s inside the if statements which is (local definition) of variable.
You need to modify the two methods as follows:
The recursive method will be
public static int recursivesum(int n, int m) {
int s = 0;
if (n < m) {
s = n;
s += recursivesum(n + 2, m);
} else {
if(m < n){
s = m;
s += recursivesum(m + 2, n);
}
}
return s;
}
And the iterative method will be:
public static int iterativesum(int n, int m) {
int s = 0;
if(n < m) {
s = n;
for(int i = n; i <= m; i += 2) {
s += i;
}
} else {
if(m < n) {
s = m;
for(int i = m; i <= n; i += 2) {
s += i;
}
}
}
return s;
}
You have an error in the first method where you define s outside the scope which you return it from. In the second method you return inside the loop.
As others in this thread suggests, use an IDE like Eclipse (https://www.eclipse.org/) or IntelliJ (http://www.jetbrains.com/idea/)
import java.util.Scanner;
public class AssignmentQ7 {
public static int recursivesum(int n, int m) {
int s = n;
if (n < m) {
s += recursivesum(n + 2, m);
}
else {
if (m < n) {
s = m;
s += recursivesum(m + 2, n);
}
}
return s;
}
public static int iterativesum(int n, int m) {
int s = 0;
if (n < m) {
for (int i = n; i <= m; i += 2) {
s += i;
}
}
else if (m < n) {
for (int i = m; i <= n; i += 2) {
s += i;
}
}
return s;
}
public static void main(String args[]) {
int n, m;
Scanner in = new Scanner(System.in);
System.out.println("Enter two numbers: ");
n = in.nextInt();
m = in.nextInt();
while (n % 2 == 0) {
System.out.println("Enter the first number again: ");
n = in.nextInt();
}
while (m % 2 == 0) {
System.out.println("Enter the second number again: ");
m = in.nextInt();
}
System.out.println("The answer of the recursive sum is: " + recursivesum(n, m));
System.out.println("The answer of the iterative sum is: " + iterativesum(n, m));
}
}
Your code must be like this, you dont have to use two for loop.
import java.util.Scanner;
public class AssignmentQ7 {
public static int recursivesum(final int n, final int m) {
final int lower = n < m ? n : m;
final int upper = n > m ? n : m;
final int total = lower;
if (lower >= upper) {
return total;
}
return total + AssignmentQ7.recursivesum(lower + 2, upper);
}
public static int iterativesum(final int n, final int m) {
final int lower = n < m ? n : m;
final int upper = n > m ? n : m;
int total = 0;
for (int num = lower; num <= upper; num = num + 2) {
total += num;
}
return total;
}
public static void main(final String args[]) {
int n, m;
final Scanner in = new Scanner(System.in);
System.out.println("Enter two numbers: ");
n = in.nextInt();
m = in.nextInt();
while (n % 2 == 0) {
System.out.println("Enter the first number again: ");
n = in.nextInt();
}
while (m % 2 == 0) {
System.out.println("Enter the second number again: ");
m = in.nextInt();
}
System.out.println("The answer of the recursive sum is: " + AssignmentQ7.recursivesum(n, m));
System.out.println("The answer of the iterative sum is: " + AssignmentQ7.iterativesum(n, m));
}
}