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

Ugly Number With Recursion (Addition of Ugly Number Problem #1286) #1424

Merged
merged 8 commits into from
Dec 9, 2020
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
2 changes: 2 additions & 0 deletions Java/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ _add list here_
- [Happiness Problem (cp sets question)](cp/HappinessProblem.java)
- [Prime Number With Recursion](cp/PrimeRecursion.java)
- [Prime Number without Recursion](cp/Prime.java)
- [Ugly Number With Recursion](cp/UglyRecursion.java)
- [PDDI Using Recursion](cp/ArmStrongRecursion.java)

## Cryptography

Expand Down
33 changes: 33 additions & 0 deletions Java/cp/ArmStrongRecursion.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Checking Armstrong number with Recursion

import java.util.Scanner;
class ArmStrongRecursion
{
static int p;
public static int armstrong(int i)
{ if(i<10) //base class
return (int)(Math.pow(i,p));
else
return((int)Math.pow(i%10,p)+armstrong(i/10));
}
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.print("N = ");
int n=sc.nextInt();
p=Integer.toString(n).length(); //calculating the number of digits in the number by converting it into String

if(n==armstrong(n))
System.out.println(n+" is an Armstrong number");
else
System.out.println(n+" is not an Armstrong number");
}
}

/*
Sample Input and Output :
N = 153
153 is an Armstrong number
Space Complexity: O(1)
Time Complexity : O(p)
*/
48 changes: 48 additions & 0 deletions Java/cp/UglyRecursion.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
//finding nth ugly number using recursion
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm quite new to GitHub. Sorry for the troubles.
Ugly Problem link : #1286
PDDI Problem link : #1217

Copy link
Contributor Author

@ErzaTitania-2001 ErzaTitania-2001 Dec 9, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@iamrajiv @atarax665 when is this pull request going to be resolved? Should I open two new PRs for each code individually?

import java.util.Scanner;
class UglyRecursion
{

public static int ugly(int n)
{

if (n == 1) //base cases
return 1;
if (n <= 0)
return 0;
if (n % 2 == 0)
return (ugly(n / 2));

if (n % 3 == 0)
return (ugly(n / 3));

if (n % 5 == 0)
return (ugly(n / 5));

return 0;
}



public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.print("N= ");
int n=sc.nextInt();
int i=1;
while(n>0)
{ if(ugly(i)==1)
n-=1;
i++;
}
System.out.println(i--); //to avoid the last increment,we decrement by one
}
}


/* Sample Input
Input:
N = 10
Output: 12
Time Complexity : O(n)
Space Complexity : O(1)*/
33 changes: 33 additions & 0 deletions Java/math/ArmStrongRecursion.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Checking Armstrong number with Recursion

import java.util.Scanner;
class ArmStrongRecursion
{
static int p;
public static int armstrong(int i)
{ if(i<10) //base class
return (int)(Math.pow(i,p));
else
return((int)Math.pow(i%10,p)+armstrong(i/10));
}
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.print("N = ");
int n=sc.nextInt();
p=Integer.toString(n).length(); //calculating the number of digits in the number by converting it into String

if(n==armstrong(n))
System.out.println(n+" is an Armstrong number");
else
System.out.println(n+" is not an Armstrong number");
}
}

/*
Sample Input and Output :
N = 153
153 is an Armstrong number
Space Complexity: O(1)
Time Complexity : O(p)
*/