Skip to main content

Posts

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...