The Artima Developer Community
Sponsored Link

Java Answers Forum
Novice Java question

10 replies on 1 page. Most recent reply: Dec 24, 2002 1:32 AM by V. Srikanth

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 10 replies on 1 page
Francesco Angelini

Posts: 1
Nickname: franco
Registered: Dec, 2002

Novice Java question Posted: Dec 8, 2002 4:54 AM
Reply to this message Reply
Advertisement
Hello,

I'm new to the world of Java and have been set an assignment which I seem to be having a considerable degree of difficulty in.

The task is to write a specific program that requires to test if a given number, input by a user, is a triangular number.

I would appreciate if somebody could assist me or point me in the right direction.

Franco


Ugo Posada

Posts: 37
Nickname: binaryx
Registered: Dec, 2002

Re: Novice Java question Posted: Dec 9, 2002 5:49 AM
Reply to this message Reply
That doesn't have anything to do with Java programmning does it?

What is a triangular number anyway, if you give a formal definition maybe someone would like to help you coding a Java program even if its up to you to actually think your homework's solution.

ralph malph

Posts: 5
Nickname: ollie
Registered: Dec, 2002

Re: Novice Java question Posted: Dec 9, 2002 12:16 PM
Reply to this message Reply
alright melvin! do it yourself!!!1

V. Srikanth

Posts: 9
Nickname: iamnobody
Registered: Dec, 2002

Re: Novice Java question Posted: Dec 16, 2002 2:56 AM
Reply to this message Reply
Hi Franco,

If u really wanna help, then I can give u an idea, how to go
ab't. let me know !!!

Regards,
Srikanth.

I am nobody. Nobody is perfect. So I am perfect.

kevin leahy

Posts: 5
Nickname: kevster
Registered: Dec, 2002

Re: Novice Java question Posted: Dec 18, 2002 5:04 AM
Reply to this message Reply
tri numbers:
1 = 1
1 + 2 = 3
1 + 2 + 3 = 6
1 + 2 + 3 + 4 = 10

an easy solution
have your program run through the triangular number sequence until it either gets a number that is the same as the one you're checking, or the numbers in the sequence are bigger than that number.
if the number was in the sequence the number is triangular, if not it isn't.

this is not very elegant. someone who is familar with number theory could probably provide a better algorithm, but it will work.

good luck with your program
kevin :)

Singh M.

Posts: 154
Nickname: ms
Registered: Mar, 2002

Re: Novice Java question Posted: Dec 18, 2002 5:20 AM
Reply to this message Reply
http://www.artima.com/forums/flat.jsp?forum=1&thread=2911&message=9881&redirect=true&hilite=true&q=triangular

Joyce Ann

Posts: 5
Nickname: newtoforum
Registered: Nov, 2002

Re: Novice Java question Posted: Dec 18, 2002 8:02 AM
Reply to this message Reply

import java.io.*;
public class TriangularNumber { // begin class
public static void main(String[] s) throws IOException { // begin main
BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));

System.out.println("enter n number : " );
int n = Integer.parseInt(stdIn.readLine());
if (checkTriangular(n))
System.out.println(n + " is a triangular number.");
else
System.out.println(n + " is not a triangular number.");
} // end main

/* return true if the argument nth is triangular
otherwise false
*/
public static boolean checkTriangular(int nth){ // begin
int sum = 0;
boolean isTriangular = false;
for (int i = 1; sum < nth; i++) { // begin for
sum += i;
if(sum == nth)
isTriangular = true;
} // end for
return isTriangular;
} // end
} // end class

V. Srikanth

Posts: 9
Nickname: iamnobody
Registered: Dec, 2002

Re: Novice Java question Posted: Dec 18, 2002 9:36 PM
Reply to this message Reply
Hi,

I would rather give different solution to this problem:

We know any triangle # , say t = n * (n+1) / 2
=> n*n + n -2t = 0

Solving this quadratic equations for 'n', and if we find
that any of the two roots of the above eqn is a positive
integer, then we say that 't' is a triangle number.

-Srikanth.

Joyce Ann

Posts: 5
Nickname: newtoforum
Registered: Nov, 2002

Re: Novice Java question Posted: Dec 19, 2002 8:22 PM
Reply to this message Reply
can you implement your idea. or rather can you create the method you stated.

V. Srikanth

Posts: 9
Nickname: iamnobody
Registered: Dec, 2002

Re: Novice Java question Posted: Dec 23, 2002 11:19 PM
Reply to this message Reply
Hi Joyce,

Yes, I can create a method to find it out.

We knew that quad. eqn was : n*n + n - 2 *t = 0
=> For finding roots, a = 1, b = 1, c = 2*t
=> Roots r1 = (sqrt(1+8*t) - 1)/2
r2 = (sqrt(1+8*t) + 1)/2

With this we can go ahead with the program. Following is the working program:

// Finds if a given number is a triangle number or not.
 
import java.io.*;
 
public class TriangularNumber {
        public static void main(String[] s) throws IOException {
 
                BufferedReader stdIn =
                        new BufferedReader(new InputStreamReader(System.in));
                System.out.println("enter a number : " );
                int n = Integer.parseInt(stdIn.readLine());
 
                if (checkTriangular(n))
                        System.out.println(n + " is a triangular number.");
                else
                System.out.println(n + " is not a triangular number.");
        }
 
        public static boolean checkTriangular(int num) {
                float r1 = 0, r2 = 0;
                float det = 0;
 
                det = (float)Math.sqrt(1 + 8 * num);
 
                if (det < 0) // Imaginary roots, => not Triangle #
                        return false;
 
                r1 = (det - 1) / 2;
                r2 = (det + 1) / 2;
 
                if (r1 > 0) {
                        if (r1 == (int)r1)
                                return true; // Triangle #
                }
 
                if (r2 > 0) {
                        if (r2 == (int)r2)
                                return true; // Triangle #
                }
 
                return false; //  not Triangle #
        }
}


Now show me the money :-)))))

V. Srikanth

Posts: 9
Nickname: iamnobody
Registered: Dec, 2002

Re: Novice Java question Posted: Dec 24, 2002 1:32 AM
Reply to this message Reply
Minor problem with the above code, follg is the correction:

                float d = 0, det = 0;
                det = (1+8*num);
 
                if (det < 0) // Imaginary roots, => not Triangle #
                        return false;
 
                d = (float)Math.sqrt(det);
 
                r1 = (d - 1) / 2;
                r2 = (d + 1) / 2;


Thanks,
Srikanth.

Flat View: This topic has 10 replies on 1 page
Topic: Measuring CPU, Memory Usage and I/O in Java Previous Topic   Next Topic Topic: Calculator

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use