hey,
could u help me with my java programming? i have this problem: Thanks a lot!!!!
The mathematician Augustus De Morgan was aged 43 in the year 1849AD. This is interesting because 43 squared is 1849, ie, in 1849 his age was the square root of the year.
Given that no person has ever lived longer than 123 years (and assuming that no one ever will), write a Java application that will determine if it is possible that anyone who is alive today is, has ever been, or will ever be alive in a year that is the square of their age.
If it is possible, your program should print out the years in which it happens and the ages that the people concerned will have in those years.

Recommended Answers

All 17 Replies

what your question about the problem.
It seems pretty straight forward to me
use a condition that checks if the square of the current age is equal to the current year until the age of 123 using a loop

For mathematical part, your starting year is this year (2012). The year is 0, and there are people age from 0 (new born today) to 123 (turn 123 this year). The next year (2013), the year is 1, you aren't going to count anyone who is born in that year because the question required to compute only those who are alive in 2012. So, those whose age is 0 (new born) would become 1, and those who are 123 will no longer alive. As a result, your iteration will be from 1 to 123 (instead of 0 to 123). Inside each iteration, you need to check whose age is the square root of the year you are going through. Keep doing the iteration until 123 years later.

Remember, the value of square root must be exactly an integer in order to be compared. This point will reduce a lot of computation in your program.

please post some code and tell where the problem is?
how can we suggest answer without knowing question.?

Thank you

public class Square_Ages {


    public static void main(String [] args) {
    final int Max_Life_Expectancy=123;
    final int current_Year=2012;
    for(int age=1; age<123; age++) {
        int year=age*age;
        int Born_Year= (year-age);
        int (current_Year -Born_Year) <= Max_Life_Expectancy;




    }


}
}

this is what i have written so far, don't know what to do next..

Before going into your code, I would like to talk about code convention. For naming convention, please read this link. There are certain reasons why there are rules.

Now back to your code. Line 5, you should not make current_year to be final (constant). The idea is that you would want to use it to increment in your loop. You don't need to keep track of what the original value is.

Line 7, the loop should start from 0 and ends at MAX_LIFE_EXPECTANCY (not less than) because you need to check for the current year as well.

Inside the loop, a much easier way to check is to find out the square root value of the current year. If the result is an integer, the value is the age of the person whose age becomes the square root of the year. Display the result of the age and the person's born year (simply subtract the age from the current year). If the result is not an integer (contain decimals), ignore the year and move to the next year.

The currentYear value could be increment by 1 in each loop iteration. Therefore, the counter of the loop may not need to be used but simply a counter.

thank you, Taywin!!!!!!

It would be like this:

current_year<-2012
for i=current_year to current_year+123 do
    for j=0 to 124 do
        if i*i==j then
            print year=j
            print age=i
            print birth year=j-i
        end_if
    end_for
end_for

Remember, this is written in pseudo-code, so you'll have to implement it.

Lucaci, your method is too slow and contain unnecessary inner loop. The value of square root of a year will never be greater than 123 and only one born-year will match the square root value, so iterate from 0 to 124 to find a match square value is overkilled.

thanks a lot!!!

public class Square_Ages {


    public static void main(String [] args) {
    int MaxLife_Expectancy=123;
    int current_Year=2012;
    for(int age=0; age<123; age++) {
        int year=age*age;
        int Born_Year= (year-age);
        int current_Age = current_Year -Born_Year <=MaxLife_Ecpectancy;
        System.out.println(((int + >0)?",":"")+int);

hi again. I still haven't finished my program. In line 10, when using symbols like <= , can I not start it with 'int'?

Hmm... Your mind is still stuck to the brute-force algorithm... Also, I saw that the pseudo code given by Lucaci is incomplete. The code will display the value of people who are born after the year 2012. Below is a pseudo code for the algorithm I explained earlier. It looks a bit different but it works exacly what I mentioned (no inner loop and check for the square root value of the year).

/*
startYear <- 2012
for age between 0 upto MAX_LIFE_EXPENTENCY exclusive
  currentYear <- startYear+age
  currentAge <- square root of currentYear
  bornYear <- currentYear-currentAge
  if (currentAge is an integer) and (bornYear is less than or equal to startYear)
    display currentYear, bornYear, and currentAge
  end if
end for
*/

The result should show only the year 2025 but not 2116. The reason is that people who were born in that year were not alive in the starting year (2012).

thank you again, Taywin! I have just started learning Java in college and I find it very difficult!!! have to finish it till tomorrow)

Hahahaha I'm in Trinners aswell, it's a bit of a balls figuring this one out.

hi

I am doing this exact question and I don't know where I am going wrong. someone please help asap!!!!

import javax.swing.JOptionPane;
public class SquareAge {


    public static void main (String [] args) {
}
    int MAX_LIFE_EXPECTANCY = 123;
    int startYear = 2012; {
    int age =1; 
    for (age=0; age<=MAX_LIFE_EXPECTANCY; age++); {
        double currentYear = (startYear+age);
        double currentAge = (Math.sqrt(currentYear));
        double bornYear = (currentYear - currentAge);

          if ((currentAge == (int)currentAge) && (bornYear <= currentYear));
          {
          JOptionPane.showMessageDialog (null, "currentYear, bornYear , currentAge");

    }
    }
    }
}

Would this solve it?

int min_year = 2012-122;
int max_year = 2012+122;

for (int age = 1; i <= 123; i++)
{
    if ((i*i >= min_year) && (i*i <= max_year))
    {
        int age = i;
        int year = i*i;
        int birth = (i*i)-i;
        System.out.println("A person borne in "+birth+" would be "+age+" yrs of age in "+year);
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.