View Single Post
Old 14-08-2009, 16:02   #30
punky
Inactive
 
Join Date: Jun 2003
Age: 44
Posts: 14,750
punky has a golden aurapunky has a golden aura
punky has a golden aurapunky has a golden aurapunky has a golden aurapunky has a golden aurapunky has a golden aurapunky has a golden aurapunky has a golden aurapunky has a golden aurapunky has a golden aurapunky has a golden aurapunky has a golden aurapunky has a golden aurapunky has a golden aurapunky has a golden aurapunky has a golden aura
Re: Programming Challenges?

I've had a bash at the modulus one:

Spoiler: 

Code:
        static void Main(string[] args)
        {
            for (int i = 1; i <= 100; i++)
            {
                if (Modulus(i, 3) == 0 && Modulus(i, 5) == 0)
                    Console.WriteLine(i + " is a multiple of both 3 AND 5!");
                else if (Modulus(i, 3) == 0)
                    Console.WriteLine(i + " is a multiple of 3");
                else if (Modulus(i, 5) == 0)
                    Console.WriteLine(i + " is a multiple of 5");
                else
                    Console.WriteLine(i + " is not a multiple of 3 (remainder " + Modulus(i, 3) + ") or a multiple of 5 (remainder " + Modulus(i, 5) + ")");
            }


            Console.In.ReadLine();
        }

        public static int Modulus(int num, int divisor)
        {
            while (num > divisor)
            {
                num -= divisor;
            }

            if (num == divisor)
                num = 0;

            return num;
        }
Which produces:

1 is not a multiple of 3 (remainder 1) or a multiple of 5 (remainder 1)
2 is not a multiple of 3 (remainder 2) or a multiple of 5 (remainder 2)
3 is a multiple of 3
4 is not a multiple of 3 (remainder 1) or a multiple of 5 (remainder 4)
5 is a multiple of 5
6 is a multiple of 3
7 is not a multiple of 3 (remainder 1) or a multiple of 5 (remainder 2)
8 is not a multiple of 3 (remainder 2) or a multiple of 5 (remainder 3)
9 is a multiple of 3
10 is a multiple of 5
11 is not a multiple of 3 (remainder 2) or a multiple of 5 (remainder 1)
12 is a multiple of 3
13 is not a multiple of 3 (remainder 1) or a multiple of 5 (remainder 3)
14 is not a multiple of 3 (remainder 2) or a multiple of 5 (remainder 4)
15 is a multiple of both 3 AND 5!
...


Speaking of operators, in some OO (Object-Orrientated) languages like C# and Java you can actually overload operators (re-code them to do some different). I had to do this in university once - write a Matrix class (a matrix is like a grid/table of values) including defining the + - * / operations for them.

So there's another good educational challenge if someone wants. Don't complain, you're getting a programming degree for free in thread
punky is offline   Reply With Quote