Post Reply 
 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Global math static class
12-04-2009, 11:44 AM
Post: #1
Global math static class
Anybody want to help build a global math class. We will add useful functions that are common. For example isPrime, isPerfectNumber, Factorial etc... Anything people can come up with, and will just build and build off it.
Visit this user's website Find all posts by this user
Quote this message in a reply
12-04-2009, 11:04 PM
Post: #2
RE: Global math static class
Well very good idea. You have almost everything made in Pascal and C++ here.
You can make this for Java and include all Pascal and C++ functions and make that for that two languages !

There's a fine line between genius and insanity. I have erased this line.
Oscar Levant
There's a fine line between an administrator and black hat hacker. I have erased this line.
Dr DEBCOL
Visit this user's website Find all posts by this user
Quote this message in a reply
12-05-2009, 12:43 AM (This post was last modified: 12-16-2009 09:17 AM by codecaine.)
Post: #3
RE: Global math static class
Yea I will start the class and post a couple functions. Then anybody can add on or revise it to make it more efficient. Here is was I got so far.

Code:
import java.util.*;

public class MathExtended {

    //returns a List of factors
    public static List<Long> factors(long n)
    {
        List<Long> list = new ArrayList<Long>();
        for(long x = 1; x < n; x++)
        {
            if((n % x) == 0)
                list.add(x);
        }
        return list;

    }

    //returns true if the number is prime and false if the number not prime
    public static boolean isPrime(long num)
    {
        final long MAX = num / 2;
        if(num == 2)
            return true;
        if(num < 2)
            return false;
        if((num % 2) == 0)
            return false;
        for(int x = 3; x < MAX; x = x += 2)
            if( (num % x) == 0)
                    return false;

        return true;
    }
    

    //returns true if a number is a perfect number else return false
    public static boolean isPerfect(long number)
    {
        final long i = MathExtended.sumFactors(number); //see below
        if (i == number)
            return true;
        else
            return false;
    }

    //returns the sum of factors of a number
    public static long sumFactors(long number)
    {
        long factor = 0, sum = 0;
        for (int i = 1; i < number; i++){ //don't include the number itself, only its factors
            if ((number % i) == 0){
                factor = i;
                sum += factor;
            }
        }
        return sum;
    }

    // Evaluate n!
    public static long factorial( long n )
    {
        if( n <= 1 )     // base case
            return 1;
        else
            return n * factorial( n - 1 );
    }

    //returns true if the number is even else false
    public static boolean isEven(long n)
    {
        if( (n % 2) == 0 )
            return true;
        else
            return false;
    }

    //returns true if the number is odd else false
    public static boolean isOdd(long n)
    {
        if( (n % 2) != 0 )
            return true;
        else
            return false;
    }

    //return nth odd number given
    public static long oddPos(long num)
    {
        final int STEP = 2; //increments value by 2
        long value = -1; //holds odd numbers
        for(long x = 0; x < num; x++)
        {
            value += 2;
        }
        return value;
    }

    //return nth odd number given
    public static long evenPos(long num)
    {
        final int STEP = 2; //increments value by 2
        long value = 0; //holds even numbers
        for(long x = 0; x < num; x++)
        {
            value += 2;
        }
        return value;
    }
    

    //returns fibonacci
    public static long fib(long n)
    {
         long a=0,b=1;
         for (long i=0;i<n;i++){
              a=a+b;
              b=a-b;
         }
         return a;
    }

    //returns a list of fibonacci sequence
    public static List fibSequence(long n)
    {
         List<Long> list = new ArrayList<Long>();
         long a=0,b=1;
         for (long i=0;i<n;i++){
              list.add(a);
              a=a+b;
              b=a-b;
         }
         return list;
    }

    //returns the sum of fibonacci sequence
    public static long fibSum(long n)
    {
         long a=0,b=1;
         long sum = 0;
         for (int i=0;i<=n;i++){
              sum += a;
              sum = sum + a;
              a=a+b;
              b=a-b;
          }
         return sum;
    }

    //returns a list of fibonacci sequence
    public static void fibSequencePrint(long n)
    {
         long a=0,b=1;
         System.out.println("Fibonacci Sequence:");
         for (long i=0;i<n;i++)
         {
              System.out.println(a);
              a=a+b;
              b=a-b;
         }
    }    

}

Here I am testing the functions
Code:
public class MathTest
{
    public static void main(String[] args)
    {
        //check if any of the first 10 numbers are prime
        for(int x = 1; x <= 10; x++)
        {
            if(MathExtended.isPrime(x))
                System.out.println(x + " is prime");
            else
                System.out.println(x + " is not prime");
        }
        
        //check if any of the first 10 numbers is a perfect number
        for(int x = 1; x <= 10; x++)
        {
            if(MathExtended.isPerfect(x))
                System.out.println(x + " is a perfect number");
            else
                System.out.println(x + " is not a perfect number");
        }
        
        //get the value of factorisl from !1 to !10
        for(int x = 1; x <= 10; x++)
        {
            System.out.println(MathExtended.factorial(x));
        }
    }
}

source code attached to the bottom


Attached File(s)
.zip  MathExtended.java.zip (Size: 1.06 KB / Downloads: 1)
Visit this user's website Find all posts by this user
Quote this message in a reply
12-05-2009, 03:33 AM
Post: #4
RE: Global math static class
Very good, but i have one improvement.
When you search for primes in this line :
for(int x = 3; x < HALF_MAX; x = x + 2 )
except HALF_MAX you should find an square root of the number and count to floor() of that square root.
Think about that !

There's a fine line between genius and insanity. I have erased this line.
Oscar Levant
There's a fine line between an administrator and black hat hacker. I have erased this line.
Dr DEBCOL
Visit this user's website Find all posts by this user
Quote this message in a reply
12-05-2009, 04:42 AM
Post: #5
RE: Global math static class
Yea I was trying that but for some reason wasn't getting correct results.
Visit this user's website Find all posts by this user
Quote this message in a reply
12-15-2009, 03:51 PM
Post: #6
RE: Global math static class
I added some fibonacci functions to the java math class
Visit this user's website Find all posts by this user
Quote this message in a reply
12-16-2009, 09:18 AM
Post: #7
RE: Global math static class
updated the global math class with a factorial function that returns the factions as a list and is even or odd function checker
Visit this user's website Find all posts by this user
Quote this message in a reply
12-17-2009, 04:50 AM
Post: #8
RE: Global math static class
You can also add some geometry functions for example :
Euclidean distance
http://en.wikipedia.org/wiki/Euclidean_distance

There's a fine line between genius and insanity. I have erased this line.
Oscar Levant
There's a fine line between an administrator and black hat hacker. I have erased this line.
Dr DEBCOL
Visit this user's website Find all posts by this user
Quote this message in a reply
04-11-2010, 10:59 AM
Post: #9
RE: Global math static class
Here is a basic function:

Code:
public static int Geo_Polygon_angles(int side){
    int measure = (side - 2) * 180;

    
    return measure;
}

"Character is determined more by the lack of certain experiences than by those one has had."
Friedrich Nietzsche
Visit this user's website Find all posts by this user
Quote this message in a reply
Post Reply 


Forum Jump:


 Quick Theme: