Skip to content

Add Happy numbers problem in "others" category #2839

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

Merged
merged 7 commits into from
Nov 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions src/main/java/com/thealgorithms/others/HappyNumbersSeq.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.thealgorithms.others;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;

public class HappyNumbersSeq {
private static final Set<Integer> CYCLE_NUMS = new HashSet<>(Arrays.asList(4, 16, 20, 37, 58, 145));

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);
}
String res = n == 1 ? "1 Happy number" : "Sad number";
System.out.println(res);
}

private static int sumSquares(int n) {
int s = 0;
for (; n > 0; n /= 10) {
int r = n % 10;
s += r * r;
}
return s;
}

private static boolean isSad(int n) {
return CYCLE_NUMS.contains(n);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package com.thealgorithms.searches;

import java.util.Scanner;

public class LinearSearchThread {
public static void main(String[] args) {
int[] list = new int[200];
for (int j = 0; j < list.length; j++) {
list[j] = (int) (Math.random() * 100);
}
for (int y : list) {
System.out.print(y + " ");
}
System.out.println();
System.out.print("Enter number to search for: ");
Scanner in = new Scanner(System.in);
int x = in.nextInt();
Searcher t = new Searcher(list, 0, 50, x);
Searcher t1 = new Searcher(list, 50, 100, x);
Searcher t2 = new Searcher(list, 100, 150, x);
Searcher t3 = new Searcher(list, 150, 200, x);
t.start();
t1.start();
t2.start();
t3.start();
try {
t.join();
t1.join();
t2.join();
t3.join();
} catch (InterruptedException e) {
}
boolean found = t.getResult() || t1.getResult() || t2.getResult() || t3.getResult();
System.out.println("Found = " + found);
}
}

class Searcher extends Thread {
private final int[] arr;
private final int left, right;
private final int x;
private boolean found;

Searcher(int[] arr, int left, int right, int x) {
this.arr = arr;
this.left = left;
this.right = right;
this.x = x;
}

@Override
public void run() {
int k = left;
found = false;
while (k < right && !found) {
if (arr[k++] == x) {
found = true;
}
}
}

boolean getResult() {
return found;
}
}