Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4382950
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T12:50:41+00:00 2026-05-21T12:50:41+00:00

I am trying to write a method that determines if the square of the

  • 0

I am trying to write a method that determines if the square of the age of a person is equal to the current year. For example a man was aged 43 in 1849 and the square of 43 is 1849.
Below is my code for it but I dont know why my exception is not working because it is assumed that no body can live past 123 years

 public Boolean isAlive(int y){
     assert( y>=1888 && y<=2134);
     int age=0;
     while(age<=123){
         y=y+1;
         age=age+1;
         if(age*age==y){
             int c=age;
             return true;
             }
         }
      return false;
   }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T12:50:42+00:00Added an answer on May 21, 2026 at 12:50 pm

    The problem you are trying to solve appears to be

    y + n = n^2
    

    where y is the starting year and n is the age and both must be a positive whole number.

    Rearranged this is

    n = (sqrt(1 + 4 * y) + 1)/2;
    

    e.g.

    when y = 1806, n = 43, n + y = n * n = 1849.

    when y = 1980, n = 45, n + y = 2025, n * n = 2025.

    So you can write

    public boolean squareAgeIfBorn(int year) {
       double n = (Math.sqrt(1 + 4 * year) + 1) / 2;
       return Math.abs(n - (long) n) < 1e-9; // i.e. if n is a whole number.
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a method that acts as a setter and takes some
I am trying to write a generic Parse method that converts and returns a
I am trying to write an Audit Log method that will log all changes
I'm trying to write (or just find an existing) PHP method that can take
I'm trying to write an extension method for objects that will dump the structure
I am trying to write a method, that takes a ComboBox, a DataTable and
I'm trying to write some C# code that calls a method from an unmanaged
Im trying to write a method that will pull out the ID from a
I'm trying to write a method that interpolates from 0 to x (position of
I am trying to write a method that uses recursion to compare the strings

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.