Skip to main content

Posts

Projecteuler.net: Problem3: Matlab: Find the largest prime factor

Problem 3 - projecteuler.net The prime factors of 13195 are 5, 7, 13 and 29. What is the largest prime factor of the number 600851475143 ? Solve using MATLAB? Solution:  To solve this problem we will be using two loops namely for loop and the while loop . We will also use the rem( ) function . This program takes a very little time to generate the solution. The program is as follows: Find the explanation below the program Program function prime_factor(n) %this function will find the largest prime factor of a given number for i = 2 : 1000000 while rem(n,i) == 0 n = n/i; fprintf( 'Yay, %d is a factor, now we should test %d \n' ,i, n); if n == 1 || n == i fprintf( '%d\n' ,i); break ; end end end Explanation: First of all the function takes the number as argument. Now a for loop is generated in the range of values 2 to 1000000 (why have I used numbers only till...

Check the given number is prime or not in MATLAB using functions

This function will check whether the number is prime or not. It should be noted that this function is valid for real numbers greater than 2. Some one may ask, why not use the built in isprime function . The simple answer is the isprime function of the MATLAB is useful for numbers which are less than 2^32. If the numbers are greater than 2^32 then this function can be used instead of isprime function . This program is very simple. The program is as follows: function tf = isprim(n) %this function will check whether the number is prime or not tf = true; for i = 2 :n- 1 if rem(n,i) == 0 tf = false; break end end Explanation: In the start of the function a variable 'tf' is created assuming that the number is a prime number. Now a for loop is created to check if the number is divisible by any numbers other than 1 and iteself. If it is divisible then the variable 'tf' becomes False and the loop is breaked. The final result wi...

Problem 2:projecteuler.net: Matlab :find the sum of the even-valued fibonacci terms

Problem :  Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 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. Solve the problem using MATLAB. Solution: To solve this problem we will use the previously created fibon.m program to generate all the even fibonacci numbers whose values are less than 4 million. We will sort out the even fibonacci numbers and finally find the sum of all the even fibonacci numbers. If you have not seen the previous post of the fibon.m function then click here: Fibon function The program is as follows: %This script generates even fibonacci numbers %Here we will use the function fibon which was created earlier %create a vector with fibonacci numbers fib = fibon( 4000000 ); %create an empty vector to store the values of eve fib...

Generate fibonacci numbers in Matlab whose values are less than the given number n using functions

Problem: Generate Fibonacci numbers whose values are less than the given number n. Use the function name 'fibon'. For example fibon(10) should give output as 1 2 3 5 8 Solution : In this function we will generate the fibonacci numbers first by taking the basic numbers in the fibonacci numbers a, b as 0,1 respectively. we will add them to generate the fibonacci numbers using the loop. while we will use the loop the condition is that the value of the sum (generated Fibonacci number)  is less than the given number n. But it can be found that for the last iteration the value of the fibonacci number will be less than the given number n and thus the loop goes on and a fibonacci number is generated which is greater than the given number n. Thus finally we will trim off the last number from our vector. You can understand this easily by seeing the program. Program is as follows: function fib = fibon (n) %this function generates fibonacci numbers %whose values are less th...