Finding a number with conditions while using for loop
Mostrar comentarios más antiguos
Write a program in a script file that finds the smallest odd integer that is divisible by 13 and whose square root is greater than 120. Use a for-loop in the program. The loop should start from 1 and stop when the number is found. The program prints the message “The required number is:” and then prints the number. Hint: Use rem(n,2)~=0 for finding odd integers, and rem(n,13)==0 for finding the number to be divisible by 13.
I know this is easily solved through other methods but I don't know what to do.
I've rewritten my code many times and don't know what I am doing.
3 comentarios
David Hill
el 1 de Abr. de 2020
Show us what you have done.
Bryan Cordeiro
el 1 de Abr. de 2020
Bryan Cordeiro
el 1 de Abr. de 2020
Respuesta aceptada
Más respuestas (2)
John D'Errico
el 2 de Abr. de 2020
Editada: John D'Errico
el 2 de Abr. de 2020
Why use a loop? ;-)
Lets see, the smallest INTEGER with the desired property is 14404. We can get that as:
n = 120;
>> m = n^2 + mod(-n^2,13)
m =
14404
>> sqrt(m)
ans =
120.01666550942
>> rem(m,13)
ans =
0
But, then I see that you needed to find the smallest ODD integer, and since the smallest such integer is even, we need to find a solution yielding the smallest odd integer. This will work, but it is sort of a kludge:
n = 120;
m = n^2 + mod(-n^2,13);
if rem(m,2) == 0
m = m + 13;
end
m
m =
14417
Well, yes. This is a homework problem.
Hmm. How would I solve it using a loop? After all, you are making a credible effort.
The smallest number that satisfies the listed conditions MUST be one of the integers in the set: [120^2 + (0:(2*13-1))]. So we never need to loop over more than 26 elements beyond 120^2. THINK ABOUT IT! As such, I could set this up as a for loop, over 26 numbers, then breaking out of the loop when we find success.
Or, you could just use a while loop, which requires far less thought.
n = 120;
m = n^2;
while ~isequal(mod(m,[13,2]),[0 1])
m = m + 1;
end
m =
14417
You should see the isequal test reduces two tests into one vectorized test. As well, since we started out at 120^2, we absolutely know that sqrt(m) must be greater than 120.
baiel kurstanbek
el 21 de Dic. de 2021
0 votos
Write a program in a script file that finds the smallest even integer that is divisible by a and by b whose square root is greater than c. Use a loop in the program. The loop should start from 1 and stop when the number is found.
Sample Input: 3
5
7
Sample Output: 60
1 comentario
Image Analyst
el 21 de Dic. de 2021
Editada: Image Analyst
el 21 de Dic. de 2021
@baiel kurstanbek Read this link:
Hint: Look up the functions sqrt(), rem(), mod(), gcd(), break, for, and while. Start a new question (not part of @Bryan Cordeiro's question) with your attempt at code if you still have questions.
Usually divisible means that the ratio of the two numbers is an integer like
output = 60/3
however it looks like you've either made a typo (like 60 should have been 70), or you're allowing absolutely anything since 60 is not an integer multiple of 7
output = 60/7
So in that case (allowing results with fractional parts), any number is divisible by any other number and you don't need to check for divisibility.
Categorías
Más información sobre Programming en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!