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 4 of 4

Thread: How would you approach this?

  1. #1
    Member Staticity's Avatar
    Join Date
    Jul 2011
    Location
    Texas
    Posts
    105
    My Mood
    Inspired
    Thanks
    3
    Thanked 5 Times in 5 Posts

    Default How would you approach this?

    So, I just finished Problem 28 on Project Euler (Please click on this to see the problem).. And I am quite proud of my algorithm! But, I'm curious about how yal would approach this problem!! Just comment with your step by step logic.

    I'll gladly post my algorithm after seeing some of yal's approaches
    Last edited by Staticity; October 9th, 2011 at 11:06 PM. Reason: Fixed Link
    Simplicity calls for Complexity. Think about it.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: How would you approach this?

    Mathematical formulas for computing the value at each diagonal:

    n^2-x*(n-1), where n is the width of the sub-spiral and x is which diagonal (0 is top-left, going counter-clock wise).

    Simply sum up all spirals from n=3:2:1001 and add on the 1 for the center.

  3. #3
    Member Staticity's Avatar
    Join Date
    Jul 2011
    Location
    Texas
    Posts
    105
    My Mood
    Inspired
    Thanks
    3
    Thanked 5 Times in 5 Posts

    Default Re: How would you approach this?

    Oh wow this is interesting... I bet there are several formulas like this one that I could implement into my later algorithms.. Nice HelloWorld

    Here's my approach:
    public class prob28 {
    	public static void main(String args[]){
     
    		double sum = 0;
    		double inc = 2;
    		double limit = 3;
     
    		for(int i = 1; i <= 1001*1001; i+=inc){
    			if(i%(limit*limit) == 0){
    				inc+=2;
    				limit+=2;
    			}
    		  sum+=i;
    		}
    		System.out.println(sum);
     
    	}
    }

    Looking at the spiral given.. You can make a few inferences:

    1. The spiral will end at a square number of every odd number.. 1^2 = 1 -> 3^2 = 9 -> 5^2 = 25
    2. All of the above numbers are the end of each individual perfect square..
    3. Also, since the dimensions are increasing by odd numbers, the distance between each corner will increase by 2.. Here's the pattern:

    Increments of 2: 1, 3, 5, 7, 9 (reach perfect odd square)
    Increments of 4: 13, 17, 21, 25 (reach perfect odd square)
    Increments of 6: 31, 37, ... etc.

    So that's basically what my code represents! I thought it was pretty crafty of me
    Simplicity calls for Complexity. Think about it.

  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: How would you approach this?

    Clever It's basically the same idea except you left it in the recurrence relationship form, and I had removed the recurrence relationships. Both equally correct and quick to compute.

Similar Threads

  1. How best to approach this project?
    By eb_dev in forum Java Theory & Questions
    Replies: 0
    Last Post: March 22nd, 2011, 11:43 AM