# Stop Computation - Is Exit[] overkill?

The following example either finds a factor of the number $n$ or returns a message saying that the number in question is prime (I specifially wrote this to demonstrate my question below).

n = 125;
For[i = 2, i <= Sqrt[n], i++,
If[Mod[n, i] == 0, Print["Factor found: ", i]; Break[];]
]
If[i <= Sqrt[n], Exit[];]
Print[n, " is prime."]
...


I know I could simply include the last Print statement in the if-condition, but I am specifically looking for a command that makes Mathematica skip the last Print statement (and all other statements that might come thereafter...). I tried Break, Abort and Interrupt, but none have the desired effect.

Now the commands Exit and Quit work, but they terminate the entire Mathematica kernel, which is a bit of an overkill. How could I bypass this issue? I need something like SkipRemainingCommands.

• For is an overkill. And about Your question, please take a look at Throw.
– Kuba
Commented Jun 20, 2013 at 20:41
• It is good to avoid For, more here: mathematica.stackexchange.com/q/7924/5478
– Kuba
Commented Jun 20, 2013 at 20:44
• Throw doesn't seem to be helping me as it produces additional "error" output. I just want Mathematica to stop computing / skip the remaining commands. Commented Jun 20, 2013 at 20:55
• Tom, you have to Catch the Throw, otherwise you get that error. Commented Jun 20, 2013 at 20:56
• Mod[n, i] == 0 is the same as Divisible[n, i]. I would prefer Scan here over Do. You can exit a Scan using Return. Related Q&A Commented Jun 21, 2013 at 11:11

After comment:

Abort will help You:

(Do[
If[Mod[n, i] == 0, Print["factor found: ", i]; Abort[];]
, {i, 2, Round@Sqrt@n}];
Print[n, " is prime."];)

factor found: 2
$Aborted  I understand this behavior of ( ) rather intuitively so I will wait for someone smarter. I assume this is forcing procedures to create CompoundExpression, which is also created if You avoid line break between Your loop and last Print: Do[ If[Mod[n, i] == 0, Print["factor found: ", i]; Abort[];] , {i, 2, Round@Sqrt@n}]; Print[n, " is prime."];  factor found: 2$Aborted


Before comment:

It might be good to show/try/learn different way to do this.

Goto: This is not elegant and my friend once have told me "do not use Goto in C++", but it works in simple cases:

f[n_] := Module[{},
Do[
If[Mod[n, i] == 0, Print["factor found: ", i]; Goto["End"]]
, {i, 2, Round@Sqrt@n}];
Print[n, " is prime."];
Label["End"];
]
f[15]

factor found: 3


n = 12;
Catch[
Do[
If[Mod[n, i] == 0, Throw@Print["factor found: ", i];]
, {i, 2, Round@Sqrt@n}];
Print[n, " is prime."];
]

factor found: 2

• I really do appreciate the effort, but it is just not what I was looking for. I am aware of Module/Catch/etc. constructions, but I wanted to keep it clean. Hence I was trying to find a simple command that would just stop Mathematica from continuing whatever it was doing. Commented Jun 20, 2013 at 22:10
• @Tom So I have missed the point again :). Please take a look at this edit.
– Kuba
Commented Jun 20, 2013 at 22:37

I agree with using Goto and Label in this situation. We can replace the For with While by testing the greatest factors. Of course, any number mod 1 is 0, which gets us out of the While.

Block[{n = 115, i = Ceiling[Sqrt[n]]},
While[0 != Mod[n, --i]];
If[i != 1, Print["Factor found ", n]; Goto["end"]];
Print[n, " is prime"];
(*...other code...*)
Label["end"];
]


Use Break to get out of the loop, saving the test result for later use:

For[ ....  If[ notprime = (Mod[n, i] == 0) , Break[]] ... ];
If[notprime , Primt["not prime a factor is",i] , Print["Prime" ] ]


( I don't even use goto in Fortran code )