# I wanna get a result of i/j . Like 1/3, 1/5, 1/7, 3/5, 3, 5/3… Something like that. But none of my code works [closed]

None of this works,can anyone correct this?

## 1st method

j = {1, 3, 5, 7, 9}; i = {1, 3, 5, 7, 9}; jase[i_, j_] := i/j

## 2nd method

i != j; Table[i/j, j \[Element] {1, 3, 5, 7, 9}; i \[Element] {1, 3, 5, 7, 9}]

## 3rd method

jase[i_, j_] := Module[{}, i ≠ j; Element[{i, j}, List[1, 3, 5, 7, 9]]; i/j]

• It's unclear what you want and none of this syntax is even close to valid Mathematica syntax. It looks like syntax from a very different language to me. Could you try to explain what you need or expect? What are the desired outputs? – Sjoerd Smit Oct 10 '19 at 10:34

Going through your attempts one by one:

### Attempt 1

j = {1, 3, 5, 7, 9};
i = {1, 3, 5, 7, 9};
jase[i_, j_] := i/j


What you are doing here is three things:

• You set the value of j to {1, 3, ...}
• You set the value of i to {1, 3, ...}
• You define a function jase that takes two arguments named i and j (no connection to the variables from before) that returns i/j. So e.g. jase[3,4] will return 3/4

To fix it, a first attempt could be to call jase[i,j]:

jase[i, j]
(* {1, 1, 1, 1, 1} *)


As we can see, this is not what we want. The problem is that {...}/{...} simply divides corresponding elements. To get all possible combinations, you can use Outer (as shown by @kglr):

Outer[jase, i, j]
(* {{1, 1/3, 1/5, 1/7, 1/9}, {3, 1, 3/5, 3/7, 1/3},
{5, 5/3, 1, 5/7, 5/9}, {7, 7/3, 7/5, 1, 7/9},
{9, 3, 9/5, 9/7, 1}} *)


### Attempt 2

i != j;
Table[i/j, j ∈ {1, 3, 5, 7, 9}; i ∈ {1, 3, 5, 7, 9}]


You're doing two things:

• i != j simply returns itself without doing anything. You're suppressing that output with the ;
• The syntax for the Table expression is wrong. To specify that i and j should be elements of the list {1, 3, ...}, use {i, {1, 3, ...}}:

Table[i/j, {j, {1, 3, 5, 7, 9}}, {i, {1, 3, 5, 7, 9}}]
(* {{1, 3, 5, 7, 9}, {1/3, 1, 5/3, 7/3, 3},
{1/5, 3/5, 1, 7/5, 9/5}, {1/7, 3/7, 5/7, 1, 9/7},
{1/9, 1/3, 5/9, 7/9, 1}} *)


### Attempt 3

jase[i_, j_] :=
Module[{}, i ≠ j; Element[{i, j}, List[1, 3, 5, 7, 9]]; i/j]


This one defines a function jase with two arguments, that does three things:

• Evaluate the expression i ≠ j (which does nothing on its own) and discard its result
• Evaluate the expression Element[{i, j}, List[1, 3, 5, 7, 9]] (which does nothing on its own), and discard its result
• Evaluate the expression i/j (which stays unevaluated) and return its result

So calling jase[1, 2] gives for example:

jase[1, 2]
(* 1/2 *)


I don't think this one can be easily fixed

You'll notice that all the examples above return a bunch of ones, since the cases where i==j are not discarded. There are several ways to do this, see e.g. the answers of @kglr and @ChrisK. A solution that is more explicit could be the following:

jase[i_, j_] := i/j
jase[i_, i_] := Nothing

Outer[jase, Range[1, 9, 2], Range[1, 9, 2]]
(* {{1/3, 1/5, 1/7, 1/9}, {3, 3/5, 3/7, 1/3}, {5, 5/3, 5/7, 5/9}, {7, 7/3, 7/5, 7/9}, {9, 3, 9/5, 9/7}} *)


This is essentially your first attempt with an additional case for jase: The second definition says "if both arguments are the same, then return Nothing". And Nothing is automatically removed from lists.

