Creating a formula for a question?
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
kerozi
le 25 Sep 2015
Commenté : Pankaj Kumar Sinha
le 21 Mai 2017
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 commentaires
Réponse acceptée
John D'Errico
le 25 Sep 2015
Modifié(e) : John D'Errico
le 25 Sep 2015
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 commentaires
John D'Errico
le 27 Sep 2015
Modifié(e) : John D'Errico
le 27 Sep 2015
@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
le 28 Sep 2015
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.
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Whos dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!