Results 1 to 5 of 5

Math Help - Java Blue J

  1. #1
    Newbie
    Joined
    Sep 2010
    Posts
    8

    Java Blue J

    How can I write a program in Blue J?

    I want to do this:

    x is in {0,1,2,...,9999}
    Is x a prime? if not then give the the factors.

    Is there natural numbers a,b such that x = a^3 + b^3 ?
    Follow Math Help Forum on Facebook and Google+

  2. #2
    MHF Contributor undefined's Avatar
    Joined
    Mar 2010
    From
    Chicago
    Posts
    2,340
    Awards
    1
    Quote Originally Posted by Quantum View Post
    How can I write a program in Blue J?

    I want to do this:

    x is in {0,1,2,...,9999}
    Is x a prime? if not then give the the factors.

    Is there natural numbers a,b such that x = a^3 + b^3 ?
    I looked up BlueJ and found it's not a programming language, just an IDE.

    There are many ways to get the factors of x in Java for x in that range. One is trial division, another is using a smallest prime sieve, and there are many others,

    Integer factorization - Wikipedia, the free encyclopedia

    Here is a simple way with trial division, it will be plenty fast for that range. You will probably learn more if you try coding yourself and only look at the code below as a last resort. There is some optimisation that might make it hard for you to read.

    Spoiler:
    Code:
    public ArrayList<Integer> naiveFactorise(int n) {
        // returns prime factors in ascending order, duplicates allowed
        ArrayList<Integer> pFacts = new ArrayList<Integer>();
        if (n < 2) return pFacts;
        while (n % 2 == 0) {
            pFacts.add(2);
            n /= 2;
        }
        while (n % 3 == 0) {
            pFacts.add(3);
            n /= 3;
        }
        int lim = (int)Math.sqrt(n);
        for (int i = 5, inc = 2; i <= lim; i += inc, inc = 6 - inc) {
            if (n % i == 0) {
                pFacts.add(i);
                n /= i;
                while (n % i == 0) {
                    pFacts.add(i);
                    n /= i;
                }
                lim = (int)Math.sqrt(n);
            }
        }
        if (n != 1) pFacts.add(n);
        return pFacts;
    }
    Of course you will need to import java.util.ArrayList and if you're using public static void main() then make this method static and call it from main().


    I generally use a smallest prime sieve for larger numbers that aren't too large.

    "Is there natural numbers a,b such that x = a^3 + b^3 ?"

    This is a completely different question. However the answer is obviously yes since (a,b) = (1,1) satisfies.
    Follow Math Help Forum on Facebook and Google+

  3. #3
    MHF Contributor undefined's Avatar
    Joined
    Mar 2010
    From
    Chicago
    Posts
    2,340
    Awards
    1
    Oh yeah the method above gives just the prime factors with multiplicity. I have other methods to turn this into a list of factors but you're probably better off just doing

    Code:
    public ArrayList<Integer> getFactors(int n) {
        ArrayList<Integer> factors = new ArrayList<Integer>();
        for (int i = 1; i <= n; i++) {
            if (n % i == 0) factors.add(i);
        }
        return factors;
    }
    The number is prime if and only if factors.size() == 2.
    Follow Math Help Forum on Facebook and Google+

  4. #4
    Newbie
    Joined
    Sep 2010
    Posts
    8
    Thanks!

    Well how can we write a program for x
    if x can be writen like
    x= a^3 + b^3, where a and b is natural numbers? And then it shall return me a list of the numbers.
    x is in (0,1,...,9999)
    Follow Math Help Forum on Facebook and Google+

  5. #5
    MHF Contributor undefined's Avatar
    Joined
    Mar 2010
    From
    Chicago
    Posts
    2,340
    Awards
    1
    Quote Originally Posted by Quantum View Post
    Thanks!

    Well how can we write a program for x
    if x can be writen like
    x= a^3 + b^3, where a and b is natural numbers? And then it shall return me a list of the numbers.
    x is in (0,1,...,9999)
    ~~~~~~
    Here's one way. Generate all cubes greater than 0 and less than 10^4. Put them in a list (sorted ascending). Then for each prime x, do a loop where you go through the list, subtracting the cube from x and seeing if the result is also in the list. Get out of the loop when the number you're subtracting is greater than floor(x/2).

    For example you're going through all primes and you're currently at x = 23. Is 23-1=22 a perfect cube? No. Is 23-8=15 a perfect cube? No. And then you stop the loop because the next cube 27 is greater than floor(23/2). Thus 23 is not the sum of two cubes of natural numbers.

    ~~~~~~

    Here's another way, probably a bit easier to write.

    Do a loop within a loop:

    Code:
    for(i=1,i3=1;i3+i3<9998;i++,i3=i*i*i)
    for(j=i,s=i3+j*j*j;s<9998;j++,s=i3+j*j*j) {
        // check if s is prime here
    }
    Last edited by undefined; September 5th 2010 at 11:47 AM.
    Follow Math Help Forum on Facebook and Google+

Similar Math Help Forum Discussions

  1. Help with Computer Science Java
    Posted in the Advanced Math Topics Forum
    Replies: 1
    Last Post: October 1st 2009, 05:24 PM
  2. Manipulate Math with Java
    Posted in the Math Forum
    Replies: 0
    Last Post: September 27th 2009, 11:24 AM
  3. Another Java Program
    Posted in the Advanced Math Topics Forum
    Replies: 0
    Last Post: November 2nd 2006, 01:23 PM
  4. Java Program - Summing Integers
    Posted in the Advanced Math Topics Forum
    Replies: 13
    Last Post: October 28th 2006, 05:19 AM
  5. More Java Help
    Posted in the Advanced Math Topics Forum
    Replies: 1
    Last Post: October 27th 2006, 07:45 PM

Search Tags


/mathhelpforum @mathhelpforum