#loop problem

4 messages · Page 1 of 1 (latest)

fast crystal
#

so I wanna find the largest prime factor and the loop kinda stops at some number (pls dont give optimized code rather tell me why the loop doesnt continue)

code:

import java.math.BigInteger;

public class Main {



    public static void main(String[] args) {


        BigInteger num = new BigInteger("600851475143");
        BigInteger erg = BigInteger.ZERO;

        for (BigInteger i = BigInteger.ONE; i.compareTo(num) <= 0; i = i.add(BigInteger.ONE)) {
            if (isPrime(i) && num.mod(i).equals(BigInteger.ZERO)) {
                erg = i;
                System.out.println(erg);
            }
        }

        System.out.println(erg);

    }

    static boolean isPrime(BigInteger num)
    {
        if(num.compareTo(BigInteger.ONE) <= 0)
        {
            return false;
        }
        for(BigInteger i = BigInteger.TWO; i.multiply(i).compareTo(num) <= 0; i = i.add(BigInteger.ONE))
        {
            if(num.mod(i).equals(BigInteger.ZERO)) {
                return  false;
            }

        }
        return true;
    }
}

this is my output and its still in the loop:
https://cdn.discordapp.com/attachments/1116821865772626020/1166473603194310766/image.png?ex=654a9e33&is=65382933&hm=6c4a6f8141d5665421ccc7d113ef26fbb3deaa7e5972cb7efb00c57a906c5ee3&

lunar zenithBOT
#

This post has been reserved for your question.

Hey @fast crystal! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

winged cave
#

The loop continues, it's kind of the problem. You've found all prime factors so there won't be anymore, but it checks all numbers until your chosen one