Message Boards Message Boards

0
|
6068 Views
|
17 Replies
|
2 Total Likes
View groups...
Share
Share this post:

Insert an "If...Then...Else" function into the following WL code?

Posted 8 years ago

I have the following Wolfram Code Language that helps me to identify an integer. But, I would like to get the message of "NOT AN INTEGER" when the result is false. How can I insert the If...Then...Else function into the following code:

Catch[Do[If[IntegerQ[21/i],Throw[i]],{i,2,7,1}]]

Where do I put the "Then" and the "Else".

Thanks for your help.

POSTED BY: Louis Piche
17 Replies

I don't know what you are trying to achieve exactly. This may help:

Do[Print[{21/i, If[IntegerQ[21/i], "integer", "not an integer"]}], {i,
   2, 7, 1}]
POSTED BY: Gianluca Gorni
Posted 8 years ago

Sir, Madam, Thanks for your help. The code you provided runs well but it is not exactly what I am looking for.

First exercice, I want to transfer the total output (using the exact initial code you have provided) into an excel file. I have used table=........... then Export["ExcelTest.xlsx, table]. The table is created but the data do not transfer. Can you tell me why? Notice the extension of the file is .xlsx (excel 2016) and not .xls

Second exercice, I want the output corresponding only to the results such 3{integer} and 7{integer} BUT if there is not an integer I want the output to be only showing a result such as {no integer at all}. How do you transform your initial code?

Thanks for your help again, _______________

POSTED BY: Louis Piche

Is this what you want? Maybe the Print does not produce exportable data...

Table[ If[Mod[21, i] == 0, {i, "integer"}, {"no integer"}], {i, 2, 7}]
POSTED BY: Hans Dolhaine
Posted 8 years ago

Sir, It is very helpful. I am going to make my tests and if ever I got new difficulties I let you know if you do not mind. Thanks very much again for your help Have a good day LOUIS

POSTED BY: Louis Piche
Posted 8 years ago

Sir, Can you help on the following? I use the below wolfram code which gives an output={3}.

Catch[Do[If[IntegerQ[21/i],Throw[i]],{i,2,7,1}]].

But, If I use instead a number like ,11, I will get no output at all (Wolfram does not show any output at all). I would like to see an output that shows a message that there is no solution such as: output=NO SOLUTION. Is it possible?. Thanks for your reply. Thanks for your time. LOUIS. ___________

POSTED BY: Louis Piche

What exactly do you mean by "But, If I use instead a number like ,11," ?

Where do you use this 11?

I must say, that I do not know "wolfram", I use Mathematica. And I have only a faint idea about what you want to do.

Perhaps you should forget this Catch/Throw stuff and and get a result in any case

If[IntegerQ[#], 21/#, ff] & /@ Table[21/i, {i, 2, 7}]

Here you can get what you want.

Union[If[IntegerQ[#], 21/#, ff] & /@ Table[21/i, {i, 2, 7}]]

Get your result:

Part[Union[If[IntegerQ[#], 21/#, ff] & /@ Table[21/i, {i, 2, 7}]], 1]

And now with what I understand you mean by using the number 11

Part[Union[If[IntegerQ[#], 11/#, ff] & /@ Table[11/i, {i, 2, 7}]], 1]

Of course you could use another symbol than ff. Try the commands step by step and see what happens.

POSTED BY: Hans Dolhaine
Posted 8 years ago

Sir, Thank you very much for your help and time Yours very truly LOUIS ________

POSTED BY: Louis Piche
Posted 7 years ago

Sir, Your solution was quite interesting but the output is too heavy for me. It gives all results along with "Integer" or "not an integer"

Do[Print[{21/i, If[IntegerQ[21/i], "integer", "not an integer"]}], {i,   2, 7, 1}]

But I am looking for the following: If the input gives a result, I Print only the integer and maybe only the first integer I catch up But If the input does not give any result, such as

Do[Print[{21/i, If[IntegerQ[21/i], "integer", "not an integer"]}], {i,   2, 5, 1}] 

I only want to print an output such as "No Solution" Is it possible to make so? Thanks for your reply Louis __________

POSTED BY: Louis Piche
Posted 7 years ago

Sir, Somebody provides me with the following input Flatten[Table[If[IntegerQ[21/i],i,{}],{i,2,7,1}]]/.{}?"No Solution" The output is: 3 But It works first and now it does not work anymore Has the code changed? The system says it cannot replace {} by "No Solution". Where is the mistake? Thanks for your reply LOUIS ________

POSTED BY: Louis Piche
Posted 7 years ago

Flatten removes the {} elements in the list returned by Table. So there is then nothing to replace. If you omit Flatten, your example seems to work:

In[1]:= Table[If[IntegerQ[21/i], i, {}], {i, 2, 7, 1}] /. {} -> "No Solution"

Out[1]= {"No Solution", 3, "No Solution", "No Solution", "No Solution", 7}
POSTED BY: Hans Milton
Posted 7 years ago

Sir, Thanks for your help but this is not exactly what I am looking for. I would like the output shows once "No solution" if I had Table[If[IntegerQ[21/i], i, {}], {i, 2, 5, 1}] /. {} -> "No Solution" 21 cannot be divided by 2,3,4,5 and then I would like to get an output showing "No Solution" once only.

Can you help me on? Thanks for your reply Louis __________

POSTED BY: Louis Piche

What about this?

Union[Table[If[IntegerQ[21/i], i, False], {i, 2, 5, 1}]] /.  False -> "no solution"
POSTED BY: Hans Dolhaine
Posted 7 years ago

Hans,

What follows is not very interesting, but just another example of how to skin the same old cat:

In[1]:= DeleteDuplicates@Table[If[IntegerQ[21/i], i, {}], {i, 2, 7, 1}] /. {} -> "No Solution"

Out[1]= {"No Solution", 3, 7}
POSTED BY: Hans Milton

ist doch super

POSTED BY: Hans Dolhaine

It is incredible that this thread is so long with answers that are far away from the OP's. The OP's code is just fine, you need a slight modification to get exactly what you need:

Catch[Do[If[IntegerQ[21/i],Throw[i]],{i,2,7,1}];"no solution"]

and will not do more evaluations than necessary... It either find the first integer that divides 21, if not in range "no solution" will be returned.

POSTED BY: Sander Huisman
Posted 7 years ago

Sander, point taken. But to prolong the pain, here is yet another alternative:

In[1]:= SelectFirst[Range[2, 11], MemberQ[Most@Rest@Divisors@21, #] &]

Out[1]= 3

In[2]:= SelectFirst[Range[2, 11], MemberQ[Most@Rest@Divisors@11, #] &]

Out[2]= Missing["NotFound"]

Avoids the Catch/Throw mechanism, which can be seen as a bit "ugly" from a functional programming perspective.

POSTED BY: Hans Milton

I agree that a solution using SelectFirst, FirstPosition, FirstCase might be more 'Wolframian' indeed; but if the OP wants to go the Catch/Throw route--which is not too bad actually--, he can, and quite easily. Moreover I was not sure what version the OP uses; SelectFirst and friends are relatively new...

You can actually give a third argument to SelectFirst in the case it is missing, e.g.:

SelectFirst[Range[2, 7], IntegerQ[11/#] &, "no solution"]
POSTED BY: Sander Huisman
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract