Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Happy numbers problem using thread #2832

Closed
louvelbr opened this issue Nov 20, 2021 · 6 comments
Closed

Happy numbers problem using thread #2832

louvelbr opened this issue Nov 20, 2021 · 6 comments

Comments

@louvelbr
Copy link
Contributor

import java.util.;
public class HappyNumSeqTest {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter number: ");
int n = in.nextInt();
while(n != 1 && !isSad(n)){
System.out.print(n+" ");
n = sumSquares(n);
}
if(n == 1) System.out.println("1 Happy number");
else
System.out.println("Sad number");
}
static int sumSquares(int n){
int s = 0;
while(n > 0){
int r = n%10;
s = s + r
r;
n = n/10;
}
return s;
}
static boolean isSad(int n){
int cycleNums[] = {4,16,20,37,58,145};
boolean found = false;
int j = 0;
while(j<cycleNums.length && !found)
if(cycleNums[j] == n)
found = true;
else
j++;
return found;
}
}

Example of output :
Enter number: 50
50 25 29 85 89 Sad number

Or

Enter number: 19
19 82 68 100 1 Happy number

@siriak
Copy link
Member

siriak commented Nov 21, 2021

The code is difficult to understand without indentation

@louvelbr
Copy link
Contributor Author

Yes sorry. Here the code with indentation :
import java.util.;
public class HappyNumSeqTest {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter number: ");
int n = in.nextInt();
while(n != 1 && !isSad(n)){
System.out.print(n+" ");
n = sumSquares(n);
}
if(n == 1) System.out.println("1 Happy number");
else System.out.println("Sad number");
}
static int sumSquares(int n){
int s = 0;
while(n > 0){
int r = n%10;
s = s + rr;
n = n/10;
}
return s;
}
static boolean isSad(int n){
int cycleNums[] = {4,16,20,37,58,145};
boolean found = false;
int j = 0;
while(j<cycleNums.length && !found)
if(cycleNums[j] == n)
found = true;
else
j++;
return found;
}
}

And here the explication about what is a happy number :
A given positive integer number n is defined to be happy if the sequence of numbers generated by taking the sum of the squares of successive terms, starting at n, eventually terminates with a term whose value is 1. Numbers that are not happy, called sad, generate an infinite sequence of cyclical terms. For example, the number 19 is happy because the sequence of terms generated by the algorithm is 19, 82, 64,100, 1. We calculate this sequence as follows:
19 => 1 ! + 9 ! = 1 + 81 = 82
82 => 8 ! + 2 ! = 64 + 4 = 68
68 => 6 ! + 8 ! = 36 + 64 = 100
100 => 1 ! + 0 ! + 0 ! = 1 + 0 + 0 = 1
All numbers that occur in the sequence are themselves happy numbers. The number of terms generated is actually quite small because the sequence converges very quickly. For example, the number 986543210 is the greatest happy number with no repeated digits but the sequence only consists of 7 terms: 986543210, 236, 49, 97, 130,10, 1. Of course numbers that are not happy are sad! But how do we distinguish between them, i.e. how do we know when to stop generating new terms if a number is
not happy. This is important because sad number sequences will never converge to 1. It turns out that sad numbers eventually generate cyclical terms and these terms can be listed. They are the numbers: 4, 16, 20, 37, 58 and 145. The following sequential single threaded program takes a number as input and prints the terms generated in determining if a number is happy. It terminates if a number generated in the sequence occurs in the sad list given above is encountered. It uses two functions: sumSquares that returns the sum of the squares of each digit in n and isSad that returns true if n occurs in the list {4,16,20,37,58,145} .

@louvelbr
Copy link
Contributor Author

seems that the indentation doesn't work with the comments so here a picture:
happynumbers

@siriak
Copy link
Member

siriak commented Nov 22, 2021

This issue is called "Happy numbers problem using thread #2832", but I don't see any multithreading in the code. Could you elaborate as to what you want to do?

@louvelbr
Copy link
Contributor Author

Oh yes sorry I mistaken this one is without thread but I have a search algorithm that is using threads below if it can be useful :
found

searcher
linearSearch2

@siriak
Copy link
Member

siriak commented Nov 23, 2021

Feel free to add any of the discussed algorithms in a PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants