# Sums of Squares

Page 1 of 1

## 6 Replies - 12766 Views - Last Post: 14 September 2007 - 04:09 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=33093&amp;s=c20345aedb100a01ad1a9addcc831cb1&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 danielleb3ar

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

# Sums of Squares

Posted 12 September 2007 - 03:55 AM

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 Liontrue1

Reputation: 47
• Posts: 811
• 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.

### #3 danielleb3ar

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

## Re: Sums of Squares

Posted 12 September 2007 - 08:12 PM

alpha02, 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 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...

### #4 B@LL!ST!C

Reputation: 0
• 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.
There are lots of missing parameters.
Try reading some books or looking at our tutorial or snippets area.

Naintindihan mo ba?

### #5 1lacca

• code.rascal

Reputation: 44
• 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.

### #6 danielleb3ar

Reputation: 0
• 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 = 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...");
}
}

B@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.
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

### #7 PennyBoki

• system("revolution");

Reputation: 53
• Posts: 2,335
• 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.