I want to know how to separate 2024 into 2 and 0 and 2 and 4. I already know how to do num2str-'0'. However, it cannot be used because there is a gap. Please help me.

4 件のコメント

Voss
Voss 2024 年 3 月 31 日
Where is a gap?
윤호
윤호 2024 年 3 月 31 日
function v = MySecret( y )
v=unique(num2str(factor(y))-'0')
If y=2024, the value is output as -16 1 2 3. But what I want is 1 2 3.
윤호
윤호 2024 年 3 月 31 日
This is a question.
Write a function v = MySecret(y) that returns a vector v that arranges each number that comes out in ascending order when a number y is prime factorized.
Use Examples
>> v = MySecret(2024)
v =
1 2 3
If we sub-factorize 2024 given as an argument, it is 2x2x2x11x23, so the numbers used are 1, 2, and 3.
Voss
Voss 2024 年 3 月 31 日
Sounds like they are asking for numbers (2, 11, 23) not digits (1, 2, 3).

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

 採用された回答

Voss
Voss 2024 年 3 月 31 日

0 投票

function v = MySecret( y )
str = num2str(factor(y));
str(str == ' ') = []; % remove spaces
v = unique(str-'0');

3 件のコメント

윤호
윤호 2024 年 3 月 31 日
Is there anything smaller in size?
I want an extremely small code.
Voss
Voss 2024 年 3 月 31 日
編集済み: Voss 2024 年 3 月 31 日
Fewer lines:
function v = MySecret( y )
str = num2str(factor(y));
v = unique(str(str ~= ' ')-'0');
Fewer lines still:
function v = MySecret( y )
v = unique(strrep(num2str(factor(y)),' ','')-'0');
Voss
Voss 2024 年 3 月 31 日
If they want the prime factors, rather than their digits, then of course it's just:
function v = MySecret( y )
v = unique(factor(y));

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

その他の回答 (0 件)

質問済み:

2024 年 3 月 31 日

編集済み:

2024 年 3 月 31 日

Community Treasure Hunt

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

Start Hunting!

Translated by