Creating a formula for a question?

Hello Dear Matlab users
I need some HELP.
I saw 'a "one-a-day" journal contest' question about so called 'perfect numbers'. (I don't know exact translation of the term). The question was as following:
The Number 18432 is a number that has a feature of non-zero digit in itself,non-repeating digit, also has the ability to split into their numbers sum and products. Also it's more one digit, positive number (it has a lot). Other examples are 12,432,3168,13248 and they go until the number reaches total 21 piece. Well the question asks:
what is the smallest one, greater than 18432??
I thought that I could write a m.file to answer this question.
I've tried to initiate the m.file as following
if true
A=sym('A%d%d',[1,5],'integer&&positive');
sum(A)
assumptions(A);
end
I know it needs a lot of work, but a general idea might spark some thoughts to reconfigure the necessary function.
Thank you already for your consideration.

6 件のコメント

John D'Errico
John D'Errico 2015 年 9 月 25 日
Huh?????>?
"has the ability to split into their numbers sum and products"
What does this mean? I'm sorry, but that statement carries no mathematical meaning. If you want an any help at all, you need to be FAR more specific. Explain the problem. I'm sorry if English is not your natural language, but if you want help, you need to try a bit harder.
As far as your code goes, I have no idea what you thought MATLAB would do with it.
In general on problems of this sort, you need to do some advance mathematical thinking, as otherwise your code will spend a LOT of time searching, and inefficient code will be useless.
WAT
WAT 2015 年 9 月 25 日
編集済み: WAT 2015 年 9 月 25 日
That part was actually reasonably close to the definition of a perfect number, which is a number that is the sum of its divisors (other than itself).
For example, 6 is divisible by 1,2,3 (and 6) and 6 = 1+2+3
Of course, then the rest of the question doesn't make sense, because the original poster seems to suggest that 18432 is a perfect number which is not true.
kerozi
kerozi 2015 年 9 月 25 日
編集済み: kerozi 2015 年 9 月 25 日
Thank you for your interest on this question. And I am really sorry for the language.
I meant the individual 1-8-4-3-2 numbers. And by "split" I meant divided by the sum of the digits.
So the code may be meaningless but I kindly request a general thinking through the problem. Of course my explanation is SO BAD I ll try harder now...
1*8*4*3*2=192
18432 / 192 = is an integer
1+8+4+3+2=18
18432 / 18 = also is an integer
and this number has no 0 (zero).
So the number I am looking for has not zero either. And by positive number, I mean the result is larger than 0(zero). (these restrictions are not for the result.)
I am looking for the number which has these features and bigger than 18432.
Of course with the code I tried to create an array which has 5 digits and has the property of summing of its elements and so on. But I couldn't even begun.
%A=sym('A%d%d',[1,5],'integer');
%sum(A)
%assumptions(A);
John D'Errico
John D'Errico 2015 年 9 月 25 日
@WAT - Yes, I do know what a perfect number is. But nothing in the question has anything to do with perfect numbers.
kerozi
kerozi 2015 年 9 月 25 日
I assume the perfect number is mathematically related with its divisors. Then the journal I picked up the question was not talking about the perfect numbers. It's something else, but I couldn't translate it in English properly.
Pankaj Kumar Sinha
Pankaj Kumar Sinha 2017 年 5 月 21 日
I also want to this solution

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

 採用された回答

John D'Errico
John D'Errico 2015 年 9 月 25 日
編集済み: John D'Errico 2015 年 9 月 25 日

3 投票

So, you want to find a (nonrepeating) sequence of decimal digits, a vector D taken from the set [1:9], that have the two properties that
1. The product of the digits divides the number when viewed as an integer, thus converted to base 10.
2. The sum of the digits also divides the number when viewed as an integer.
N = (18434:99999)';D = dec2base(N,10) - '0';
ind = find((mod(N,sum(D,2)) == 0) & (mod(N,prod(D,2)) == 0) & all(D > 0,2) & all(diff(D,[],2) ~= 0,2));
N(ind)
ans =
21216
21312
21672
24192
24912
26136
26712
27216
31212
32616
32832
34272
35175
41232
41616
42192
42624
42912
43632
51975
61824
71316
81216
83232
Seems a bit trivial. Unless by non-repeating digit, you mean that no single digit can appear more than once in the entire number. In that case...
ind = find((mod(N,sum(D,2)) == 0) & (mod(N,prod(D,2)) == 0) & all(D > 0,2) & all(diff(D,[],2) ~= 0,2) & all(diff(sort(D,2),[],2) > 0,2))
N(ind)
ans =
61824
Still trivial.

8 件のコメント

kerozi
kerozi 2015 年 9 月 25 日
編集済み: kerozi 2015 年 9 月 25 日
Yes, but with one exception as you put forward that no single digit can appear more than once in the entire number.
Also can you explain that after dec2base() you wrote "- '0';" is that related with char - double conversion? or sth else??
John D'Errico
John D'Errico 2015 年 9 月 25 日
Ok. So what did I do at the end?????? READ MY RESPONSE. COMPLETELY!
If you want to know what something in there does, read the help for that function. Try somethings out! This is how you will learn, not by me giving you everything on a silver platter. In fact, I gave you the complete answer. So why not put in some effort and figure out why it works?
kerozi
kerozi 2015 年 9 月 25 日
Thanks for your kindness in replying this question.
John D'Errico
John D'Errico 2015 年 9 月 25 日
I will say that dec2bin returns a string. And that when you subtract two strings, MATLAB computes the difference of the ascii representation of the characters. So for example, we have the identity:
'1' - '0' == 1
WAT
WAT 2015 年 9 月 25 日
編集済み: WAT 2015 年 9 月 25 日
I'm curious why your first sequence (repeats still allowed) starts at 21216. Shouldn't 18816 work?
I can't run your version because I don't have the memory, but here's code that's pretty much the same thing:
go = 1;
i = 18432;
while (go)
i = i + 1;
digits = dec2base(i,10)-'0';
a = sum(digits);
b = prod(digits);
if (mod(i,a) == 0 && mod(i,b) == 0 && isequal(sort(digits),unique(digits)))
go = 0;
end
end
i
kerozi
kerozi 2015 年 9 月 27 日
Thank you so much for your effort to consider replying the question and enlightining an ongoing study. @WAT.
John D'Errico
John D'Errico 2015 年 9 月 27 日
編集済み: John D'Errico 2015 年 9 月 27 日
@WAT - note the requirement that NO digit is repeated. Depending on the definition of repeated digit, 21216 has no digit that repeats, i.e., is equal to the previous digit. This was my first assumption as to how the word repeated digit was intended.
After I learned that by "no repeated digits" meant that any digit could appear no more than once in the number, then I solved it a second time.
So by the first definition of repeated digit, 18816, has a digit that repeats: 8. There are two 8's in a row. Of course it fails by both definitions, since there are a pair of 8's as well as a pair of 1's.
As far as a looped version goes, while it will succeed, loops are a solution I'd avoid if possible here. The non-looped solution indicates the sheer prevalence of numbers that satisfy the goals. As such, it is a bit of a simple problem. (Project Euler is NOT interested in this idea, lol.) Given that the memory requirements on what I did are so minimal...
N = (18434:99999)';D = dec2base(N,10) - '0';
whos N D
Name Size Bytes Class Attributes
D 81566x5 3262640 double
N 81566x1 652528 double
The largest array I created takes up all of 3 megabytes? If you intend to use MATLAB for any serious purpose, I'd suggest getting some more RAM. Memory is CHEAP after all.
WAT
WAT 2015 年 9 月 28 日
The memory issue turned out to be a typo on my end (apparently "N,sum(D,2)" and "N/sum(D,2)" aren't the same thing). And I missed that your first solution was an attempt at a "nonrepeating" solution, just with a different definition of nonrepeating.
You're obviously right about avoiding loops in MATLAB if at all possible. But I do think that the logic is often easier to follow for people who aren't quite as familiar with using MATLAB.
In fact, this is a great example of why you want to avoid using loops since your code finds the answer significantly faster.

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeHistorical Contests についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by