Why is my code printing the output 4 times? The answer is correct but the answer is printed 4 times instead of the desired one time.
import java.util.*;
import java.math.BigInteger;
class THIRTYSEVEN
{
static Scanner sc = new Scanner(System.in);
public static void main(String[] args)
{
BigInteger a = new BigInteger("1");
multiply(a,0,sc.nextInt());
}
static void multiply(BigInteger b, int loop, int power)
{
BigInteger result = b;
while(loop<power)
{
result = result.multiply(new BigInteger("8"));
loop++;
multiply(result,loop,power);
}
System.out.println(result);
}
}
You call multiplyonly once, but it recursively calls itself (and prints every time). You could return the result instead (and print it from main).
Related
I am solving a code for competitive programming and my code which I think is correct is throwing an error for some test cases:
The question is to find the total number of digits in an integer using recursion.
My Code:
import java.util.*;
import java.io.*;
import java.lang.*;
class Driver_class
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-- > 0)
{
int n =sc.nextInt(); // taking number "n" as input
System.out.println(new Digitcount().countDigits(n)); // prints the count of digits
}
}
}
class Digitcount
{
static int count=0;
public static int countDigits(int n)
{
if(n<1)
return 0;
countDigits(n/10);
count++;
return count;
}
}
The count variable is the total number of digits.
The test case that this code fails:
However, this code passes all the test cases:
class Digitcount
{
static int count=0;
public static int countDigits(int n)
{
if(n<1)
return 0;
return countDigits(n/10)+1;
}
}
I dont see a difference in any of my codes regarding the output value. What is causing the first code to fail the test case?
The difference is in use of a static variable. It will hold the result between tests and thus in any test after the first one it will be incorrect. If you have two calls one after the other like so:
int a = countDigits(605);
int b = countDigits(605);
The result will be a==3, b==6 - the first result is carried over to the second call.
I tried coderbyte and looks something is wrong with it. The first challenge is simply to reverse a string. I did this in java:
import java.util.*;
import java.io.*;
class Main {
public static String FirstReverse(String str) {
char[] chars = str.toCharArray();
String ret = "";
for (int i = chars.length - 1; i >= 0; i--) {
ret += chars[i];
}
return ret;
}
public static void main (String[] args) {
// keep this function call here
Scanner s = new Scanner(System.in);
System.out.print(FirstReverse(s.nextLine()));
}
}
It said, that three test cases had wrong output and there was the correct output. I tried running the code with the specified cases and it printed the same string as correct output for that case. So I tried resubmitting it and it said that only one test case was correct and all other had wrong out put. So I said OK and rewrote my code this way:
import java.util.*;
import java.io.*;
class Main {
public static String FirstReverse(String str) {
return new StringBuilder(str).reverse().toString();
}
public static void main (String[] args) {
// keep this function call here
Scanner s = new Scanner(System.in);
System.out.print(FirstReverse(s.nextLine()));
}
}
Unfortunately it still says only one test case was successful... Any ideas what happened? Thanks
I am beginner at coding. I am getting NZEC error on submission of my code for prime number genertaion to spoj. But the code is working perfectly fine in my desktop. Kindly help me. This is what i have coded.
import java.util.*;
import java.lang.*;
import java.io.*;
import static java.lang.Math.*;
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
int testcase;
int lower_limit,upper_limit;
int i,j,k;
boolean[] a= new boolean[100001];
Arrays.fill(a, Boolean.TRUE);
Scanner sc1 = new Scanner(System.in);
testcase= sc1.nextInt();
for(;testcase>0;testcase--)
{
lower_limit= sc1.nextInt();
upper_limit= sc1.nextInt();
for(i = 2;i<sqrt(upper_limit);i++)
{
if(a[i]=true)
{
for(j=i;j<=upper_limit;j=j+i)
{
a[j]=false;
}
}
}
for(i=lower_limit;i<upper_limit;i++)
{
if(a[i]==true)
{
System.out.println(i);
}
}
}
}
}
Your array goes out of index for large values of m and n. Try this sample test case on your system where m = 999900000 and n = 1000000000. You can't store these large values as the index of the array. Even m or n = 10^8 will go out of bound index for an array.
this is my java code :
import java.util.Scanner;
import java.math.*;
abstract class ccFctrl {
public static long countZero(BigInteger a){
long noOfZero=0;
long b;
do{
b =noOfZero;
if(a.remainder(BigInteger.TEN)==BigInteger.ZERO){
noOfZero++;
}
a.divide(BigInteger.TEN);
}while((noOfZero!=0)&&(noOfZero!=b));
return noOfZero;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner s=new Scanner(System.in);
int numOfFctrlsToCalculat=s.nextInt();
BigInteger fctrl=new BigInteger("1");
BigInteger inc=new BigInteger("1");
int num;
long[] count=new long[numOfFctrlsToCalculat];
System.out.println();
for(int i=0;i<numOfFctrlsToCalculat;i++){
num=s.nextInt();
for(int j=1;j<=num;j++){
fctrl=fctrl.multiply(inc);
inc=inc.add(BigInteger.ONE);
}
inc=BigInteger.ONE;
count[i]=countZero(fctrl);
System.out.println();
}
for(int i=0;i<numOfFctrlsToCalculat;i++){
System.out.println(count[i]);
}
s.close();
}
}
IDE on compiler is constantly showing me error. just can't get why code is not running
Two things:
You are going into infinite loop in your method countZero. Remember BigInteger is immutable, so doing operation on the same, you should reassign the value like:
a = a.divide(BigInteger.TEN);
Your condition will never get satisfied because of above and hence will lead to infinite loop. You should check for below condition:
} while (!a.equals(BigInteger.ZERO));
Note Aside: Any reason for defining your class as abstract?
This is known programming contest problem.
Descrition is here for example.
please have a look at the following code
import java.util.ArrayList;
import java.util.List;
public class Big
{
static int primeNumber = 2;
public static void main(String[]args)
{
int numberDevident = 147;
int left=0;
int result=0;
List numbers = new ArrayList();
while(true)
{
result = numberDevident/primeNumber;
left = numberDevident%primeNumber;
if(left!=0)
{
primeNumber++;
}
else
{
numbers.add(primeNumber);
numberDevident = result;
System.out.println(primeNumber);
}
}
}
}
This code find the prime factors of a given number (Variable "numberDevident" in the code"). But, there is a case, that is, the given number is 600851475143
It is no way matching to int, long or double. How can I solve this using this much of a big number? Please help
Here I am doing the same with BigInteger
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
public class Problem3
{
static BigInteger primeNumber = new BigInteger("2");
static BigInteger zero = new BigInteger("0");
static BigInteger add = new BigInteger("1");
public static void main(String[]args)
{
BigInteger numberDevident = new BigInteger("147");
BigInteger left= new BigInteger("0");;
BigInteger result=new BigInteger("0");;
List numbers = new ArrayList();
while(true)
{
result = numberDevident.divide(primeNumber);
left = numberDevident.remainder(primeNumber);
if(left!=zero)
{
primeNumber.add(add);
}
else
{
numbers.add(primeNumber);
numberDevident = result;
System.out.println(primeNumber.toString());
}
}
}
}
still no good, it is not displaying anything. Please help.
You might want to take a look at BigInteger.