I've the below code.
import java.util.ArrayList;
import java.math.*;
import java.util.Arrays;
import java.util.Collections;
public class Dummy {
public static void main(String args[]) throws Exception {
int n=12;
double val=(3+Math.sqrt(5));
double ne=Math.pow(val, n);
String new2=String.valueOf(ne);
System.out.println(ne);
String[] new1=new2.split("\\.");
if(new1[0].length()>3){
new1[0]=new1[0].substring(Math.max(new1[0].length() - 4, 0));
if(new1[0].length()<3){
new1[0]=("0").concat(new1[0]);
}
else{
new1[0]=new1[0];
}
}
else if(new1[0].length()<2){
new1[0]=("00").concat(new1[0]);
}
else if(new1[0].length()<1){
new1[0]=("000").concat(new1[0]);
}
else if(new1[0].length()<3){
new1[0]=("0").concat(new1[0]);
}
System.out.println(new1[0]);
}
}
here i'm trying to calculate sum of 3 with root 5 and whole to power of 12
(3+sqrt(5))^12
when i do it the result i get is 4.246814719604947E8 but in real the answer is to be 424681471.960494. please let me know where am i going wrong.
It's in the scientific notation.
If you don't want it in this notation, you can try
public static void main(String[] args) {
Double d = Math.pow(3+Math.sqrt(5),12);
System.out.println(d); //4.246814719604947E8
System.out.println(new BigDecimal(d).toPlainString()); //424681471.960494697093963623046875
}
Use this
System.out.println(String.format("%f", ne));
Did you notice the E8 in your answer? Then the answer is correct :)
The answer you are getting is correct.
4.246814719604947E8 means 4.246814719604947 times 10^8. If you move the decimal point 8 places to the right you see the answer you expect.
Related
So I designed this code (given below) for the question & it seems to be giving all the answers there are but for some reason I'm not able to pass my test cases in the Hackerrank Question except for the sample one.
My Code :
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int t = in.nextInt();
for(int a0 = 0; a0 < t; a0++){
BigInteger n = new BigInteger(in.next());
BigInteger max = BigInteger.valueOf(1);
for(BigInteger i=BigInteger.valueOf(2);i.multiply(i).compareTo(n)<=0;i=i.add(BigInteger.ONE))
{
if(i.isProbablePrime(1) && n.mod(i).compareTo(BigInteger.valueOf(0))==0 && i.compareTo(max)>=0)
{
max = i;
n = n.divide(i);
}
}
if(n.isProbablePrime(1))
max = n;
else if(n.divide(BigInteger.valueOf(2)).isProbablePrime(1))
max = n.divide(BigInteger.valueOf(2));
System.out.println(max);
}
}
}
I'm getting the same output for each input as this website here.
Can anyone please look have a look at my code & point out where am I going wrong. I'm not looking for new code or logic, I just want to know how & why my code or logic is wrong. Can anyone help?
Thanks in advance.
EDIT : NEW CODE Found The Solution Thanks to #Douglas for helping me with hints
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int t = in.nextInt();
for(int a0 = 0; a0 < t; a0++){
long n = in.nextLong();
long max = 1;
while(n%2==0)
n = n/2;
if(n==1)
max = 2;
for(long i=3;i*i<=n;i+=2)
{
while(n%i==0 && i>=max)
{
max = i;
n = n/i;
}
}
if(n>2)
max = n;
System.out.println(max);
}
}
}
The code above runs perfectly fine & passes all the testcases.
You have three problems with this code:
An actual bug. You don't handle when a prime divides the number multiple times. I'm fairly sure the four lines before the println output are a workaround you put in when you encountered a failure this causes, that fixed the specific failure you found but don't actually address the root cause. I just tested it, your code outputs 3 for the input 252. The correct output for 252 is 7.
Part of your code invokes a library method that may be significantly expensive. You should not be using isProbablePrime.
Your algorithm's logic scales poorly. Hackerrank's test cases probably include something where the correct output is something like 20 digits long. A major point of Project Euler is that it's not enough to write code that will find the correct answer. You need to write code that will find the correct answer efficiently. To fix this, you will need new logic for the fundamental design of the program.
I recently started learning java and this question keeps on bugging me..
class Example{
public static void main(String args[]){
double d;
d=4.1 % 1.1;
System.out.println("4.1%1.1 : "+d);
d=5.5 % 1.1;
System.out.println("5.5%1.1 : "+d);
}
}
the output for the above program is
4.1%1.1 : 0.7999999999999994 and
5.5%1.1 : 1.0999999999999996
but when we do these calculations by hand, we get 0.8 for the first one and 0 for the second one... why does this happen??
% gives the correct answer when integers are used in the expression..
class Example{
public static void main(String args[]){
int x;
x=10%17;
System.out.println("10%17 : "+x); **//prints 10**
}
}
can anyone pl explain what exactly happens here?
I would expect this to return with 7, given the input of (2,2). Instead of getting a proper output, the program returns a java.lang.StackOverflowError at line 16.
package main;
import java.math.BigInteger;
public class Ackermann {
public static void main(String[] args) {
System.out.println(ack(BigInteger.valueOf(2),BigInteger.valueOf(2)));
}
public static BigInteger ack(BigInteger a, BigInteger b) {
BigInteger ans;
if (a.equals(0)) ans = b.add(BigInteger.ONE);
else if (b.equals(0)) ans = ack(a.subtract(BigInteger.ONE),BigInteger.valueOf(1));
else ans = ack(a.subtract(BigInteger.ONE), ack(a,b.subtract(BigInteger.ONE))); //line 16
return (ans);
}
}
I've increased the maximum stack size all the way up to 2GB, but it's still throwing the error at the small input of (2,2). Before I started using the BigIntegers instead of Longs, everything worked out fine with the input (2,2), but now it's a mess.
Instead of equals(0) you have to use equals(BigInteger.ZERO).
Otherwise you compare a BigInteger to an Integer (auto boxing) which will always be false.
I am using the code below to get the answer of a method i created into two decomal places. But when I do this and complile i get an error saying identifier expected. 2 error come up one pointing at the 2 and the other just before. what is my problem?
import java.text.NumberFormat;
NumberFormat nf = NumberFormat.getNumberInstance();
nf.setMaximumFractionDigits(2);
What you've shown is correct, assuming that the lines aren't all together (import statements must be outside of any class). So for instance, this is valid:
import java.text.NumberFormat;
class MyClass {
void someMethod() {
NumberFormat nf = NumberFormat.getNumberInstance();
nf.setMaximumFractionDigits(2);
// ...
}
}
...but those lines together as shown in your question are not.
If that's not it, you said that the error seems to focus on the 2. Sometimes when we see questions like this here on SO, it's because some zero-width or space-like special character has accidentally ended up in the source. So it may be that if you delete that line and retype it, you'll wipe out the offending character. (It's actually surprising how often that comes up.)
You can write a general purpose function as follows:
public static double round(double inputNumber, int fractionDigits, int roundingMode) {
BigDecimal bigDecimal = new BigDecimal(inputNumber);
BigDecimal rounded = bigDecimal.setScale(fractionDigits, roundingMode);
return rounded.doubleValue();
}
Please find below the sample test results:
import java.math.BigDecimal;
public class RoundHelper {
public static void main(String[] args) {
System.out.println(RoundHelper.round(123.98980, 2, BigDecimal.ROUND_HALF_UP));
System.out.println(RoundHelper.round(123.98000, 2, BigDecimal.ROUND_HALF_UP));
System.out.println(RoundHelper.round(123.98000, 2, BigDecimal.ROUND_HALF_UP));
System.out.println(RoundHelper.round(123.55087, 2, BigDecimal.ROUND_HALF_UP));
System.out.println(RoundHelper.round(123.14000, 2, BigDecimal.ROUND_HALF_UP));
}
public static double round(double inputNumber, int fractionDigits, int roundingMode) {
BigDecimal bigDecimal = new BigDecimal(inputNumber);
BigDecimal rounded = bigDecimal.setScale(fractionDigits, roundingMode);
return rounded.doubleValue();
}
}
Output:
123.99
123.98
123.98
123.55
123.14
I just wrote a lab that is supposed to flip two coins 500 times and count how many times heads and tails were flipped and is supposed to calculate the percentage to the tenth. I remember the template for Decimal formatting, but forgot how to use it. Can somebody help? My program is as follows:
import java.util.Scanner;
import java.text.DecimalFormat;
public class Lab97a
{
public static void main(String [] args)
{
Scanner input = new Scanner(System.in);
DecimalFormat accuracy=new DecimalFormat("0.0");
Dice coin;
int numCoin;
int numHeads;
int numTails;
int count;
double percentageHeads;
double percentageTails;
coin=new Dice(2);
numHeads=0;
numTails=0;
System.out.println("A coin is tossed 500 times. The results are asfollows: ");
for (count=1;count<=500;count=count+1)
{
numCoin=coin.roll();
if (numCoin==1)
{
numHeads+=1;
}
if (numCoin==2)
{
numTails+=1;
}
}
percentageHeads=numHeads/5;
percentageTails=numTails/5;
System.out.println("Heads was flipped "+numHeads+" times,
"+percentageHeads+"%.");
System.out.println("Tails was flipped "+numTails+" times, "+percentageTails+"%");
}
}
It looks like you just need to format the accuracy with e.g. accuracy.format(percentageHeads). See NumberFormat.format(double) that DecimalFormat extends.
You can use the String.format method for this, as described here:
System.out.println(String.format("Heads was flipped %d times, %.2f %%"), numHeads,percentageHeads);