Find perfect square numbers, highest, lowest of desired digits using MATLAB program

Find perfect square numbers, highest, lowest of desired digits using MATLAB program.

Using this program prepared using MATLAB M-File program, you can find the highest and lowest perfect square number of your desired digits.

When you run this M file code, you will be asked the number of digits that you want, At this step, just enter the number of digits and wait for the result to be displayed.

While preparing this program, i have not considered the optimum time complexity algorithms, so this program can give results in less time for 6 digits. Though you can get accurate answers for higher digits also, but it will take a long time.

Currently I have tested this set of code in MATLAB Version 7.0 in 2.13 GHz processor and found that till 5 digits, it gives answers instantly. While for 6 digits it takes 2 seconds and for 7 digits it takes 50 seconds.

The M-File code is as follows:

clc
clear all
%program to find the highest and lowest perfect square number of desired digits
n=input('enter the number of digits')
lowlt=10^(n-1);
uplt=(10^n)-1;
j=1;
num=lowlt;
while(num<=uplt)
    whole=sqrt(num);
    natural=fix(whole);
    diff=whole-natural;
    if (diff==0)
        psqr(j)=num;
        psqrt(j)=whole;
        j=j+1;
    end
    num=num+1;
end
fprintf('\n------------------------\n%d digit perfect square\n HIGHEST:%d root:%d\n LOWEST :%d root:%d',n,psqr(j-1),psqrt(j-1),psqr(1),psqrt(1))


If you want to see the perfect squares in the increasing order, then you can add the code given below:

fprintf('\n------------------------\nNumber Square Root\n')
i=1;
while(i<j)
  fprintf('%d   --> %d \n',psqr(i),psqrt(i))
  i=i+1;
end

Note:
This program is best recommended for upto 7 digits computations.

If you want to do computations for more than 7 digits, I recommend using C/C++ compilers, since they can perform fast looping related computations. The algorithm will remain same unless and until you are considering time complexity.

Tip: If you enter higher digits example 8 or more, then it would take large amount of time to process, during which MATLAB appears to be stuck, so you can restart MATLAB after closing it from the task manager by pressing ‘Ctrl+Alt+Del’ keys.

You may be interested in: Matlab program to count the number of perfect squares between 2 numbers

5 comments:

  1. Replies
    1. I don't see any threads, kindly clarify what kinds of threads are being shown.

      Delete
  2. write a program in This function should return the
    // number of perfect squares x such
    // that start <= x <= end.
    // For example:
    // numSquare(1, 5) = 2, since there
    // are two numbers between 1 and 5
    // that are perfect squares: 1, 4.

    ReplyDelete
  3. A square number, sometimes also called a perfect square, is an integer that is the square of an integer;[1] in other words, it is the product of some integer with itself. So, for example, 9 is a square number.Subtracting Mixed Numbers from Whole Numbers

    ReplyDelete

If you liked this blog then Join me at:
Facebook
Twitter