Project Euler #3 out of integer range java [duplicate] - java

This question already has answers here:
The literal xyz of type int is out of range
(5 answers)
Closed 8 years ago.
The code is supposed to give back the biggest prime number.
More about the task here: https://projecteuler.net/problem=3
int checkFactors(double na) {
long n = (long) na;
int biggestPrimeFactor = 0;
for (int i = 1; i < n; i++)
if (n % i == 0 && isPrimFaktor(i) && i > biggestPrimeFactor)
biggestPrimeFactor = i;
return biggestPrimeFactor;
}
boolean isPrimeFactor(int n) {
int length= 0;
for (int i = n; i > 0; i--)
if (n % i == 0)
length++;
if (length== 2)
return true;
return false;
}
I decided to make the parameter of checkFactors() a double because I tried to test why my code didn't work properly.
System.out.println(checkFactors(13195));
works and returns "29".
However, System.out.println(checkFactors(600851475143));
does not work,
"600851475143 of type int is out of range".
System.out.println(checkFactors(600851475143.0));
does compile but gives me after a couple of seconds an ArithmeticException.

600851475143 of type int is out of range
This number is bigger than int can store. Appending .0 to the number converts the number into a double which can represent that number
Instead of .0 you can do checkFactors(600851475143d) which ensure the number is a double and not an int

Use long as a data type for na and also biggestPrimeFactor. The values are too large for storing in an int variable.

Try to make Your parameter back to long and make letter L after your large number like this 600851475143L, I think it will work

Related

Java Math.sqrt function rounding down to zero [duplicate]

This question already has answers here:
Int division: Why is the result of 1/3 == 0?
(19 answers)
How to divide 1 by 25(1/25) and get result .04 in java [duplicate]
(4 answers)
Closed 1 year ago.
I am trying to write a function to check a T score value and populate half of a 5x5 array.
public void calcTScores()
{
double temp = 0;
double tempSp = 0;
int n = 24;
this.tScores = new String [5][5];
for (int i = 0; i< this.Headers.length; i++){
for (int j = 0; j<this.Headers.length; j++)
{
if(i < j)
{
tempSp += (n-1)*this.SD[i] * this.SD[i] + (n-1)*this.SD[j] * this.SD[j];
tempSp = tempSp/(n+n-2);
tempSp = Math.sqrt(tempSp);
temp = tempSp * Math.sqrt(0.0833);
System.out.println(Math.sqrt(1/12));
temp = ((this.Mean[i] - this.Mean[j])/temp);
if(temp > 2.25 || temp< -2.25)
{
this.tScores[i][j] = "Y";
}
else
{
this.tScores[i][j] = "N";
}
temp = 0;
tempSp = 0;
}
}
}
}
Any idea why Math.sqrt(0.0833) and Math.sqrt(1/12) would evaluate to different values?
The T score when I add the 1/24 and 1/24 value and take the sqrt keeps evaluating to zero but when I plug in the actual decimal it gives me the answer I would expect
Any ideas why this is occuring?
1/12==0 as per integer division
There's nothing wrong with Math.sqrt. You're passing it the number zero.
Math.sqrt(1/12)
1/12 is a division operation on two integers (namely, 1 and 12), so it's going to produce an integer result. 12 goes into 1 zero times, so the integer result is zero. Thus, your expression is
Math.sqrt(0)
Consider
Math.sqrt(1.0/12.0)
Math.sqrt(1/12d)
Cast 1/12 to a double by casting one operand to a double.

Java- working with an integer

Write a static method called digitsInARow that takes an integer n as a parameter and that returns the highest number of digits that appear in a row in the base-10 representation of n. For many numbers the answer will be 1 because they don't have adjacent digits that match. But for a number like 3555585, the answer is 4 because there are four occurrences of the digit 5 that appear in a row. You are NOT allowed to use a String to solve this problem. You may assume that the value passed to the method is greater than or equal to 0.
public static int digitsInARow(int n) {
if (n / 10 == 0) {
return 1;
}
int count = 0;
int count1 = 0;
while (n > 0) {
int digit = n % 10;
int a = n / 10;
if (digit == a % 10) {
count++;
} else {
count1 = Math.max(count1, count);
count = 0;
}
n = n / 10;
}
return Math.max(count, count1);
}
I know the if statement is messed up. I am trying to figure out a way to compare consecutive digits WITHOUT using Integer class or String class. Any suggestions?
The problem with your code is that count keeps track of the current count, not of the highest count. You need to add a variable that tracks the highest count as well, and update it each time you process a digit, before resetting count back to zero.
Don't forget to update the highest count when you exit the loop, in case then-current count is greater than the previously found max.

