Skip to main content
5 of 5
Commonmark migration

Matlab

a warning: this is primarly math based, so do not expect fancy source code

Note that a = n^2 iff log(a) = log(n)*2 iff log(log(a)) = log(log(n))+log(2). So this function is just finding the zero of the function f(a) = log(log(n))+log(2) - log(log(a)) which obviously is at a = n^2.

function s = g(n)
    f = @(a) log(log(n))+log(2)-log(log(a));
    s = fnzeros(f);
end

Here some other not very creative functions:

Here the program wil sum sum the number 1+2+3+...+n = 1/2 * (n^2+n) twice and substract n, so the result is always n^2

g=@(n)sum(1:n)+sum(1:n)-n

This function creates a n x n matrix of random numbers (between 0 and 1) and then returns the number of elements.

g=@(n)numel(rand(n));

The following functin creates a vandermonde matrix of the vector (0,0,n) and outputs the entry that consists of n^2

function s = g(n)
    a = vander([0,0,n]);
    s = a(3,1)
end

This function creates the inverse of a hilbert matrix of size n where the top left element is always n^2

function s = g(n)
    a = invhilb(n);
    s = a(1);
end
flawr
  • 43.9k
  • 7
  • 104
  • 249