Write a function so that, when given a natural number N, the function returns the value max n ∈ N : 12 + 22 + · · · + n 2 ≤ N
Mostrar comentarios más antiguos
Write a function so that, when given a natural number N, the function returns the value max n ∈ N : 12 + 22 + · · · + n 2 ≤ N
4 comentarios
Walter Roberson
el 17 de En. de 2016
The meaning of "n 2" is not clear in that question.
TIanhang Hu
el 22 de En. de 2017
n2 simply means n^2, how can it be not clear...
Walter Roberson
el 22 de En. de 2017
So the question is about finding the largest n such that the sum of squares of 1 to n is less than or equal to the original number? Hmmm, plausibly.
It would have been easy enough for the poster to have clarified that when asked a year ago.
Respuestas (4)
Andrei Bobrov
el 17 de En. de 2016
your_function = @(n,m,N)sum((n+m):n:N);
use
>> n = 10;
m = 2;
N = 90;
>> your_function(n,m,N)
ans =
376
>>
1 comentario
Vin Sen Lee
el 17 de En. de 2016
Torsten
el 18 de En. de 2016
1 voto
n=floor((-7+sqrt(20*N+49))/10);
Best wishes
Torsten.
Rusty
el 17 de En. de 2016
function [ p ] = rusty( N)
a(1)=12;
i=1;
while sum(a(:))<=N
a(i+1)=a(i)+10;
i=i+1;
end
if sum(a(:))>N
a(end)=[];
end
if isempty(a)
disp('N is less than 12');
return
else
p=(a(end)-2)/10
end
3 comentarios
Vin Sen Lee
el 17 de En. de 2016
Rusty
el 18 de En. de 2016
you just go and save this file. now open the command window and write : rusty(36), if N=36, and you will get the answer
Márcio
el 20 de Oct. de 2022
0 votos
Write a function that for a given natural number nn returns the matrix which has 2 rows, in its first row every entry is 1, while in its second row there are the numbers n,n−1,…,1n,n−1,…,1 (in this order).
1 comentario
N = 3624;
v = N:-1:1;
A = [ones(1,2*N);[v,v]]
Categorías
Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!