Recortes de codigo / Code Snippets

Tuesday, July 9, 2019

isPrime, Naive Version


1
2
3
4
5
6
7
8
bool isPrime0( int x ) {
  for( int divisor = 2; divisor < x; divisor++ ) {
        if( x % divisor == 0 ) { 
            return false;
        }
  }
  return true;
}


Other entries:
  • isPrime: Naive version 
  • isPrime, Optimization 1: Iterate only until the square root of the number
  • isPrime, Optimization 2: Only divide by odd numbers, until the square root of the number
  • isPrime, Optimization 3: Skip the divisors that are multiple of 2 or 3
  • Keep the previosly generated primes and use them to test the following numbers.
  • isPrime, Recursive.
Posted by Rodrigo at 1:09 AM
Email ThisBlogThis!Share to XShare to FacebookShare to Pinterest

No comments:

Post a Comment

Newer Post Older Post Home
Subscribe to: Post Comments (Atom)

About Me

Rodrigo
View my complete profile

Blog Archive

  • ►  2021 (1)
    • ►  January (1)
  • ►  2020 (4)
    • ►  May (1)
    • ►  March (2)
    • ►  February (1)
  • ▼  2019 (10)
    • ►  August (4)
    • ▼  July (6)
      • Sort java list, lambda comparators
      • Merge 2 sorted lists, merged list has reversed order
      • Merge 2 sorted lists
      • isPrime, Naive Version
      • Fibonacci
      • Decimal a binario
Simple theme. Powered by Blogger.
ko-fi.com/inviterodacoffee
ko-fi.com/inviterodacoffee