Skip to main content

Posts

Showing posts from October, 2015

Problem 16: projecteuler.net: Matlab: Power digit sum

Problem 2 15  = 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26. What is the sum of the digits of the number 2 1000 ? Solve this problem using Matlab? Solution This problem is very simple. I suggest you first go through the program and if you cannot understand any part then please go through the explanation section and if you still cannot understand then once go through the links below: 1) tic, toe 2) sym function 3) char function 4) for loop in Matlab 5) str2num function in Matlab 6) disp in Matlab Program tic; y = sym( 2 ^ 1000 ); y = char(y); sum = 0 ; for i = 1 : length (y) sum = sum + str2num(y( i )); end disp (sum); toc; You can download the above program from here:  problem_16.m Explanation First I have converted the value of 2 100 as a sym using the symbolic toolbox , because if I log the value, then the value of the output is rounded off and an approximate value is given and not a exact value. Next I have converted ...

Problem 14: projecteuler.net: Matlab: Largest Collatz Sequence

Problem The following iterative sequence is defined for the set of positive integers: n   →   n /2 ( n   is even) n  → 3 n  + 1 ( n  is odd) 13 → 40 → 20 → 10 → 5 → 16 → 8 → 4 → 2 → 1 Using the rule above and starting with 13, we generate the following sequence: It can be seen that this sequence (starting at 13 and finishing at 1) contains 10 terms. Although it has not been proved yet (Collatz Problem), it is thought that all starting numbers finish at 1. Which starting number, under one million, produces the longest chain? NOTE:  Once the chain starts the terms are allowed to go above one million. Solve the above problem using Matlab Solution Collatz sequence has already been defined in the problem section. If you want to know more about collatz sequence then click on the links below: 1) Collatz Conjecture 2) Collatz sequence The solution is simple and we will only use the basics to solve this problem. Please have a loo...

Factorial of a number using Matlab

Write a program to find the factorial of a given number using Matlab. Use the function name as facto(x), where x is the number that the user gives the input to find the factorial of ‘x’. Solution The above question was asked to find the factorial of a given number x. So what is a factorial of a number? That means we have to find x! = x(x-1)(x-2)……….(3)(2)(1) Where, x! is the mathematical representation of x-factorial. Let us consider the number, user gives input as 5 then we have to find 5! = 5(5-1)(5-2)(5-3)(5-4) 5! = 5(4)(3)(2)(1) 5! = 120 Note: It should be remembered that the value of 0! (0-factorial) is 1 and the range of x is in I + ( Positive Integers ). The above was a brief introduction about factorial. To know more about factorial then please visit: 1) Factorial of a number - Wikipedia 2) Factorial function! The solution to the program is very simple. I have already added few comments to the program so that the program will be easy to unders...

Floyd's Triangle using matlab

Program Write a program in Matlab to generate Floyd's triangle, until the given rows n. Use the function name floyds. Floyds Triangle - What is floyds triangle? Floyd's Triangle is a right angle triangle generally used in computer science. Okay what actually is floyds triangle? The answer is simple. First let us assume that we have to print the numbers of floyd's triangle until row 5, then start with number 1. Now fill the first row with the first number 1. Next the second row with next two numbers 2 and 3, Next the third row with the next three numbers 4, 5 and 6 and so on. It should be observed that we are not doing any math with the numbers but we are just arranging in such a way that the given row number will have the row number of columns, for example row 2 will have 2 columns, row three will have 3 columns and so on. If we arrange the numbers in such a way, for 5 rows, the floyds triangle will be as follows: Floyd's Triangle 1 2 3 4 5 6 ...

Program in matlab to check if the letter is vowel

Problem Write a program in matlab to check if the given letter is vowel or not. Use the function name as isvowel. Solution In this function we will use the switch statement to accomplish the task. This program is only basic, and you can understand it easily. First read the program and then, read the explanation, if you still don't understand then please click the links below to check out each function and syntax in matlab. If you still don't understand then contact me . 1) Matlab functions 2) char function in matlab / Char(Matlab functions) 3) switch case in matlab Program function ret = isvowel(s) %this function will check whether the given letter is vowel or not %If the user by mistake enters a number then we will change it to character %using the char function. Or else also this function will not do any %problem s = char(s); switch s case 'a' ret = true; case 'e' ret = true; case 'i' re...

Program in matlab to check if the given number is even or odd

Program Write a program in matlab to check whether the given number is even or odd. Use the function name isevenorodd. Solution Solution to this example is very simple. Here we will just use very basic concepts to solve this problem. Have a look at the program and if you don't understand any then go to explanation section and if you still don't understand then go to the below links to check the concept( or basics) behind this program. The concepts are as follows: 1) Matlab functions 2) if else in matlab 3) rem function in matlab (remainder after division) 4) fprintf in matlab Program function isevenorodd(n) %This function will check whether the given number is even or odd and %will print even if the number is even and odd if the number is odd. %Here we are using the remainder function with if else condition statement if rem(n, 2 ) == 0 fprintf( 'The number is even\n' ); else fprintf( 'The number is odd\n' ); end To download t...

Program in matlab to add the digits of a number

Problem Write a program in matlab to add the digits of the given number using functions. For example if the input is 523 then the program should give the output as 5 + 2 + 3 = 10 Solution This program in very simple and easy to understand. To understand this program, you will have to know few concepts. First of all look at the program and if you don't understand, go to explanation section under the program section, and if you still don't understand the program then head on to the links given below and read the concept so that you will understand the program easily. The links are as follows: 1) Function in matlab 2) num2str function in matlab 3) length function in matlab 4) for loop in matlab 5) str2num function in matlab 6) string referencing in matlab 7) vectors in matlab Program function res = add_digits(n) %this function will add all the digits of th given number. %for example if the number is 523 then the output of the program will be % 10 = 5 + 2 ...

Find the equivalent resistance of parallel connected resistors using matlab

Problem Write a program in matlab to find the equivalent resistance of the given parallel connected resistors using function and without using function. Solution Here the question was asked to write the program to find the equivalent resistances which are connected in parallel. But here it is not given how many resistors are there, so I will write a program where the computer will be asking the user the value of resistance until the user types some number say(0). This program(s) is(are) simple and are based on very basic concept. The formula behind the parallel connected resistors is: Image Courtesy:  http://images.tutorvista.com/cms/images/83/parallel-resistance-formula.png And hence we will finally find the R p from the above formula. To understand any of these programs you need to know the following: 1) For loop in matlab 2) Varargin in matlab 3) nargin in matlab 4) input function in matlab 5) While loop in matlab Find the explanation for the program bel...