Project Euler #3: Largest prime factor

Sort by

recency

|

475 Discussions

|

  • + 0 comments

    You can use Unitextify for different font styles. However, the solution to problem is:

        # Start with the smallest prime factor
        factor = 2
        while factor * factor <= n:
            if n % factor == 0:
                n //= factor
            else:
                factor += 1
        return n
    
    t = int(input("Enter number of test cases: "))
    for _ in range(t):
        n = int(input())
        print(largest_prime_factor(n))
    
  • + 0 comments

    Can anyone pleasee help me find the 5th Testcase issue or an error in my code? That is the only one failing:

    fun main(args: Array<String>) {
        val t = readLine()!!.trim().toInt()
    
        for (tItr in 1..t) {
            val n = readLine()!!.trim().toLong()
            getLargePrime(n)
        }
    }
    
    private fun getLargePrime(n: Long){
        /*
            large = 0
            check prime and large than small, assign
        */
        var i = 2L
        var number = n
        var large = 0L
        while (i <= number) {
            if (number % i == 0L) {
                if(large < i) large = i            
                number /= i
                i--
            }
            i++
        }
        println(large)
    }
    
  • + 0 comments

    Below is my code but one hidden test case is still failing, can someone help here?

    import java.util.*;

    public class Solution {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();
        for(int a = 0; a < t; a++){
            long n = sc.nextLong();
            boolean isPrime=checkPrime(n);
            if(!isPrime){
                HighestPrimeFactors(n);}
            else
            {System.out.println(n);}
        }
        sc.close();
    }
     public static boolean checkPrime(long num){
    int count=0;
    for(long i=1;i<=num;i++){
            if(num%i==0){    
             count++; 
            }            
        }
     if(count==2)    
     return true;
     else
     return false;   
    }
    
    public static void HighestPrimeFactors(long n){ 
        long max=0;   
        for(long i=1;i<n;i++){
            if(n%i==0){               
             if(checkPrime(i)==true && max<i){
                max=i;
             }
            }
        }
     System.out.println(max);   
    }
    

    }

  • + 0 comments

    i passed 3 test cases but failed to pass last 2 test case.Can anybody help me out?

    handle,"%d",a0 = 0; t; Extra open brace or missing close bracehandle,"%ld",prime=array(); for(i<=i++){ if(i==0){ array_push(i); } } prime as Extra close brace or missing open braceparr)); echo "\n"; }

    ?>****

  • + 0 comments
    for a0 in range(t):
        n = int(input().strip())
        
        i = 2
        while i*i <= n:
            if n%i == 0:
                n //= i
            else:
                i += 1
        print(n)