Sunday, 24 September 2023

Solving Project Euler Problem 3: Largest Prime Factor

 Problem

"The prime factors of 13195 are 5, 7, 13, and 29. What is the largest prime factor of the number 600851475143?"

Solution

A prime factor of a number is a prime number that divides the given number without leaving a remainder. In other words, prime factors are the building blocks of a number. For example, let's consider the number 36. Its prime factors are 2 and 3 because:

  • 2 is a prime number, and 36 divided by 2 equals 18 (no remainder).
  • 3 is a prime number, and 36 divided by 3 equals 12 (no remainder).
  • There are no other prime numbers that can divide 36 without leaving a remainder.

Here is a python implementation of this:

 

 

1.      We define a function prime_factor that takes a single argument, n, representing the target number for which we want to find the largest prime factor.

2.      We start with the smallest prime number, 2, as the initial value of the prime variable.

3.      We use a while loop to continue the factorization process as long as prime * prime (a*a) is less than or equal to the current value of number. This condition ensures that we consider prime factors up to the square root of number, which is sufficient.

4.      Inside the loop, we check if the current prime number, a, divides number evenly (i.e., without a remainder). If it does, we update number by dividing it by prime a. This step repeatedly divides number by its smallest prime factors until it becomes a prime number itself.

5.      If prime does not divide number evenly, we increment prime to the next prime number.

6.      When the loop exits, number will contain the largest prime factor of the original target number.

Solving Project Euler Problems provides a great opportunity to practice coding and algorithmic thinking.

Solving Project Euler Problem 2: Even Fibonacci Numbers

 Problem

"Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first ten terms will be:

1, 2, 3, 5, 8, 13, 21, 34, 55, 89 ,...

By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms."

Solution

The Fibonacci sequence is a series of numbers where each number is the sum of the two preceding ones. The sequence typically starts with 0 and 1, resulting in the following sequence:

0, 1, 1, 2, 3, 5, 8, 13, 21, 34 , ...

To generate the next number in the sequence, you simply add the two previous numbers. In mathematical terms:

F (n) = F (n-1) + F (n-2)

Where F (n) represents the nth Fibonacci number, F (n-1) is the (n-1)th Fibonacci number, and F(n-2) is the (n-2)th Fibonacci number.

 

To solve this problem, we will write a Python program that generates Fibonacci numbers and calculates the sum of even numbers up to a given limit.

Here is a python implementation of this:

1.      We define a function Fibonacci_of that takes a single argument, n, which represents the maximum value for Fibonacci numbers.

2.      We initialize a and b with the first two Fibonacci numbers, 1 and 2.

3.      The Sum variable is initialized to zero, which will store the sum of even Fibonacci numbers.

4.      The while loop continues until the current Fibonacci number exceeds the specified limit.

5.      Inside the loop, we check if the current Fibonacci number is even (i.e., divisible by 2) using the modulo operator. If it is, we add it to Sum.

6.      We then update a and b to generate the next Fibonacci number by shifting them one position forward in the sequence.

7.      The loop continues until b exceeds the limit, and after the loop, we return the value of Sum, which contains the sum of even Fibonacci numbers below the limit.

 

 

Solving Project Euler Problems provides a great opportunity to practice coding and algorithmic thinking.

 

 

 

 

Solving Project Euler Problem 1: Multiples of 3 and 5

Problem

"If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6, and 9. The sum of these multiples is 23. Find the sum of all the multiples of 3 or 5 below 1000."

Solution

Here, we need to find and sum all the numbers below 1000 that are divisible by either 3 or 5.

To solve this problem, we can iterate through the numbers from 1 to 999 (since we want numbers below 1000) and checking if each number is divisible by either 3 or 5. If a number is divisible, we add it to a running sum. At the end of the iteration, the sum will contain the answer.

Here is a Python implementation of this:


From the code;

1.      We initialize a variable sum to zero. This variable will store the sum of multiples of 3 or 5.

2.      We use a for loop to iterate through numbers from 1 to 999.

3.      Inside the loop, we use the modulo operator (%) to check if the current number is divisible by 3 or 5. If the remainder of the division is zero, it means the number is a multiple of either 3 or 5, so we add it to the sum.

4.      After the loop completes, we print the final value of sum, which represents the sum of all multiples of 3 or 5 below the given limit.

Solving Project Euler Problems provides a great opportunity to practice coding and algorithmic thinking.

 

 

 

Solving Project Euler Problem 10: Summation of Primes

  Problem "The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17. Find the sum of all the primes below two million." Solution ...