Best way to check if a value is an integer? [duplicate]

This question already has answers here:
How to test if a double is an integer
(18 answers)
Closed 8 years ago.
There are several ways to decide if a value is an integer or not.
All of the ways i know are using the divide operation.
Which method is the fastest and:
Is there a method to do this without doing floating point operations?
EDIT: For clarification, my program is only dealing with integer and double values. Here is some of my code:
for (int i = 6;; i++) {
for (int j = i - 1; j > 0; j--) {
double number1 = i;
double number2 = j;
double d = number1 / number2;
int help = (int)d;
if( (d - help) == 0.0){
System.out.println("Whole number found");
}
}
}
Note that int / int will also be an int due to the rules of integral division. You need to promote one of them to floating point or use the idiom 1.0 * a / b if you want to retain a remainder.
If you have a floating point number, f, then
java.lang.Math.floor(f) - f == 0 is probably the best way.
It avoids an intermediate cast to int which can overflow.
This will do what you want!
String str = "1";
try {
Integer.parseInt(str);
} catch (NumberFormatException e) {
System.err.println("Not a number");
}

Fastest way to get number of digits on a number? [duplicate]

This question already has answers here:
Way to get number of digits in an int?
(29 answers)
Closed 9 years ago.
I have do detect the amount of digits on a number. For example, 329586 has 6 digits.
What I done, is simply parsing the number to string, and getting the string length, like:
number.toString().length()
But, is there a fastest way to count digits on a number? I have to use this method several times, so I think using toString() can impact performance.
Thanks.
Math.floor(Math.log10(number) + 1)
// or just (int) Math.log10(number) + 1
For example:
int number = 123456;
int length = (int) Math.log10(number) + 1;
System.out.println(length);
OUTPUT:
6
how about this homebrewed solution:
int noOfDigit = 1;
while((n=n/10) != 0) ++noOfDigit;
Try this :
Working Example
public class Main {
public static void main(String[] args) {
long num = -23;
int digits = 0;
if (num < 0)
num *= (-1);
if (num < 10 && num >= 0)
digits = 1;
else {
while(num > 0) {
num /= 10;
digits++;
}
}
System.out.println("Digits: " +digits);
}
}

Retrieving the first digit of a number [duplicate]

This question already has answers here:
Return first digit of an integer
(25 answers)
Closed 5 years ago.
I am just learning Java and am trying to get my program to retrieve the first digit of a number - for example 543 should return 5, etc. I thought to convert to a string, but I am not sure how I can convert it back? Thanks for any help.
int number = 534;
String numberString = Integer.toString(number);
char firstLetterChar = numberString.charAt(0);
int firstDigit = ????
Almost certainly more efficient than using Strings:
int firstDigit(int x) {
while (x > 9) {
x /= 10;
}
return x;
}
(Works only for nonnegative integers.)
int number = 534;
int firstDigit = Integer.parseInt(Integer.toString(number).substring(0, 1));
firstDigit = number/((int)(pow(10,(int)log(number))));
This should get your first digit using math instead of strings.
In your example log(543) = 2.73 which casted to an int is 2.
pow(10, 2) = 100
543/100 = 5.43 but since it's an int it gets truncated to 5
int firstDigit = Integer.parseInt(Character.toString(firstLetterChar));
int number = 534;
String numberString = "" + number;
char firstLetterchar = numberString.charAt(0);
int firstDigit = Integer.parseInt("" + firstLetterChar);
Integer.parseInt will take a string and return a int.
This example works for any double, not just positive integers and takes into account negative numbers or those less than one. For example, 0.000053 would return 5.
private static int getMostSignificantDigit(double value) {
value = Math.abs(value);
if (value == 0) return 0;
while (value < 1) value *= 10;
char firstChar = String.valueOf(value).charAt(0);
return Integer.parseInt(firstChar + "");
}
To get the first digit, this sticks with String manipulation as it is far easier to read.
int number = 534;
int firstDigit = number/100;
( / ) operator in java divide the numbers without considering the reminder so when we divide 534 by 100 , it gives us (5) .
but if you want to get the last number , you can use (%) operator
int lastDigit = number%10;
which gives us the reminder of the division , so 534%10 , will yield the number 4 .
This way might makes more sense if you don't want to use str methods
int first = 1;
for (int i = 10; i < number; i *= 10) {
first = number / i;
}

Categories