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 Number #815

Closed
wants to merge 7 commits into from
Closed
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
36 changes: 36 additions & 0 deletions Java/AbundantNumber.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import static java.util.stream.LongStream.rangeClosed;
public class Exercise2 {

public static void main(String[] args) {
int count_Deficient_no = 0;
int count_Perfect_no = 0;
int count_Abundant_no = 0;

for (long i = 1; i <= 10_000L; i++) {
long sum = proper_Divs_Sum(i);
if (sum < i)
count_Deficient_no++;
else if (sum == i)
count_Perfect_no++;
else
count_Abundant_no++;
}
System.out.println("Number Counting [(integers) between 1 to 10,000]: ");
System.out.println("Deficient number: " + count_Deficient_no);
System.out.println("Perfect number: " + count_Perfect_no);
System.out.println("Abundant number: " + count_Abundant_no);
}
public static Long proper_Divs_Sum(long num) {
return rangeClosed(1, (num + 1) / 2).filter(i -> num % i == 0 && num != i).sum();
}
}

/*
Time complexity : O(n)
Space complexity : O(n)

Number Counting [(integers) between 1 to 10,000]:
Deficient number: 7508
Perfect number: 4
Abundant number: 2488
*/
64 changes: 64 additions & 0 deletions Java/EvilNumber.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import java.util.*;
class EvilNumber
{
String toBinary(int n) // Function to convert a number to Binary
{
int r;
String s=""; //variable for storing the result

char dig[]={'0','1'}; //array storing the digits (as characters) in a binary number system

while(n>0)
{
r=n%2; //finding remainder by dividing the number by 2
s=dig[r]+s; //adding the remainder to the result and reversing at the same time
n=n/2;
}
return s;
}

int countOne(String s) // Function to count no of 1's in binary number
{
int c = 0, l = s.length();
char ch;
for(int i=0; i<l; i++)
{
ch=s.charAt(i);
if(ch=='1')
{
c++;
}
}
return c;
}

public static void main(String args[])
{
EvilNumber ob = new EvilNumber();
Scanner sc = new Scanner(System.in);

System.out.print("Enter a positive number : ");
int n = sc.nextInt();

String bin = ob.toBinary(n);
System.out.println("Binary Equivalent = "+bin);

int x = ob.countOne(bin);
System.out.println("Number of Ones = "+x);

if(x%2==0)
System.out.println(n+" is an Evil Number.");
else
System.out.println(n+" is Not an Evil Number.");
}
}
/**
INPUT : 15
BINARY EQUIVALENT : 1111
NO. OF 1’s : 4
OUTPUT : EVIL NUMBER

Time Complexity = O(n)
Space Complexity = O(n)

*/
42 changes: 42 additions & 0 deletions Java/HappyNumber.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
public class HappyNumber
{
//isHappyNumber() will determine whether a number is happy or not
public static int isHappyNumber(int num){
int rem = 0, sum = 0;

//Calculates the sum of squares of digits
while(num > 0){
rem = num%10;
sum = sum + (rem*rem);
num = num/10;
}
return sum;
}

public static void main(String[] args) {
int num = 82;
int result = num;

while(result != 1 && result != 4){
result = isHappyNumber(result);
}

//Happy number always ends with 1
if(result == 1)
System.out.println(num + " is a happy number");
//Unhappy number ends in a cycle of repeating numbers which contains 4
else if(result == 4)
System.out.println(num + " is not a happy number");
}
}

/*
Time Complexity: O(n)
Space Complexity: O(n)

32 is a happy number as the process yields 1 as follows

32 + 22 = 13
12 + 32 = 10
12 + 02 = 1
*/
6 changes: 6 additions & 0 deletions Java/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
Add links to your code in Alphabetical Order.
Format: -[Program name](package/name of the file)

-[AbundantNumber](AbundantNumber.java)

-[Anagrams](Anagrams.java)

-[Approximating PI value](Algorithms/approxing_pi.java)
Expand Down Expand Up @@ -61,6 +63,8 @@ Format: -[Program name](package/name of the file)

-[Equation of line ,slope ,Distance between 2 points](EqOfLine.java)

-[Evil Number](EvilNumber.java)

-[Fibonacci Search Implementation](Java/Search/FibonacciSearch.java)

-[Find middle element in a Linked List](DataStructures/Middle_element_of_a_Linked_List.java)
Expand All @@ -75,6 +79,8 @@ Format: -[Program name](package/name of the file)

-[GCD](Algorithms/GCD_1.java)

-[Happy Number](HappyNumber.java)

-[Heap Sort](Sorting/HeapSort.java)

-[Implement of Rotation of Array](Algorithms/RotateClockwise.java)
Expand Down