Message Boards Message Boards

5 Replies
2 Total Likes
View groups...
Share this post:

Can somebody help me on building up my LOOP structure?

Posted 10 years ago

Can somebody help me on finalizing my loop structure? I am looking for a loop that will BREAK when I get an integer results and print that result

I tried that one but it does not work Can you improve it?


Thanks for your help


POSTED BY: Louis Piche
5 Replies
Posted 10 years ago


Sir David Reiss, I have to thank you very much for your response. It works as required.

Catch@Do[If[IntegerQ[14291/i], Throw[{i, 14291/i}]], {i, 2, 14291}]

But now, as you specified, It would be better to insert a BREAK as soon as the loop encounters a suitable factor and Print the results. But, when I say PRINT, I would like to be able to Print the results from my printer machine too not only Print on the Wolfram screen.

There is something more I would be pleased. I would like to see and print on the screen and on my printing machine the results of all divisions made into the loop keeping a certain number of decimals after the point (maybe 5 decimals when it is the case). As sample, 14291/2 = 7145.5; 14291/3=4763.66666.


And, by the way, do you know a reference book when I could find out Wolfram programming language practice exercices of all kink?

Waiting for your reply, Thanks so much for your help, ___________________

POSTED BY: Louis Piche

Note that the approach does not actually break when the value is reached (and in the original case illustrated it will never result in a returned value since 107 is not even). The Do loop will go until the end whether or not the value in the first argument of the If statement is satisfied. An approach like this (without a Print statement since you want a returned value rather than a byproduct of the Do loop as a printed statement) will satisfy what you are looking for (using 14291 as a test case and returning both the value of i where it achieves its IntegerQ requirement and the result of that requirement):

Catch@Do[If[IntegerQ[14291/i], Throw[{i, 14291/i}]], {i, 2, 14291}]

And if you actually wanted to go through the whole Do loop and get all instances where the If statement evaluated to True you could use Reap and Sow like this:

Reap@Do[If[IntegerQ[14291/i], Sow[{i, 14291/i}]], {i, 2, 14291}]

Note that the first element of the list that is returned is Null because the Do loop does not return results--it just evaluates its argument.

POSTED BY: David Reiss

You need a right bracket to close the If statement.

Do[If[IntegerQ[107/i], Print[107/i]], {i, 1, 8}]


POSTED BY: Frank Kampas
Posted 10 years ago


Thanks. You are right. I forgot a bracket for the If.

Can you help me on the following (I got this from David Reiss which was a very good response)? I would like to insert a BREAK as soon as a first factor is found out, print on the screen and also print the result from my printing machine.

Catch@Do[If[IntegerQ[14291/i], Throw[{i, 14291/i}]], {i, 2, 14291}]

ALSO, From a new command line, to get all the results of the divisions along with decimals (maximum of 5 decimals) and print on the screen and from my printing machine.

Thanks for your reply, Have a good day

POSTED BY: Louis Piche
Posted 7 years ago

Any can help, Sir, Can you help me on? The input is: Do[If[IntegerQ[121/i], Print[i]],{i,2,11}] The output will be: 11 BUT I would like to get an output such as "No Solution" if I have the following Input Do[If[IntegerQ[121/i], Print[i]],{i,2,7}] I want to print an output : "No Solution" How and where to insert the code? Thanks for your help ____________

POSTED BY: Louis Piche
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
or Discard

Group Abstract Group Abstract