Tutorial by Examples

Example implementation of a prime factorization algorithm. A prime factorization algorithm will find for a given number n a list of primes, such that if you multiply those primes you get n. The below implementation will add -1 to the list of prime factors in case n < 0. Note that there exists no ...
Please note that by definition 1 is not a prime number. To check if a number n is a prime we should try to find a divisor i of n. If we cannot, then n is a prime. We have found a divisor when n % i == 0 evaluates to true. We only need to try odd numbers, since there's only one even prime, namely 2,...
The Sieve of Eratosthenes generates all the primes from 2 to a given number n. Assume that all numbers (from 2 to n) are prime. Then take the first prime number and removes all of its multiples. Iterate the step 2 for next prime. Continue until all numbers till n have been marked. Pseudocode...

Page 1 of 1