Mathematica: I made a program, but can't manipulate it. Please help

  • Context: Mathematica 
  • Thread starter Thread starter gikiian
  • Start date Start date
  • Tags Tags
    Mathematica Program
Click For Summary
SUMMARY

The discussion focuses on a Mathematica program designed to calculate the combinations of three dice rolls that sum to a specified number between 3 and 18. The original code provided by the user was ineffective in manipulating the input number, leading to confusion. A solution was proposed by defining a function, f[w_], which successfully computes and prints the combinations for any given sum, demonstrating the correct implementation of nested loops and conditional statements in Mathematica.

PREREQUISITES
  • Understanding of Mathematica syntax and functions
  • Familiarity with loops and conditional statements in programming
  • Basic knowledge of combinatorial mathematics
  • Experience with debugging code in Mathematica
NEXT STEPS
  • Explore advanced Mathematica functions for combinatorial calculations
  • Learn about the use of modules and scoping in Mathematica
  • Research optimization techniques for nested loops in programming
  • Investigate error handling and debugging strategies in Mathematica
USEFUL FOR

Mathematica users, programmers interested in combinatorial algorithms, educators teaching programming concepts, and anyone looking to enhance their skills in mathematical programming.

gikiian
Messages
98
Reaction score
0
The program I wrote accepts a certain number between 3 and 18.
Then it returns the ways this number can be obtained by throwing three dice simultaneously and adding the face values.
I want to manipulate this number, :biggrin:
but can't achieve it using the ordinary code of manipulation.

I tried to find any error, but really wasn't able to figure out where have I gone wrong. :confused:

Need help!

Code:
j = 0;
For [
      x = 1, 
      x <= 6, 
      x++, 
      For [ 
            y = 1, 
            y <= 6, 
            y++, 
            For [ 
                  z = 1, 
                  z <= 6, 
                  z++,
                  {
                   If [ 
                       x + y + z == 5,  (*accepts the required sum of the face values*)
                       { 
                        If [
                            j + 1 <= 9, 
                            Print [j + 1, ".", "   ", x , " ", y, " " , z ], 
                            Print [j + 1, ".", "  ", x , " ", y, " " , z ]
                           ], 
                        j++
                       }    
                      ]
                  }
                 ]
           ]
     ]
 
Last edited:
Physics news on Phys.org
You could define a function, and then evaluate it for different values, like this:

Code:
f[w_] :=
 Module[{j = 0},
  For[x = 1, x <= 6, x++, 
   For[y = 1, y <= 6, y++, 
    For[z = 1, z <= 6, 
     z++, {If[
       x + y + z == 
        w,(*accepts the required sum of the face values*){If[
         j + 1 <= 9, Print[j + 1, ".", "   ", x, " ", y, " ", z], 
         Print[j + 1, ".", "  ", x, " ", y, " ", z]], j++}]}]]]]

f[5]

1.   1 1 3

2.   1 2 2

3.   1 3 1

4.   2 1 2

5.   2 2 1

6.   3 1 1

f[3]

1.   1 1 1

f[4]

1.   1 1 2

2.   1 2 1

3.   2 1 1
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
5K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 1 ·
Replies
1
Views
4K