Skip to main content
Code Review

Return to Answer

Commonmark migration
Source Link

#Performance

Performance

You can use a Sieve of Eratosthenes to make it faster.

This is a slightly more complicated sieve as it only sieves the odd numbers up to the square root of the input

public long largestPrimeFactor( final long input ) {
 // Initialise a BitSet for the sieve.
 final BitSet sieve = new BitSet( ((int) Math.sqrt( input )) + 1 );
 // Find if there are factors of 2 and eliminate them.
 long i = Long.numberOfTrailingZeros(input);
 long value = input >> i;
 long maxPrime = i > 0 ? 2 : 1;
 // Loop only over the values which remain in the sieve (i.e. the primes)
 for ( int j = 1; (i = (j<<1)+1) * i <= value; j = sieve.nextClearBit( j + 1 ) ){
 sieve.set(j);
 // Check if it is a factor
 if ( value % i == 0 )
 {
 maxPrime = i;
 // Eliminate the factor
 do value /= i; while( value % i == 0 );
 if ( value == 1 )
 break;
 }
 // Iterate through the sieve setting all the multiples
 // of the current value to be non-prime.
 for ( long k = (i*i-1)>>1; k * k <= value; k += i )
 sieve.set( (int) k);
 }
 return value > 1 ? value : maxPrime;
}

For small numbers, using a simple algorithm will be faster but if you (a) can re-use the sieve when finding/testing multiple numbers or (b) are using it for large numbers; then a sieve is likely to be faster.

#Performance

You can use a Sieve of Eratosthenes to make it faster.

This is a slightly more complicated sieve as it only sieves the odd numbers up to the square root of the input

public long largestPrimeFactor( final long input ) {
 // Initialise a BitSet for the sieve.
 final BitSet sieve = new BitSet( ((int) Math.sqrt( input )) + 1 );
 // Find if there are factors of 2 and eliminate them.
 long i = Long.numberOfTrailingZeros(input);
 long value = input >> i;
 long maxPrime = i > 0 ? 2 : 1;
 // Loop only over the values which remain in the sieve (i.e. the primes)
 for ( int j = 1; (i = (j<<1)+1) * i <= value; j = sieve.nextClearBit( j + 1 ) ){
 sieve.set(j);
 // Check if it is a factor
 if ( value % i == 0 )
 {
 maxPrime = i;
 // Eliminate the factor
 do value /= i; while( value % i == 0 );
 if ( value == 1 )
 break;
 }
 // Iterate through the sieve setting all the multiples
 // of the current value to be non-prime.
 for ( long k = (i*i-1)>>1; k * k <= value; k += i )
 sieve.set( (int) k);
 }
 return value > 1 ? value : maxPrime;
}

For small numbers, using a simple algorithm will be faster but if you (a) can re-use the sieve when finding/testing multiple numbers or (b) are using it for large numbers; then a sieve is likely to be faster.

Performance

You can use a Sieve of Eratosthenes to make it faster.

This is a slightly more complicated sieve as it only sieves the odd numbers up to the square root of the input

public long largestPrimeFactor( final long input ) {
 // Initialise a BitSet for the sieve.
 final BitSet sieve = new BitSet( ((int) Math.sqrt( input )) + 1 );
 // Find if there are factors of 2 and eliminate them.
 long i = Long.numberOfTrailingZeros(input);
 long value = input >> i;
 long maxPrime = i > 0 ? 2 : 1;
 // Loop only over the values which remain in the sieve (i.e. the primes)
 for ( int j = 1; (i = (j<<1)+1) * i <= value; j = sieve.nextClearBit( j + 1 ) ){
 sieve.set(j);
 // Check if it is a factor
 if ( value % i == 0 )
 {
 maxPrime = i;
 // Eliminate the factor
 do value /= i; while( value % i == 0 );
 if ( value == 1 )
 break;
 }
 // Iterate through the sieve setting all the multiples
 // of the current value to be non-prime.
 for ( long k = (i*i-1)>>1; k * k <= value; k += i )
 sieve.set( (int) k);
 }
 return value > 1 ? value : maxPrime;
}

For small numbers, using a simple algorithm will be faster but if you (a) can re-use the sieve when finding/testing multiple numbers or (b) are using it for large numbers; then a sieve is likely to be faster.

Source Link
MT0
  • 1k
  • 5
  • 8

#Performance

You can use a Sieve of Eratosthenes to make it faster.

This is a slightly more complicated sieve as it only sieves the odd numbers up to the square root of the input

public long largestPrimeFactor( final long input ) {
 // Initialise a BitSet for the sieve.
 final BitSet sieve = new BitSet( ((int) Math.sqrt( input )) + 1 );
 // Find if there are factors of 2 and eliminate them.
 long i = Long.numberOfTrailingZeros(input);
 long value = input >> i;
 long maxPrime = i > 0 ? 2 : 1;
 // Loop only over the values which remain in the sieve (i.e. the primes)
 for ( int j = 1; (i = (j<<1)+1) * i <= value; j = sieve.nextClearBit( j + 1 ) ){
 sieve.set(j);
 // Check if it is a factor
 if ( value % i == 0 )
 {
 maxPrime = i;
 // Eliminate the factor
 do value /= i; while( value % i == 0 );
 if ( value == 1 )
 break;
 }
 // Iterate through the sieve setting all the multiples
 // of the current value to be non-prime.
 for ( long k = (i*i-1)>>1; k * k <= value; k += i )
 sieve.set( (int) k);
 }
 return value > 1 ? value : maxPrime;
}

For small numbers, using a simple algorithm will be faster but if you (a) can re-use the sieve when finding/testing multiple numbers or (b) are using it for large numbers; then a sieve is likely to be faster.

lang-java

AltStyle によって変換されたページ (->オリジナル) /