6 Replies - 10873 Views - Last Post: 14 September 2007 - 04:09 AM Rate Topic: -----

#1 danielleb3ar  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 12-September 07

Sums of Squares

Posted 12 September 2007 - 03:55 AM

please help...i'm new in JAVA and my teachers arent helping because they do not teach anything!! please help. i dont know how this is done...please post the java code of this problem....thanks!!!! :) :)

Problem 1: (Filename: HappyNumber.java)
A happy number n is a positive integer defined by the following process:
• determine the sum of the squares of the digits of n
• repeat the process to the resulting sum of squares until the result equals 1
If n reaches this point, then n is a happy number. Otherwise, it will loop endlessly in a cycle which the calculated series of sum of squares does not include 1. Such a number is an unhappy number.

Illustration:
The number 7 is a happy number:
72 = 49
42 + 92 = 97
92 + 72 = 130
12 + 32 + 02 = 10
12 + 02 = 1

The number 5 is an unhappy number:
52 = 25 42 + 22 = 20
22 + 52 = 29 22 + 0 = 4
22 + 92 = 85 42 = 16
82 + 52 = 89 12 + 62 = 37
82 + 92 = 145 32 + 72 = 58
12 + 42 + 52 = 42 52 + 82 = 89
At this point, a cycle has occurred at the number 89.

Write a program that will read as input a nonzero integer n and determine whether it is a happy number or an unhappy number. Display the sum of squares that is calculated for each iteration. Since the generation of the sequence of numbers can lead to an infinite loop (for unhappy numbers), terminate the loop after 100 iterations (for as long as the calculated result is not equal to 1) and consider the number an unhappy number.

Algorithm:
• Input the value of n
• Declare a counter to keep track of the number of iterations so far and initialize it to 0.
• Declare a “working variable” num that will be manipulated in the body of the loop. The initial value of num will be the value of n.
• Declare a variable SumSquares, which will serve as the running sum of the squares of the digits of num.
• Determine SumSquares:
 Set the value of SumSquares to 0.
 Extract the digits of num following the approach in NumberPalindrome.
 For each digit extracted, accumulate its square to SumSquares
 Stop when there are no more digits to extract.
• Display the calculated sum of squares.
• Replace the value of num with SumSquares. Increment the counter.
• Repeat the process (from the fourth step). Terminate when the counter reaches 100 or when the sum of the squares is equal to 1.
• Output whether n is a happy or an unhappy number.

Sample Input and Output:
Input N: 7
49
97
130
10
1
7 is a happy number.

Is This A Good Question/Topic? 0
  • +

Replies To: Sums of Squares

#2 alpha02  Icon User is offline

  • Sexy DIC God
  • member icon

Reputation: 47
  • View blog
  • Posts: 803
  • Joined: 20-May 06

Re: Sums of Squares

Posted 12 September 2007 - 04:08 AM

Hi and welcome to DIC! Unfortonately, we don't do other people's homework, so please post what you've done so far as well as the errors you are getting and we will help you! Thanks.
Was This Post Helpful? 0
  • +
  • -

#3 danielleb3ar  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 12-September 07

Re: Sums of Squares

Posted 12 September 2007 - 08:12 PM

View Postalpha02, on 12 Sep, 2007 - 04:08 AM, said:

Hi and welcome to DIC! Unfortonately, we don't do other people's homework, so please post what you've done so far as well as the errors you are getting and we will help you! Thanks.

this isnt homework. this was a seatwork given to me by our teacher that i could not answer. besides, i would trust you with it because a simple word as UNFORTUNATELY you could not spell...
okay, this is what i came up with and how i answered my SEATWORK A WEEK AGO:

public class HappyNUmber {
public static void main (String [] args) {
System.out.print ("Inout an integer: ");
int n = Keyboard.read.Int();

int num = n
int = sumsquares = 0;

while (num > 0) {
int digit = num % 10;
num = num/10

if (num > 0; num =10; num++) {

... there, i ended there. i do not know if it will perform the needed task...
Was This Post Helpful? 0
  • +
  • -

#4 B@LL!ST!C  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 152
  • Joined: 20-August 07

Re: Sums of Squares

Posted 12 September 2007 - 10:16 PM

First of all I think you better read the forum rules for you to know how to post properly.
Secondly, we don't make your assignment,seat work or any kind of code.
You must make an effort of making and understanding it yourself.
By then you can ask us of what help we can do.
Third, I think you better learn more about java because that code you made is just only at the beginning.
There are lots of missing parameters.
Try reading some books or looking at our tutorial or snippets area.

Naintindihan mo ba?
Was This Post Helpful? 0
  • +
  • -

#5 1lacca  Icon User is offline

  • code.rascal
  • member icon

Reputation: 44
  • View blog
  • Posts: 3,822
  • Joined: 11-August 05

Re: Sums of Squares

Posted 12 September 2007 - 10:30 PM

Quote

i do not know if it will perform the needed task...


It definitely won't.
Was This Post Helpful? 0
  • +
  • -

#6 danielleb3ar  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 12-September 07

Re: Sums of Squares

Posted 14 September 2007 - 03:20 AM

public class HappyNumber {
	static void main (String [] args) {
		Sysytem.out.print("Input an integer: ");
		int n = Keyboard.readInt();
		
		int n = num
		int sumsquares = 0;
		
		while (num > 0; num = 10) {
			int digit = num % 10;
			num = num / 10
			
			num = num * num
			digit = digit * digit
			
			sumsquares = num + digit
			
		}
		System.out.print((sumsquares == 1)?"num + "is a happy number!":"is an unhappy number...");
	}
}


HOW ABOUT THIS? PLEASE HELP WITH THE KINKS...

View PostB@LL!ST!C, on 12 Sep, 2007 - 10:16 PM, said:

First of all I think you better read the forum rules for you to know how to post properly.
Secondly, we don't make your assignment,seat work or any kind of code.
You must make an effort of making and understanding it yourself.
By then you can ask us of what help we can do.
Third, I think you better learn more about java because that code you made is just only at the beginning.
There are lots of missing parameters.
Try reading some books or looking at our tutorial or snippets area.

Naintindihan mo ba?


<edit>please post in english and use code tags</edit>

This post has been edited by PennyBoki: 14 September 2007 - 04:10 AM

Was This Post Helpful? 0
  • +
  • -

#7 PennyBoki  Icon User is offline

  • system("revolution");
  • member icon

Reputation: 53
  • View blog
  • Posts: 2,334
  • Joined: 11-December 06

Re: Sums of Squares

Posted 14 September 2007 - 04:09 AM

Hi, I edited your post since it was not in english, and added code tags which should be used when posting code.

Now your last code doesn't compile. So first before you proceed any further with your assignment, I suggest you learn: how to get the user's input, to learn the vary basic that the main() should be public, not to declare variables with identical names, to know the basic syntax of loops, especially what should be the conditions, the most basic of all to use a semicolon ; after every command(java is a compiled language), and how to print the wanted output.

Why do I suggest all these? Because you'll be seeing them a lot, so it better to get used on them as of right now.
Just trying to help.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1