In an attempt to relearn how to write code in Java I've been going through some problems from Project Euler. The following code I've written is to solve problem 3: finding the largest prime factor of the number 600851475143.
public class ProjectEuler {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
ProjectEuler t = new ProjectEuler();
System.out.println(t.findLargestPrime(600851475143L));
}
public Boolean isPrime(int x) {
Boolean answer = true;
for (int i = 2; i < x/2; i++) {
if (x%i == 0) {
answer = false;
}
}
return answer;
}
public int findLargestPrime(Long max) {
int largest = 1;
for (int i = 2 ; i < max/2; i++) {
if (max%i == 0 && isPrime(i) && i > largest) {
largest = i;
}
}
return largest;
}
}
However, when I run it the code throws an arithmetic error because I'm trying to divide by zero? The actual error message is shown here:
Exception in thread "main" java.lang.ArithmeticException: / by zero
at projecteuler.ProjectEuler.findLargestPrime(ProjectEuler.java:37)
at projecteuler.ProjectEuler.main(ProjectEuler.java:19)
I don't know if I've made a stupid mistake somewhere or if it's a quirk of Java that I don't understand? Could anyone shed some light on this?
Thank you.
for (int i = 2 ; i < max/2; i++) will eventually overflow (because 600851475143 / 2 is bigger than Integer.MAX_VALUE) and eventually i will equal to 0 when it will happen max%i will throw that exception.
Change i to long if you want to prevent it from happeninng (you should also change all the other int's to long).
This is not an answer to the actual question, but: You do not have to check all the numbers up to max, and you do not even have to do any checks whether a divisor is prime. You just have to (repeatedly) divide the max by any divisor i you found.
long l = 600851475143L;
for (int i = 2; i <= Math.sqrt(l); i++) {
if (l % i == 0) {
System.out.println(i);
l /= i;
i--;
}
}
System.out.println("--> " + l);
This way, you know that this divisor must be prime -- if it were composite, max would already have been divided by its components. This also means that you do not have to loop all the way up to max (which, for the given value, would take a really long time), but only up to the largest prime divisor, which might be much much smaller. And once you reach sqrt(l) -- for the current value of l, not the original! -- you can stop, since there can not be any divisors higher than that, and the remaining value of l will be the final (and thus, largest) prime factor.
Thus, this reduces the complexity from about O(n²) to about O(n½).
Related
I'm a newbie.
So I've been doing the project Euler problems to sharpen my java skills and I got past the first 2. I'm stuck on the third one. It says :
"The prime factors of 13195 are 5, 7, 13 and 29.
What is the largest prime factor of the number 600851475143 ?"
I wrote the following code:
public class Exercise {
public static void main(String[] args) {
long a = 600851475143L;
long i = 1;
boolean isPrime = true;
long currentNum = 0;
while (i <= a) {
if (a % i == 0) {
for (long j = 1; j < a; j++) {
if (a == i || a % j == 0) {
isPrime = false;
} else {
currentNum = i;
}
}
}
i++;
}
System.out.println("the largest prime factor of " + a + " is " + currentNum);
}
}
It works perfectly for smaller numbers like 10,20,55,100,560523 etc but when I enter a large number like 600851475143L, the code compiles and runs but nothing happens.
Any help appreciated, thanks !
Look at your a variable. It's 600851475143L.
That means that your processor has to do up to 600851475143 loop cycles, based on your condition:
while (i <= a) {
i++;
}
I do not take into consideration the code inside the while() loop, but even without it that is too much, even for modern computers :)
If you want to calculate prime factors of numbers of that range... you should consider using some more efficient ways than simple iteration.
You have nested conditions, executions of which will hop CPU.
while (i <= a) {// This condition will be executed 600851475143 times
if (a % i == 0) {
for (long j = 1; j < a; j++) {// This condition will be executed 600851475143 times
So far a=600851475143, code will execute 600851475143*600851475143 times. That's why you don't see output as CPU is busy executing so many conditions.
Also reason you saw output for a=100 because for this input number of iterations = 100*100 which won't take much time on any modern machine
public class Exercise {
public static void main(String[] r)
{
try{
long a = 600851475143L;
System.out.println("the largest prime factor of " + a+ " is " +largestPrimeFactor(a) );
}catch(Exception e)
{
e.printStackTrace();
}
}
public static int largestPrimeFactor(long number) {
int i;
long copyOfInput = number;
for (i = 2; i <= copyOfInput; i++) {
if (copyOfInput % i == 0) {
copyOfInput /= i;
i--;
}
}
return i;
}
}
Try this one
The answer given by Pavel Smirnov is correct. Actually, the code is running for value 600851475143L as well. It's just that the input number is huge so the code is taking hell lot of time to finish. Even the code is not in the deadlocked state. Still, to prove the point to you, you can add a log in the inner-loop body like:
System.out.println("Testing Prime for Number :"+j);
in the inner-loop for (long j = 1; j < a; j++){}
Doing this, you will see that the code is actually running.
P.S.: This algorithm will loop 600851475143L * 600851475143L times, which is really time-consuming.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
Hello I am doing a Euler Project :
2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.
What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?
But I cant't see what is wrong in my code.
Please help.
public long mainNumber()
{
long number=1;
for(int i=2;i<=20;)
{
while(number%i!=0)
{
number++;
}
i++;
}
return number;
}
public static void main(String[] args)
{
Smallest_multiple result =new Smallest_multiple();
System.out.println("The smalest multiple "+result.mainNumber());
}
Try:
public long mainNumber() {
long number = 1;
for (int i = 1; i <= 20; i++) {
if (number % i != 0) {
i = 1; //restart i
number++;
}
}
return number;
}
This will loop from 1 through 20, each time checking if i divides number evenly. If it doesn't then it sets i back to 1 and tries the next number. Of course, there's much more elegant ways of getting to this. But just I'll keep it in the iterative fashion that was presented in the post. Running this results in 232792560 as the final outcome.
Your number is almost always going to be divisible by i, after one increment of number i.e. as you increase number, it becomes divisible by the current value of i, so it will simply stop at 20, as that's the last number you're checking that it's divisible by. It needs to be divisible by ALL of them simultaneously. That's where you've gone wrong here. You simply need to keep incrementing the number and check that it's divisible by all of the numbers from 1 to 20. And once it does, then you've got your desired number. But be careful of variable type limits as you keep incrementing.
It's really not a difficult algorithm. Here's a very simple implementation of it...
public class Test {
public static void main(String[] args) {
long number = 0;
int factors = Integer.parseInt(args[0]);
boolean found = false;
while (!found) {
number++; // OR you could use [number += 2;] instead, as we know it will be even. Performance improvement! :)
found = true;
for (int i = 2; i <= factors; i++) {
found = found && number % i == 0;
}
}
System.out.println("Number: " + number);
}
}
I've written this console app to allow you to enter the number of factors you wish to check. Usage: java Test 20 to get the value you desire of 232792560.
The problem is that your outer loop may get completely exhausted and you still might not find the number that you are looking for.
You should be doing something like this:
private static long gcd(long a, long b)
{
while (b > 0)
{
long temp = b;
b = a % b;
a = temp;
}
return a;
}
public static long mainNumber()
{
long number = 1;
for(int i = 2; i <= 20; i++) {
number = number * (i / gcd(number, i));
}
return number;
}
public static void main(String[] args)
{
Smallest_multiple result =new Smallest_multiple();
System.out.println("The Smallest Multiple: " + result.mainNumber());
}
Output:
The Smallest Multiple: 232792560
Your current algorithm will fail for the large numbers but still if you want to give it a try then you can do something like this:
public long mainNumber()
{
long number = 3;
while(true)
{
boolean flag = true;
for(int i = 2; i <= 10; i++) {
if(number % i != 0) {
flag = false;
break;
}
}
if(flag) break;
else number++;
}
return number;
}
public static void main(String[] args)
{
Smallest_multiple result =new Smallest_multiple();
System.out.println("The Smallest Multiple: " + result.mainNumber());
}
Note that here I'm calculating the result for 10 and not 20. You can modify the condition in the loop as you desire.
Output:
The Smallest Multiple: 2520
Warning: You need to choose the data type carefully as it might overflow for bigger numbers.
So I'm trying to make a method that finds a random prime number from 0 to n(inputed from user) using the RandomGenerator class from the acm.utils package and for some reason it doesn't work! I've thought it through lots of times and I think this solution is right, bot it ends up giving numbers that are not wrong!
This is for a project at the uni and I have to use only acm.util! no scanners! only this kind of code
public int nextPrime(int n){
int num = rgen.nextInt(1, n);
boolean prime=false;
if (num==1){
return (num);
}else{
int i = 2;
int c = 0;
while ((i < num-1)&&(prime=false)){
if( (num % i) == 0){
c=c+1;
}
if((c==0)&&(i==(num-1))){
prime=true;
}
if(c>=1){
num = rgen.nextInt(1, n);
i=1;
}
i=i+1;
}
}
return (num);
}
= is for assignment and == is for comparison.
You need to change your condition
while ((i < num-1)&&(prime=false)){
to
while ((i < num-1)&&(prime==false)){ or
while ((i < num-1)&&(!prime)){
Here is a basic method to determine if a number is prime or not. This method is really only meant to understand the logic behind finding if a number is prime or not. Enjoy.
public boolean isPrime()
{
boolean prime = true;
for (int s = 2; s < original; s++)
if (original % s != 0 )
{
prime = true;
}
else
{
prime = false;
return prime;
}
return prime;
Your code is rather bizarre. Variable like c is not very clear, and the name doesnt help.
You could read other implementations.
I made some change to make it work. Traces help too !
public static int nextPrime(int n)
{
int num = (int)(1+Math.random()*n); // other generator
// TRACE HERE
System.out.println("START:"+num);
boolean prime=false;
// CHANGE HERE
if (num==2)
{
return (num);
}
else
{
int i = 2;
int c = 0;
// CHANGE HERE
while ((i < num-1)&&(prime==false))
{
// Not prime => next one
if( (num % i) == 0)
{
// TRACE HERE
System.out.println("YOU LOSE: :"+num+" divided by "+i);
c=c+1;
}
if((c==0)&&(i==(num-1)))
{
prime=true;
// TRACE HERE
System.out.println("BINGO:"+num);
// CHANGE HERE
break;
}
if(c>=1)
{
// SAME PLAYER LOOP AGAIN
num = (int)(1+Math.random()*n);
// TRACE HERE
System.out.println("RESTART:"+num);
i=1;
// CHANGE HERE
c=0;
}
i=i+1;
}
}
return (num);
}
Either you have misstated the problem or else you have not come close to coding the problem correctly. There are 4 prime numbers up to 10: {2,3,5,7}. If the user enters 10, should you give a random prime from this set, or a random one of the first 10 primes {2,3,5,7,11,13,17,19,23,29}? You said the problem was the first interpretation (where 11 would not be a valid response to 10) but you implemented an attempt at the second interpretation (where 11 would be a valid response to 10).
There is a simple way to generate a prime number uniformly within the primes in the range [1,1000000], say. Choose a random integer in the range and test whether it is prime. If so, return it. If not, repeat. This is called rejection sampling. It is quite complicated to get a uniformly random prime number without rejection sampling, since it's not easy to count or list the prime numbers in a large range. It is relatively easy to test whether a number is prime, and for n>1 it only takes about log n samples on average to find a prime in [1,n].
I'm trying to solve this coding question:
Given an integer n, return the number of trailing zeroes in n!
Below is my code (codec this up using the wiki link)
public int trailingZeroes(int n) {
int count = 0, i = 5;
while(i<=n){
count+= n/i;
i*=5;
}
return count;
}
This runs for all test cases except when n = Integer.MAX_VALUE upon which I get a TLE. How can I fix this code to make it cover that test case. I have read about five articles on the net and everything seems to agree with my approach.
Much thanks.
So, I followed the long/BigInteger approach (thanks y'all):
public int trailingZeroes(int n) {
long count = 0;
for(long i= 5; n/i >= 1; i= i*5){
count+= n/i;
}
return (int)count;
}
As Iaune observed, your loop will never terminate when n is Integer.MAX_VALUE, because there is no int greater than that number (by definition). You should be able to restructure your loop to avoid that problem. For instance, this is the same basic approach, but flipped upside-down:
public int trailingZeroes(int n) {
int count = 0;
while (n > 0) {
n /= 5;
count += n;
}
return count;
}
You cannot write a for or while loop where the loop counter is an int and the upper limit is <= Integer.MAX_VALUE.
What happens with a simple increment (counter++) is that the loop counter is set to that value, the body executes and then the counter is incremented which results in a negative number, Integer.MIN_VALUE. And then everything happens all over again.
Other weird things may happen when the loop counter is incremented in quantities > 1 or (as here) is multiplied: the int loop counter just can't hold a value > Integer.MAX_VALUE
Consider another approach for iterating over these numbers. Or handle MAX_VALUE separately.
Your problem is that once i gets large enough (more than Integer.MAX_INT / 5) then the line i*=5; causes i to overflow to the "wrong" value. The value in question is 5 to the 14th power, which is 6103515625, but which overflows to 1808548329.
The result of this is that the loop just keeps executing forever. i will never become a value that's not <= Integer.MAX_INT, because there's just no such int.
To avoid this, you need i to be a larger data type than an int. If you change i and count in your original code to long, this will work fine. Of course, BigInteger would also work.
public class FactorialNumberTrailingZeros {
public static void main(String[] args) {
System.out.println(trailingZeroes(1000020));
}
private static int trailingZeroes(int n) {
int count = 0;
while (n > 0 && (n % 10 == 0)) {
n /= 10;
count ++;
}
return count;
}
}
public static void main(String[] args) {
int result = findFactorialTrailingZero(100);
System.out.println("no of trailing zeros are " + result);
}
public static int findFactorialTrailingZero(int no) {
int zeros = no / 5;
int zeroIncrementNo = 25;
int zerosIncrementFactor = 1;
int nextZeroIncrenent = 5;
for (int i = 1;no >= zeroIncrementNo; i++) {
zeros=zeros+zerosIncrementFactor;
zeroIncrementNo=25*(i+1);
if(i+1==nextZeroIncrenent){
zerosIncrementFactor++;
nextZeroIncrenent=nextZeroIncrenent*5;
}
}
return zeros;
/*
[n/5]+[n/25]+[n/125]+....
if n<25 then [n/5]
if n<125 then [n/5]+[n/25]
if n<625 then [n/5]+[n/25]+[n/125]
*/
#include<bits/stdc++.h>
#include<iostream>
using namespace std;
int countTrailingZeroes(int n)
{
int res=0;
for(int i=5;i<=n;i=i*5){
res=res+n/i;
}
return res;
}
int main(){
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int n;
cin>>n;
cout<<countTrailingZeroes(n);
return 0;
}
Output
25
6
Explanation:
25!=1.551121e+25 i.e contains 6 trailing zeroes
Here is my python code that could solve your problem:
def check(n):
j,ans=5,0
while j<=n:
ans=ans+n//j
j=j*5
return ans
First of all, this isn't homework... working on this outside of class to get some practice with java.
public class Problem3 {
public static void main(String[] args) {
int n = 13195;
// For every value 2 -> n
for (int i=2; i < n; i++) {
// If i is a multiple of n
if (n % i == 0) {
// For every value i -> n
for (int j=2; j < i; j++) {
if (n % j != 0) {
System.out.println(i);
break;
}
}
}
}
}
}
I keep modifying the code to try to make it do what I want.
As the problem says, you should be getting 5, 7, 13 and 29.
I get these values, plus 35, 65, 91, 145, 203, 377, 455, 1015, 1885, and 2639. I think I'm on the right track as I have all the right numbers... just have a few extras.
And in checking a few of the numbers in both being divisible by n and being prime numbers, the issue here is that the extra numbers aren't prime. Not sure what's going on though.
If anyone has any insight, please share.
This part
for (int j=2; j < i; j++) {
if (n % j != 0) {
System.out.println(i);
break;
}
doesn't check whether i is prime. Unless i is small, that will always print i at some point, because there are numbers smaller than i that don't divide n. So basically, that will print out all divisors of n (It wouldn't print the divisor 4 for n == 12, for example, but that's an exception).
Note also that the algorithm - using long instead of int to avoid overflow - even if fixed to check whether the divisor i is prime for deciding whether to print it, will take a long time to run for the actual target. You should investigate to find a better algorithm (hint: you might want to find the complete prime factorisation).
I solved this problem in Java and looking at my solution the obvious advice is start using BigInteger, look at the documentation for java.math.BigInteger
Also a lot of these problems are "Math" problems as much as they are "Computer Science" problems so research the math more, make sure you understand the math reasonably well, before coming up with your algorithm. Brute force can work some times, but often there are tricks to these problems.
Brut force can also work for checking whether factor is prime or not for this problem...
eg.
for(i=1;i<=n;i++)// n is a factor.
{
for(j=i;j>=1;j--)
{
if(i%j==0)
{
counter++;// set counter=0 befor.
}
if(counter==2) // for a prime factor the counter will always be exactly two.
{
System.out.println(i);
}
counter=0;
}
}
Don't know about Java but here is my C code if it is of any help.
# include <stdio.h>
# include <math.h>
// A function to print all prime factors of a given number n
void primeFactors(long long int n)
{
// Print the number of 2s that divide n
while (n%2 == 0)
{
printf("%d ", 2);
n = n/2;
}
int i;
// n must be odd at this point. So we can skip one element (Note i = i +2)
for ( i = 3; i <= sqrt(n); i = i+2)
{
// While i divides n, print i and divide n
while (n%i == 0)
{
printf("%d ", i);
n = n/i;
}
}
// This condition is to handle the case whien n is a prime number
// greater than 2
if (n > 2)
printf ("%ld ", n);
}
/* Driver program to test above function */
int main()
{
long long int n = 600851475143;
primeFactors(n);
return 0;
}
Its very good that you are working on such problems out of class.
Saw your code. You are writing a procedural code inside main function/thread.
Instead write functions and think step by step algorithmically first.
The simple algorithm to solve this problem can be like this:
1) Generate numbers consecutively starting from 2 which is the least prime, to 13195/2. (Any number always has its factor smaller than half of it's value)
2) Check if the generated number is prime.
3) If the number is prime then check if it is factor of 13195;
4) Return the last prime factor as it is going to be the largest prime factor of 13195;
One more advice is try writting seperate functions to avoid code complexity.
Code is like this...
public class LargestPrimeFactor {
public static long getLargestPrimeFactor(long num){
long largestprimefactor = 0;
for(long i = 2; i<=num/2;i++){
if(isPrime(i)){
if(num%i==0){
largestprimefactor = i;
System.out.println(largestprimefactor);
}
}
}
return largestprimefactor;
}
public static boolean isPrime(long num){
boolean prime=false;
int count=0;
for(long i=1;i<=num/2;i++){
if(num%i==0){
count++;
}
if(count==1){
prime = true;
}
else{
prime = false;
}
}
return prime;
}
public static void main(String[] args) {
System.out.println("Largest prime factor of 13195 is "+getLargestPrimeFactor(13195));
}
}