find numbers whose product of digits is 6 times the sum of the digits

18 visualizaciones (últimos 30 días)
find numbers between 100-999, whose product of digits is 6 times the sum of the digits example 347 would be a number return because 3x4x7=6(3+4+7). I need to use a for-end loop to accomplish this. How can I do this?
  4 comentarios
John D'Errico
John D'Errico el 1 de Abr. de 2018
Note that the scheme I outlined is actually simpler than writing three nested loops, but it forces you to perform the step to extract the digits from a 3 digit number. There are always multiple ways to solve a problem. Your choice, since it is your homework.

Iniciar sesión para comentar.

Respuestas (2)

Matt J
Matt J el 1 de Abr. de 2018
Here's a further hint.
>> num2str(347)-'0'
ans =
3 4 7
  3 comentarios
Walter Roberson
Walter Roberson el 7 de Abr. de 2020
is it possible to do this problem without the code num2str( )
Yes! There is no need for num2str() at all if you use nested loops or a vectorized solution.
Steven Lord
Steven Lord el 7 de Abr. de 2020
Sure. One straightforward way would be to use three nested for loops as John D'Errico mentioned in this comment above. There are other ways, at the expense of higher memory usage, to avoid both num2str and for loops.

Iniciar sesión para comentar.


Erass Mirza
Erass Mirza el 29 de Jun. de 2019
for numb = 100:999
numb = num2str(numb)-'0';
if numb(1)*numb(2)*numb(3) == 6*(numb(1)+numb(2)+numb(3))
numb;
x = str2num(char(numb+'0'))
end
end
  2 comentarios
Sudeepta  Banerjee
Sudeepta Banerjee el 6 de Mzo. de 2022
please can anyone explain me this answer ?
Walter Roberson
Walter Roberson el 6 de Mzo. de 2022
num2str of an integer returns a character vector in which the characters are the characters that encode the decimal digits of the value of the integer.
For example num2str of the integer forty-two finds the decimal representation 42 and then returns a vector of characters ['4' '2'] more commonly displayed as '42'
The character '4' is not encoded as the decimal value 4 in any system designed in the last 70 years. It might have been encoded that way in the 5-bit BAUDOT system long ago, which used the same codes for digits and letters and used a "Shift-in" and "Shift-out" code to tell you which range was active. So it might have happened historically, but long ago. And on the systems that did that historically, the digit 0 was encoded as binary ten not as binary zero because binary zero was mostly reserved for "line is idle"
In all systems since that long-ago BAUDOT, the characters to code the digits have not happened to be at the binary values that correspond to their decimal representation, because all of the systems reserved the low binary values as control characters, like idle or carriage return.
Instead, the characters for the decimal digits were, in all systems, allocated a block with the code for '2' always being immediately after the code for '1', and the code for '3' always being immediately after the code for '2' and so on. IBM had an economically important character encoding in which the code for '0' was immediately after the code for '9'.
In the mid to late 1950s, standards bodies formed and standardized on the current practice of encoding '0' then '1' then '2' and so on all as consecutive codes.
When you know that the characters for the digits are consecutive, then it becomes trivial to convert from the character representing a decimal digit, into the equivalent binary value of the decimal digit: just subtract the character code for '0' from the character code for the decimal digit and you get the binary. '4'-'0' subtracts the encoding of character '0' from the encoding of the character '4' and gets binary 4 as the result. It does not matter where in the encoding table that '0' through '9' occur, as long as you know that they are in sequence.
It is very useful to have '0' to '9' be consecutive characters, so it is unlikely that this property of character encoding of digits will change as long as we continue to use decimal. If we eventually switch to base 12 or 20 or 60 as some people have proposed, then the matter would be revisited. (The proposals for base 60 have been serious. Base 60 allows direct lossless encoding of 1/2, 1/3, 1/4, 1/5, 1/6, 1/10, 1/12, 1/15, 1/20, 1/30)

Iniciar sesión para comentar.

Community Treasure Hunt

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

Start Hunting!

Translated by