Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

[Mathematica] Multiples of 5 from 0 to 100

  1. Jun 19, 2014 #1

    adjacent

    User Avatar
    Gold Member

    I have downloaded Mathematica to see if it is really that good. Many people use it.
    I want to calculate the multiples of 5 from 0 to 100.
    This seems pretty simple but I can't figure out a way to do it.
    I can't use the Modulo % in mathematica.
    I know that I should use Table. Is there any other function?
    I have searched google too, but could not find a solution. If project euler was online, it would have been so helpful!

    Please bear with me, I have never touched Mathematica until now.

    I tried this code , but it does not work;
    Code (Text):
    For[i = 0, i <= 100, i++,
     If[Mod[i, 5] = 0, Print[i], Print["Not found"]
      ]
     
    Last edited: Jun 19, 2014
  2. jcsd
  3. Jun 19, 2014 #2
    Mathematica is great - but it's been almost a decade since I last used it.
    I think what you want is something like this:

    Array[5*(#-1)&,21]

    or

    Array[5*#&,21,0]
     
    Last edited: Jun 19, 2014
  4. Jun 19, 2014 #3
    In your version, try "Mod[i,5] == 0".
     
  5. Jun 19, 2014 #4

    adjacent

    User Avatar
    Gold Member

    This works, but I don't understand that code. Can you explain?
    What is the need of 21 here? What is #?(Slot 1)? What is &?

    Ah, my method also works now.I thought mathematica does not use ==. How will I arrange them in a table now, instead of printing on a new line?
    Edit:
    This is my code now
    Code (Text):
    n = 10;
    m = 5;
    For[i = 1, i <= n, i++, If[Mod[i, m] == 0, Print[i], null]]
     
    Last edited: Jun 19, 2014
  6. Jun 19, 2014 #5
    The first argument to array is the function that will generate the values.
    The second argument is the size of the array. The set 0,5,10,...,100 has 21 elements.
    If there is a third argument and it is a simple number, it is the starting index to a 1-dimensional array. Otherwise the index starts at 1.

    For that first argument, the & at the end of the expression identifies it as a pure function. The # is a reference to the only argument o the function - which for the array syntax is the array index. If the pure function had more than one argument, references to those arguments would be #1, #2, ...
     
  7. Jun 19, 2014 #6

    adjacent

    User Avatar
    Gold Member

    Thank you so much. I have managed to perfect the code:
    Code (Text):
    m = 3;
    n = 25;
    Array[m*#1 &, Floor[(n/m)]]
     
  8. Jun 19, 2014 #7
    By George, I think you've got it!!
     
  9. Jun 19, 2014 #8

    adjacent

    User Avatar
    Gold Member

    Now is there any way to make an application with this?
    An .exe file?
     
  10. Jun 19, 2014 #9
    I'm pretty sure there isn't. You would pretty much need the entire Mathematica engine.
     
  11. Jun 19, 2014 #10

    adjacent

    User Avatar
    Gold Member

    I think there is something named "CDF Player"- Downloading now.
    I am now realising how great Mathematica is. Far greater than I expected!
     
  12. Jun 19, 2014 #11
    If you have some sort of calculus problem, the combination of human skill and pattern recognition and Mathematica's unerring, encyclopedic, and deep analysis just makes the game a whole lot easier.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook