Write a function called one_per_n that returns the smallest positive integer n for which the sum 1 + 1/2 + 1/3 + … + 1/n , is greater than or equal to x where x is the input argument. Limit the maximum number n of terms in the sum to 10,000 .

2 ビュー (過去 30 日間)
function out=one_per_n(x)
if x>10000
out=-1;
%fprintf('-1\n');
else
total=0;
for n=1:x;
total=(total+(1/n));
end
if total>=x
out=total;
%fprintf('%d\n',total);
end
end
  1 件のコメント
Adam
Adam 2015 年 5 月 22 日
What exactly is your question? Just posting some code isn't very useful even if you do also paste a homework question into the title.

サインインしてコメントする。

採用された回答

Walter Roberson
Walter Roberson 2015 年 5 月 22 日
Example:
for K = 1 : 50 %maximum 50 times
t = f(K); %some calculation dependent on the counter
if t >= SomeLimit %did we reach the limit?
break; %Yes! Stop this loop!
end
end
%now that we got here you need to figure out if you matched what you wanted to match, or if you ran out of iterations
%left as an exercise for the reader
  4 件のコメント
Christos Vyzantios
Christos Vyzantios 2015 年 5 月 31 日
編集済み: Walter Roberson 2015 年 6 月 1 日
The solution seems ok but the greater saw us the message:
Problem 5 (one_per_n):
Feedback: Your function performed correctly for argument(s) 1
Feedback: Your function performed correctly for argument(s) 2
Feedback: Your function performed correctly for argument(s) 3
Feedback: Your function performed correctly for argument(s) 4
Feedback: Your function performed correctly for argument(s) 8
Feedback: Your function performed correctly for argument(s) 9
Feedback: Your function performed correctly for argument(s) 9.7875
Feedback: Your function performed correctly for argument(s) 9.7876
Feedback: Your function made an error for argument(s) 9.7877
Your solution is _not_ correct.
Walter Roberson
Walter Roberson 2015 年 6 月 1 日
The problem statement from the title does not define what should happen when 10000 terms is not enough to reach the desired total.

サインインしてコメントする。

その他の回答 (4 件)

Chen Li
Chen Li 2017 年 3 月 17 日
編集済み: Chen Li 2017 年 3 月 17 日
function d = holiday(month,day)
if month == 1 && day == 1
d = true;
elseif month == 7 && day == 4
d = true;
elseif month == 12 && day == 25
d = true;
elseif month == 12 && day == 31
d = true;
else
d = false;
end
This is the code that is based on what you wrote. Now it works!

Jean Araujo
Jean Araujo 2015 年 6 月 1 日
function out = one_per_n(x)
total=0;
n = 1;
while total < x
total = (total+1/n);
n = n+1;
end
if n-1 > 10000
out = -1;
else
out = (n-1);
end
  2 件のコメント
Christos Vyzantios
Christos Vyzantios 2015 年 6 月 1 日
Yes it is accepted answer. I had tried as out = n-1 only but it didn'nt work...THANX!!!
Walter Roberson
Walter Roberson 2015 年 6 月 2 日
Returning -1 was definitely not in the problem statement from the title of this Question!

サインインしてコメントする。


Nava  Subedi
Nava Subedi 2016 年 11 月 28 日
編集済み: Walter Roberson 2016 年 11 月 28 日
I tried to solve the question.
Write a function called holiday that takes two input arguments called month and day; both are scalar integers representing a month (1-12) and a day (1-31). The function returns a logical true if the specified date is a holiday; if not, it returns false. For the purposes of this exercise, the following dates are considered holidays: January 1st, July 4th, December 25th, and December 31st.
I wrote:
function d = holiday(month, day)
if (month >= 1 && month <=12) && (day >= 1 && day <= 31)
if month == 1 && day == 1
d = 1
fprintf('%d is true! It is a holoday. \n', d);
end
if month == 7 && day == 4
d = 1
fprintf('%d is true! It is a holoday. \n', d);
end
if month == 12 && day == 25
d = 1
fprintf('%d is true! It is a holoday. \n', d);
end
if month == 12 && day == 31
d = 1
fprintf('%d is true! It is a holoday. \n', d);
end
else
d = 0
fprintf('%d is false! It is not a holoday. \n', d);
return
end
end
But did not work. Where did I do mistake??
  1 件のコメント
Walter Roberson
Walter Roberson 2016 年 11 月 28 日
The assignment does not request any printing.
The assignment requires logical true and logical false as outputs, not double precision 1 and double precision 0. Logical true is represented as true and logical false is represented as false

サインインしてコメントする。


Jorge Briceño
Jorge Briceño 2018 年 1 月 29 日
Hello Nava,
Maybe you could also try to simplify your code. Here is my example:
function out = holiday (m, d)
if (m==1 && d==1) || (m==7 && d==4)|| (m==12 && d==25)...
|| (m==12 && d==31)
out=true;
else
out=false;
end
return;
end

カテゴリ

Help Center および File ExchangeCalendar についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by