Results 1 to 2 of 2

Math Help - Beginner to Mathematica's question! using Module!

  1. #1
    Newbie
    Joined
    Oct 2008
    Posts
    4

    Beginner to Mathematica's question! using Module!

    Okay, this is the question that i need to solve using mathematica:

    Define the function f(n) which counts the number of zeros in n if n is the integer greater than 0.
    If you input 105, 1005, 10005, find f(n).


    I want to use Module in this question and I just found out that i have to use "Mod" and "Floor" but still havent figured out this. Please help!
    Follow Math Help Forum on Facebook and Google+

  2. #2
    Newbie
    Joined
    Oct 2008
    Posts
    12

    Module is a scoping construct

    Module is used for using variables in just the context of one input.One option for your code is:
    f[n_]:=Total[Last/@NestWhileList[{Floor[#[[1]]/10],Boole[IntegerPart[#[[1]]/10]
    #[[1]]/10]}&,{n,0},IntegerPart[#[[1]]/10]0&],1,,1]

    which repeatedly divides the number by ten and checks whether it is equal to its floor, returning elements of the form {IntegerPart of last # over 10, 1 if last # over 10 equals IntegerPart of last # over ten, 0 otherwise}. It then takes the last part of each of these elements, and sums them.

    Another option that I just thought of that uses module (I reread your post and noticed the comment about module) is:
    f[n_]:=Module[{a=0,n1=n},If[n1==0,a=1];While[n10,If[Mod[n1,10]0,a++];n1=IntegerPart[n1/10]];a]
    which does the same thing. In order for it to work for negative numbers, use integerpart rather than floor, because floor rounds down, integerpart rounds toward zero.
    This works by setting a=0 and n1=n. Then it checks if n1 already equals zero, setting a to one if it is, because zero has one zero in it. If n1 is not already equal to zero, it checks whether n1 mod 10 equals zero, if it is it increments a, and then it divides n1 by ten, takes the integer part of it and repeats until n1 equals zero.
    This works for all integers.
    Last edited by Hood; November 6th 2008 at 05:40 PM.
    Follow Math Help Forum on Facebook and Google+

Similar Math Help Forum Discussions

  1. Simple derivative - beginner question - in Mathematica
    Posted in the Math Software Forum
    Replies: 0
    Last Post: May 27th 2011, 05:40 PM
  2. Beginner Help with Wolfram Mathematica pl!
    Posted in the Math Software Forum
    Replies: 8
    Last Post: April 26th 2011, 12:58 PM
  3. Mathematica Help (Beginner) - Euler's/Heuns Method
    Posted in the Math Software Forum
    Replies: 0
    Last Post: December 2nd 2009, 08:42 PM
  4. Beginner to mathematica
    Posted in the Math Software Forum
    Replies: 8
    Last Post: November 13th 2008, 02:39 PM
  5. Mathematica Help Urgent (Very New Beginner)
    Posted in the Math Software Forum
    Replies: 1
    Last Post: November 2nd 2008, 06:10 PM

Search Tags


/mathhelpforum @mathhelpforum