Skip to main content

Posts

Showing posts from September, 2015

Check the given number is amstrong number using Matlab

Problem Write a program in matlab to check whether the given number is Amstrong or not. Solution An Armstrong number of three digits is an integer such that the sum of the cubes of its digits is equal to the number itself. For example, 371 is an Armstrong number since 3**3 + 7**3 + 1**3 = 371. To know more about Amstrong number visit the references below. To understand this program you need to know the following: Function definition in Matlab For loop in matlab num2str function in matlab length function in matlab str2num function in matlab logical function in matlab Find the explanation of the program below the program section Program function answer = isamstrong(n) %this function will give the result if the number is amstrong or not. %First convert the number to a string to split and check for the condition. n = num2str(n); %Create a sum variable to find the sum of the cubes of each digit in the %number and see if the number is equal to the given number or n...

Find the number of divisors of a given number using matlab

This post is to link all the methods for solving this problem, but here I will not write the solution but I will explain the mathematics behind the each method. Also I will provide you with the link to the solution which I have already written under each method I will state. Here it should be noted that the time mentioned at the end of this post is what I have observed on my PC and can be seen If you will execute the function for very long numbers. Method 1 This method uses the simple technique of start from 1 and divide the given number, next 2 divide the number, and so on till the given number is reached. In the above looping if the number and the iterator number is divisible then increase the counter with 1 and after the complete execution of the program then return the counter value. This counter value will give you the number of divisors of the given number. You can see the program for method 1 here: Function to find the number of divisors of a given number using matlab Meth...

Problem 12 Projecteuler.net Matlab Highly divisible triangle number Solution

Program - projecteuler.net Write a program in matlab on the following question The sequence of triangle numbers is generated by adding the natural numbers. So the 7 th  triangle number would be 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28. The first ten terms would be: 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ... Let us list the factors of the first seven triangle numbers:  1 : 1   3 : 1,3   6 : 1,2,3,6 10 : 1,2,5,10 15 : 1,3,5,15 21 : 1,3,7,21 28 : 1,2,4,7,14,28 We can see that 28 is the first triangle number to have over five divisors. What is the value of the first triangle number to have over five hundred divisors? Solution In this program we will directly use the functions which we have defined earlier. For this program we will have to generate triangle numbers, and number of divisors of a given number to accomplish the task. But I have written functions for number of divisors in three different methods. Here I will use the second method because the result...

Generate the number of divisors of a given number using matlab function method 3

Program Write a function in matlab to generate the number of divisors of a given number using matlab? Solution To find the number of divisors of a given number, the mathematics is to find the powers of all the prime numbers and then apply this formula. If a number n can be written as p1 q1 xp2 q2 xp3 q3 ...... then number of divisors of the number is (q1+1)(q2+1)(q3+1)+.......... Here we will modify the previously defined function: Function to find the exponent of each prime factor of a given number in Matlab . to get our result. Program function divisors = numberofdivisors3(n) %this function will calculate the number of divisors of the number vec = factor(n); highest = vec(length(vec)); divisors = 1 ; for i = 1 :highest if isprime(i) == 1 divisors = divisors*(nnz(vec == i)+ 1 ); end end To download this program in matlab file format click here: numberofdivisors3.m Explanation The explanation is same as that of the post which we have...

Find the power of each factor in a given number using matlab function

Problem Write a function in matlab to find the exponent of each prime factor of a given number. For example if the input is 28 then the output should be Power of 2 in the number 28 is 2 Power of 3 in the number 28 is 0 Power of 5 in the number 28 is 0 Power of 7 in the number 28 is 1 Solution The solution is simple and one has to know the function definition, factor function, for loop, nnz function, isprime function, and the if statement to understand the program. Don't worry if you don't know the function definition. You can even program this code without a function. The difference will be that there will not be a function definition line, and the value of n is directly defined instead. But it would be useful if you know them. If you want to learn any of the above then head on to the links below. 1)   Function definition in matlab 2) Factor function in matlab 3) For loop in matlab 4) nnz function in matlab(Number of Non Zero matrix elements)...

Generate the number of divisors of a given number using matlab functions method 2

See the method from here: Function to generate the number of divisors of a given number using matlab Please note that the method 1 is slower than the method 2 for large numbers. See the explanation section to know why this is happening. Problem Write a function in matlab to generate the number of divisors of a given number. For example if the input is 28 then the value should be 6. Solution To understand this program you have to first understand the mathematics behind using the for loop till the square root of the number. For this let us consider the number 28. I will start writing this number from 1. 28 = 1x28 = 2x14 = 4x7 So the divisors are 1,2,4,7,14,28 and they can be written in pairs as (1,28),(2,14),(4,7) or if we will divide the number less than its square root then we will get all the divisors. So if a number below the square root divides the number(28) then it will correspond to an another number (for example the number 28 is divisible by 1 and thus it also ...

Find the number of divisors of a given number using matlab using function Method 1

Problem Write a function in matlab to find the number of divisors of a given number using matlab. Solution In this problem we will use function definition, for loop, rem function, if statement. If you don't know know any of these then please head on to the links below to learn these: 1) Function definition in Matlab 2) For loop in Matlab 3) Rem function in Matlab(Remainder after division) 4) If statement in Matlab Program function result = numberofdivisors(n) %This function will return the number of factors of a given number result = 0 ; for i = 1 :n if rem(n,i) == 0 result = result+ 1 ; end end Download the matlab format file of the above code from here: numberofdivisors.m Explanation We will directly start with the iterations of the for loop in order to explain this function. Let us assume that the input number for the function is 28. i.e. numberofdivisors(28) First iteration During the first iteration, the value of i is i...

Generate factors for a given number using matlab with function

Problem Write a function to generate factors for a given number using matlab. For example if the input is 28 then the output should be [1 2 4 7 14 28]. Solution Before I will start with the solution, when I have googled for whether matlab has an inbuilt function in it to generate factors I have found that there is a function, but when I have tried it in my matlab it showed an error. This may be due to the difference in the version. However, whether it is inbuilt or not understanding the algorithm, or writing your own program rather than using the inbuilt function(if they are not available) is fun and will also improve your programming skills. Going back to the solution In this program we have used the for loop, rem function and the if statement to accomplish the program successfully. If you don't know any of these then please read from below links: 1)  For loop in matlab 2)  Rem function in matlab(Remainder after division) 3)  if statement in matlab You can f...

Generate triangle numbers using matlab function

Problem To get the n th triangle number then it will be 1+2+3+4+5+......+n, so we can simply say that the sum of natural numbers until a given number will give the triangle number. To know more about triangle numbers please visit wikipedia.com from here:  https://en.wikipedia.org/wiki/Triangular_number Generate triangle numbers upto the given number n. For example if the function is trianglenumbers(n), then trianglenumbers(5) should be [1 3 6 10 15]. Solve using matlab Solution Here we have used the for loop to loop through the n numbers to find their sum. Please read about the for loop from the link given if you don't know what for loop is, or how to for loop in matlab For loop in matlab Find the explanation for the program below the program section Program function result = trianglenumbers(n) %This function using matlab will generate triangle numbers %Triangle numbers can simply be called as the sum of the natural numbers %So the nth trianlge number is 1+2+3+...

Problem 10 projecteuler.net matlab Summation of primes - solution

Problem The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17. Find the sum of all the primes below two million. Solve using Matlab. Solution In this program we have used the for loop, if statement, isprime function, logical function, num2str function which are in built in matlab. Please head on to the below links if you don't know any of them: 1) For loop in matlab 2) If Statement in matlab 3) isprime function in matlab 4) logical function in matlab 5) num2str function in matlab Find the explanation of this program below Program sum = 0 ; for i = 1 : 2000000 if isprime(i) == logical( 1 ) sum = sum+i; end end num2str(sum) Download this program from here: sum_of_primes.m Explanation During the start of this program we have initiated the variable sum equal to 0. Now a for loop is started in the range of 1 to 2000000(2 million). Let us understand this problem by going through iterations. First iteration In the first iter...

Problem9 projecteuler.net Matlab Special Pythagorean triplet solution

Problem: A Pythagorean triplet is a set of three natural numbers,  a   <   b   <   c , for which, a 2   +   b 2   =   c 2 For example, 3 2   + 4 2   = 9 + 16 = 25 = 5 2 . There exists exactly one Pythagorean triplet for which   a   +   b   +   c   = 1000. Find the product   abc . Solve using matlab? Solution Here we have used the for loops in the matlab and the if condition to check the given condition. If you don't know what for loop is and what if statement is then click here: For loop in Matlab If statement in Matlab Program sol = 1 ; for i = 1 : 1000 for j = 1 : 1000 for k = 1 : 1000 if i^ 2 +j^ 2 == k^ 2 if (i+j+k) == 1000 sol = i*j*k end end end end end Download the above program from here: spytriplet.m Explanation The condition...

Problem 7 projecteuler.net Matlab 10001st prime

Problem By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13. What is the 10 001st prime number? Solve using Matlab? Solution Here we will use the isprime function built in matlab . The program is simple. the explanation is given below the program.  Program function j = nprime(n) %This function will give the value of the nth prime number. counter = 0 ; i = 2 ; while (counter < n) if (isprime(i) == logical( 1 )) counter = counter+ 1 ; j = i; end i = i+ 1 ; end Get the matlab file of the above function from here: nprime.m Explanation Here we will use two variables counter and i. Counter is used to count the index or the place or the rank of the current prime number during iteration, when compared to the rank of 2(rank is 1 for 2), and i is used to iterate through the numbers from 2 to some unknown number(This is the nth prime number). We can make it easy to understand b...

Problem 6 projecteuler.net Matlab Sum square difference

Problem: The sum of the squares of the first ten natural numbers is, 1 2  + 2 2  + ... + 10 2  = 385 The square of the sum of the first ten natural numbers is, (1 + 2 + ... + 10) 2  = 55 2  = 3025 Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is 3025 − 385 = 2640. Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum. Solution The solution is very simple and here we have used functions from our previous posts: sum_of_numbers.m and sum_of_squares.m Please have a look at these two posts, so that it will be very easy to understand the below problem: Function to generate the sum of natural numbers from 1 to n using Matlab Function to generate sum of squares of numbers with matlab Program >> ssd = sum_of_numbers( 100 )^ 2 - sum_of_squares( 100 ) ans = 25164150 The above problem has been taken ...

Generate using matlab sum of natural numbers from 1 to n using functions

Problem Write a function in matlab which will find the sum of n natural numbers. Solution The solution is as simple as just generating the for loop and adding all the numbers generated by the for loop. Find the Explanation below Program function answer = sum_of_numbers(n) %This function will calculate the sum of n numbers %For Example %1+2+3+4+5+6+......+n %A variable to store the value of the sum answer = 0 ; for i = 1 :n answer = answer+i; end Explanation: In this function the only part which a few people won't understand is after the initiation of the answer variable. After the initiation of the answer variable, the for loop is started and now let us examine through the iteration to make it easy for understanding. First iteration Now as soon as the for loop starts, the value of i is 1, now the answer variable is changed from previous value( 0 ) to 0(previous value)+1(value of the iterator). Now the new value for the answer variable is 1. ...

Generate sum of squares of numbers with matlab

Problem: Write a function in matlab which gives the sum of squares of numbers until the numbers given. For example the input is 5 then the answer should be (1^2+2^2+3^2+4^2+5^2). Solution: The solution is quite simple and you can easily understand by seeing the program. For explanation see it below the program section. Program: function answer = sum_of_squares(n) %this function will calculate the sum of squares %of n numbers %for Example if number is 5 then the function will calculate %1^2+2^2+3^2+4^2+5^2 %Variable to store the value of the sum answer = 0 ; for i = 1 :n answer = answer+i^ 2 ; end Download the above code in matlab file format from here: sum_of_squares.m Explanation: In this function we will loop through the numbers from 1 to n and we will square them and add them all to get the result. First we will create a variable answer to store the value of the sum of the squares generated in each iteration. We will equate the answer variab...

Problem 5 Projecteuler.net Matlab Smallest Factor

Problem5-projecteuler.net 2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder. What is the smallest positive number that is  evenly divisible  by all of the numbers from 1 to 20? Solution The solution for this problem is very simple. I have thought of the solution using prime factorization but my program has become so big which is not really necessary for solving this problem and I was unable to make it small using the looping system. If anyone can write a program using the prime factorization technique you are welcome and please post your solution in the comment below so that others can get benefited, Finally I have found a solution on a website written in c#, and I have converted that solution to Matlab. Solution 2 First of all Thanks to Olubukola Ogunsola , who has commented/suggested/requested this solution. Before we continue with the algorithm, remember that in the question you are asked to find the Le...

Projecteuler.net: Problem 4: Matlab: Largest Palindrome Product

Problem4 projecteuler.net A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99. Find the largest palindrome made from the product of two 3-digit numbers. Solve using Matlab? Solution Here to find the solution we will generate number which are product of three digit number and will check if the number is palindrome or not. Finally we will find the largest number of the given set and that will be the solution. If you don't know the ispalindrome function then go to our previous post on what is palindrome and the palindrome function to get a good idea to solve this problem. Click here to go to the post:  Function to check if the number is palindrome or not MATLAB Download the ispalindrome.m matlab file from here: ispalindrome.m Note that the explanation for the program is below the program section, this program will take a minute or two to generate the answer, because we will have...

Check if the number is palindrome using matlab

Problem:  Write a function in Matlab to check whether the given number is palindrome or not? Solution:  Here we will create a function in MATLAB which will check whether the number is palindrome or not. What is palindrome? A palindrome is a number which when reversed will give the original number. Example: Consider 1221, if it is reversed then the number is again 1221. Consider 1234, if the number is reversed then the number is 4321 which is not the original number 1234 and hence 1234 is not a palindrome number. To know more about palindrome numbers click here: Wikipedia: Palindrome Note that the explanation for the program in below the program section. Program function tf = ispalindrome(n) %this function will check whether the given number is %palindrome or not %Fist we will assume that the given number is palindrome tf = true; %Convert the number to string so that we can use the number as %a vector n = num2str(n); %Now we have to find ...