Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Threaded View

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Posts
    3
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Need a more efficient algorithm for finding the highest pth of a number.

    This is my algorithm so far:
        public static int getPth(int x) {
            int largestP = 1;
     
            for (int p = 1; p < x; p++) {
                for (int b = 1; b < x; b++) {
                	double ans = Math.pow(b, p);
                    if (ans > x) {
                        break;
                    }else if(ans == x) {
                    	largestP = p;
                    }
               }           
            }
     
            return largestP;	
        }
    It prints out the highest pth for 17, 625, 1024 and 10000, which is 1, 4, 10, and 4, instantly but i'm trying to speed it up finding it for 1073741824. I know the math.pow() is slow and I was wondering if anyone had any ideas on how to generally speed up the algorithm.
    Last edited by firstTimeJava; October 1st, 2012 at 09:51 AM.


Similar Threads

  1. The nth highest number from a list
    By sbjibo in forum What's Wrong With My Code?
    Replies: 6
    Last Post: April 30th, 2012, 11:20 AM
  2. Finding Chromatic Number w/ Brute Force Algorithm
    By thecrazytaco in forum Algorithms & Recursion
    Replies: 2
    Last Post: November 16th, 2011, 07:35 AM
  3. Finding Chromatic Number of a Simple Graph w/ Brute Force Algorithm
    By thecrazytaco in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 15th, 2011, 09:27 PM
  4. Finding the highest number in an array?
    By halfwaygone in forum Algorithms & Recursion
    Replies: 1
    Last Post: April 17th, 2010, 03:56 PM
  5. Inputing file (.txt) and finding the highest number in the file
    By alf in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 15th, 2010, 09:11 AM

Tags for this Thread