re: Project Euler #3 - Largest Prime Factor VIEW POST

FULL DISCUSSION
 

PHP;

<?php
/**
 * @Author: Erhan Kılıç
 * @Website: http://erhankilic.org
 * @Problem: The prime factors of 13195 are 5, 7, 13 and 29.
 * What is the largest prime factor of the number 600851475143 ?
 * https://projecteuler.net/problem=3
 */
class ProblemSolver
{
    private $original_number;
    private $number;
    private $current_prime_number = 2;
    private $prime_numbers = [];
    private $largest_prime_number;
    /**
     * ProblemSolver constructor.
     * @param int $number
     */
    public function __construct(int $number)
    {
        $this->number = $this->original_number = $number;
    }
    /**
     * Finds the next prime number
     */
    private function find_next_prime_number()
    {
        if ($this->current_prime_number == 2) {
            $this->current_prime_number++;
        } else {
            $this->current_prime_number += 2;
        }
        $can_divide = false;
        $number = 2;
        while ($number < $this->current_prime_number) {
            if ($this->current_prime_number % $number == 0) {
                $can_divide = true;
            }
            $number++;
        }
        if ($can_divide) {
            $this->find_next_prime_number();
        }
    }
    /**
     * Finds the prime factors and largest prime factor of given number
     */
    public function find_prime_factors()
    {
        while ($this->number > 0) {
            if ($this->number == 1) {
                $this->largest_prime_number = $this->current_prime_number;
                break;
            } else {
                if ($this->number % $this->current_prime_number == 0) {
                    $this->prime_numbers[] = $this->current_prime_number;
                    $this->number /= $this->current_prime_number;
                } else {
                    $this->find_next_prime_number();
                }
            }
        }
        echo $this->largest_prime_number;
    }
}
$solver = new ProblemSolver(600851475143);
$solver->find_prime_factors();

Javascript;

'use strict';

/**
 * @Author: Erhan Kılıç
 * @Website: http://erhankilic.org
 * @Problem: The prime factors of 13195 are 5, 7, 13 and 29.
 * What is the largest prime factor of the number 600851475143 ?
 * https://projecteuler.net/problem=3
 */

var number = 600851475143, prime_number = 2;

function find_next_prime_number() {
    var can_divide = false;
    var n = 2;
    if (prime_number === 2) {
        prime_number++;
    } else {
        prime_number += 2;
    }
    while (n < prime_number) {
        if (prime_number % n === 0) {
            can_divide = true;
        }
        n++;
    }
    if (can_divide) {
        find_next_prime_number();
    }
}

while (number > 1) {
    if (number % prime_number === 0) {
        number /= prime_number;
    } else {
        find_next_prime_number();
    }
}
console.log(prime_number);
 

Author: Brewyn
Largest Prime Factor

Javascript

const isPrime = ( value ) => {
  for( let i = 2; i < value; i++ )
    if( value % i === 0 )
      return false;
  return true;
};

const LargestPrimeFactor = ( value ) => {
  let result = [];  
  for( let i = 2; i < value; i++ )
    if( isPrime( i ) )
      if( value % i === 0 )
        result.push( i );
  return result;
};
console.log( LargestPrimeFactor( 600851475143 ) );

PHP


function isPrime( $value ) {
  for( $i = 2; $i < $value; $i++ )
    if( $value % $i == 0 )
      return false;
  return true;
};

function LargestPrimeFactor ( $value ) {
  $result = [];  
  for( $i = 2; $i < $value; $i++ )
    if( isPrime( $i ) )
      if( $value % $i == 0 )
        array_push( $result, $i );
  return $result; 
};

print_r( LargestPrimeFactor( 13195 ) ); 
//Array( 
//  [0] => 5 
//  [1] => 7 
//  [2] => 13 
//  [3] => 29 
//)

Python

def isPrime( value ):
  for i in range( 2, value ):
    if value % i == 0:
      return False
  return True

def LargestPrimeFactor ( value ):
  result = []
  for i in range( 2, value ):
    if isPrime( i ):
      if value % i == 0:
        result.append( i )
  return result

print( LargestPrimeFactor( 600851475143 ) ) 

Java

import java.util.Arrays;

class Main {
  public static void main(String[] args) {
    
    class LargestPrimeFactor {
      public LargestPrimeFactor( int value ) {
        System.out.println(Arrays.toString(this.LargestPrimeFactor(value)));
      }

      boolean isPrime(int value) {
        for(int i = 2; i < value; i++) {
          if(value % i == 0) {
            return false;
          }
        }
        return true;
      }
     
      int[] LargestPrimeFactor(int value) { 
        int result[] = new int[4];
        int counter = 0;
        for( int i = 2; i < value; i++ ) {
          if( this.isPrime(i) ) {
            if(value % i == 0){
              result[counter] = i;
              counter++;
            }
          }
        }

        return result;
      }
    }
    LargestPrimeFactor myObject = new LargestPrimeFactor(600851475143);
    // [ 5, 7, 13, 29 ]
  } 
}
code of conduct - report abuse