• Nice pedagogical answer! – Chris K Oct 10 '19 at 20:21
• @Lukas Lang Thank you for detailed info first. != means ≠ . The problem here is that Mathematica can intepret != into ≠ , but stackexchange.com can't do the same thing as Mathematica does – kile Oct 11 '19 at 3:05
• @kyle I am aware that != and ≠ are the same thing in Mathematica - why do you mention it? – Lukas Lang Oct 11 '19 at 5:30
• @Lucas Lang you told me i != j simply returns itself without doing anything. It should add constraints to j and i – kile Oct 12 '19 at 13:33
• kile, you forgot to add the second part of definition (jase[_, _] := Nothing) in Lucas's comment; that is, use ClearAll[jase]; jase[i_, j_] := i/j /; i != j ; jase[_, _] := Nothing; Outer[jase, Range[1, 9, 2], Range[1, 9, 2]] // Flatten // DeleteDuplicates – kglr Oct 13 '19 at 7:10

How's this?

range = {1, 3, 5, 7, 9};
DeleteCases[Flatten[Table[i/j, {i, range}, {j, range}]], 1]
(* {1/3, 1/5, 1/7, 1/9, 3, 3/5, 3/7, 1/3, 5, 5/3, 5/7, 5/9,
7, 7/3, 7/5, 7/9, 9, 3, 9/5, 9/7} *)


Just for fun:

Divide @@@ Tuples[Range[1, 9, 2], 2] /. (1 :> Sequence[])


Tuples avoids need for Flatten with Outer or Table

• @ ubpdqn Thanks. How can addi != j as a constraint? – kile Oct 13 '19 at 6:48
• @kile, the replace rule at end removes all the 1s = j/j. – ubpdqn Oct 13 '19 at 6:51
• Have you seen the comments made by Lucas Lang 17 hours ago? He tried to solve it withCondition . I don't think it works. You can see it in the response I made to his comments – kile Oct 13 '19 at 6:53
• @kile sorry I did not. As with many things, one of the joys of MMA (WL) is that you can do the same thing in many ways. You choose the way that suits your needs (including scaling up, speed, aesthetics etc). I think the other answers work well. I just added this as Tuple is sometimes useful. All the best. – ubpdqn Oct 13 '19 at 6:56
• ubpdqn ,Can you explain your Code ? How do you add i/j? – kile Oct 14 '19 at 13:23
range = Range[1, 9, 2];

# / DeleteCases[range, #] & /@ range // Flatten


{1/3, 1/5, 1/7, 1/9, 3, 3/5, 3/7, 1/3, 5, 5/3, 5/7, 5/9, 7, 7/3, 7/5, 7/9, 9, 3, 9/5, 9/7}

Flatten @ MapIndexed[Drop] @ Outer[Divide, range, range]

MapIndexed[Apply[Sequence] @* Drop] @ Outer[Divide, range, range]

Tuples[foo[range, range]] /. foo[a_, a_] -> Nothing /. foo -> Divide

Join @@ Table[i/j, {i, range}, {j, DeleteCases[range, i]}]

Distribute[{range, range}, List, List, DeleteCases[{##}, 1] &, Divide]

Array[Divide, {1, 1} Length@range, {MinMax@range}, DeleteCases[Flatten[{##}], 1] &]


and a Halloween special:

☺ = range;
## & @@@ (#/(☺ /. # -> (## &[])) & /@ ☺)

• kglr,Great thanks to your effort in answering my question here. BTW, have you seen the comments made by Lucas Lang 17 hours ago? He tried to solve it with Condition . I don't think it works. You can see it in the response I made to his comments – kile Oct 13 '19 at 7:02
• @kile, pls see my comment under Lucas's answer. – kglr Oct 13 '19 at 7:11
• Can you explain you code here? Tuples[foo[range, range]] /. foo[a_, a_] -> Nothing /. foo -> Divide – kile Oct 14 '19 at 13:25
• @kile, if we use Tuples[Divide[range, range]] , the function Divide is evaluated (and gives {1,1,1,1,1}) before Tuples gets to work.So instead I use Tuples[foo[range, range]] which gives {foo[1, 1], foo[1, 3], foo[1, 5], foo[1, 7], foo[1, 9], foo[3, 1], foo[3, 3], foo[3, 5], foo[3, 7], foo[3, 9], foo[5, 1], foo[5, 3], foo[5, 5], foo[5, 7], foo[5, 9], foo[7, 1], foo[7, 3], foo[7, 5], foo[7, 7], foo[7, 9], foo[9, 1], foo[9, 3], foo[9, 5], foo[9, 7], foo[9, 9]} then replace foo with Divide to get the ratios of all pairs. – kglr Oct 14 '19 at 14:07
• Why was this not working?i = {1, 3, 5, 7, 9}; j = {1, 3, 5, 7, 9}; Tuples[jase[i, j]] /. jase[i_, j_] -> i/j – kile Oct 15 '19 at 15:28