to print following pattern
1 回表示 (過去 30 日間)
古いコメントを表示
1 x 8 + 1 = 9
12 x 8 + 2 = 98
123 x 8 + 3 = 987
1234 x 8 + 4 = 9876
12345 x 8 + 5 = 98765
123456 x 8 + 6 = 987654
1234567 x 8 + 7 = 9876543
12345678 x 8 + 8 = 98765432
123456789 x 8 + 9 = 987654321
採用された回答
Rik
2019 年 5 月 22 日
You mean like this?
for k=1:9
a=str2double(char('0'+(1:k)));
b=a*8+k;
fprintf('%d x 8 + %d = %d\n',a,k,b)
end
0 件のコメント
その他の回答 (2 件)
Stephen23
2019 年 5 月 22 日
編集済み: Stephen23
2019 年 5 月 22 日
Three lines, no loop, and only basic numeric operations used:
>> X = 1:9;
>> Y = floor((137174210/1111111111)*10.^X);
>> fprintf('%d x 8 + %d = %d\n',[Y;X;Y.*8+X])
1 x 8 + 1 = 9
12 x 8 + 2 = 98
123 x 8 + 3 = 987
1234 x 8 + 4 = 9876
12345 x 8 + 5 = 98765
123456 x 8 + 6 = 987654
1234567 x 8 + 7 = 9876543
12345678 x 8 + 8 = 98765432
123456789 x 8 + 9 = 987654321
See also: https://oeis.org/A057137
1 件のコメント
KALYAN ACHARJYA
2019 年 5 月 22 日
編集済み: KALYAN ACHARJYA
2019 年 5 月 22 日
Today I lerned this polyval function, thats why I have posted 2nd answer, although the correct answer already given by @Rik
for i=1:9
value=polyval((1:i),10);
result=value*8+i;
fprintf('\n %d x 8 + %d = %d',value,i,result);
end
1 x 8 + 1 = 9
12 x 8 + 2 = 98
123 x 8 + 3 = 987
1234 x 8 + 4 = 9876
12345 x 8 + 5 = 98765
123456 x 8 + 6 = 987654
1234567 x 8 + 7 = 9876543
12345678 x 8 + 8 = 98765432
123456789 x 8 + 9 = 987654321
2 件のコメント
Rik
2019 年 5 月 22 日
That's nice lateral thinking with polyval. Note that our answers return different answers after 9:
%my answer:
NaN x 8 + 10 = NaN
%polyval:
1234567900 x 8 + 10 = 9876543210
That is caused by my trick with char. If you avoid that, you can also generate this:
12345678910 x 8 + 10 = 98765431290
%replace this:
a=str2double(char('0'+(1:k)));
%with this:
c=cell2mat(cellfun(@num2str,num2cell(1:k),'Uni',0));
a=str2double(c);
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!