Checking if number is ugly - java

I'm doing this problem:
Write a program to check whether a given number is an ugly number.
Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 6, 8 are ugly while 14 is not ugly since it includes another prime factor 7.
Note that 1 is typically treated as an ugly number.
Here's my attempt:
public class Solution {
public boolean isUgly(int num) {
if (num == 1) {
return true;
}
for (int i = 7; i <= num / 2; i++) {
if (isPrimeFactor(i, num)) {
return false;
}
}
return true;
}
public boolean isPrimeFactor(int candidate, int num) {
return isPrime(candidate) && isFactor(candidate, num);
}
public boolean isPrime(int num) {
if (num == 2) {
return true;
}
if (num % 2 == 0) {
return false;
}
for (int i = 3; i <= Math.sqrt(num); i += 2) {
if (num % i == 0) {
return false;
}
}
return true;
}
public boolean isFactor(int candidate, int num) {
return (num % candidate == 0);
}
}
Unfortunately, it fails on test input -2147483648. It returns true when it should be false.
Any idea what I've done wrong?

You simply forgot the following emphasized condition:
Ugly numbers are positive numbers whose prime factors only include 2, 3, 5.
Therefore, you just need to add a check for negative numbers inside your isUgly method:
if (num <= 0) {
return false;
}
As a side-note, you could perhaps improve a little the performance by swapping the conditions inside isPrimeFactor and testing isFactor(candidate, num) && isPrime(candidate) instead of isPrime(candidate) && isFactor(candidate, num). This is because it is faster to determine whether a number is a factor of another than determining if a number is a prime number.

I could propose a different but a lot faster solution O(logn) for this problem:
public static boolean isUgly(int num) {
if (num < 1) return false;
int temp;
do {
temp = num;
if (num % 2 == 0) num /= 2;
if (num % 3 == 0) num /= 3;
if (num % 5 == 0) num /= 5;
} while (temp != num);
return num == 1;
}
or an even faster approach in terms of modular checks (by splitting the do while loop):
public static boolean isUgly(int num) {
if (num < 1) return false;
int temp;
do {
temp = num;
if (num % 2 == 0) num /= 2;
} while (temp != num);
do {
temp = num;
if (num % 3 == 0) num /= 3;
} while (temp != num);
do {
temp = num;
if (num % 5 == 0) num /= 5;
} while (temp != num);
return num == 1;
}

Related

Print numbers in Words [duplicate]

This question already has answers here:
What is a debugger and how can it help me diagnose problems?
(2 answers)
Closed 2 years ago.
I am trying to print numbers in words. The code I have written is pretty much covering a lot of scenarios but not this: 100 (or 10). It should print One Zero Zero but its only printing one. Can someone please help.
public static void numberToWords (int number) {
if (number < 0) {
System.out.println("Invalid Value");
}
if (number==0){
System.out.println("Zero");
}
number = reverse(number);
int modulus = 0;
while (number > 0) {
if (number % 10 == 0) {
System.out.println("Zero"); } else if (number % 10 == 1) { System.out.println("One"); } else if (number % 10 == 2) {
System.out.println("Two"); } else if (number % 10 == 3) { System.out.println("Three");} else if (number % 10 == 4) {
System.out.println("Four");} else if (number % 10 == 5) { System.out.println("Five");} else if (number % 10 == 6) {
System.out.println("Six");} else if (number % 10 == 7) { System.out.println("Seven");} else if (number % 10 == 8) {
System.out.println("Eight");} else if (number % 10 == 9) { System.out.println("Nine");}
number/=10;
}
}
public static int reverse (int number) {
int reverse = 0;
while (number != 0) {
int lastdigit = (number % 10);
reverse*=10;
reverse +=lastdigit;
number/=10;
}
return reverse;
}
public static int getDigitCount (int number) {
if (number < 0) {
return -1;
}
if (number==0){
return 1;
}
int count = 0;
int last = 0;
while (number > 0) {
last = number % 10;
count++;
number/=10;
}
return count;
}
When you reverse 100, it becomes 1. so you output it "one".
Instead of doing all this thing, Just convert the number into a string and then char array. And then loop on this array

Miller-Rabin Primality Test Often Returns Composite for Prime Numbers

I have been trying to implement a Miller-Rabin primality test from scratch (only primitives and Strings) that works for 64 bit integers (longs). I've tried the Java and pseudocode from Wikipedia, as well as various other websites. So far, only very small numbers have worked correctly. Most numbers are incorrectly marked composite, such as 53 or 101. I have tried tracking various sections of the code to see where the problem is. It seems to be in the innermost loop. I don't know what the specific issue is. Any help is appreciated. Thanks!
Here is my code:
public class PrimeTest
{
public static void main(String[] args)
{
PrimeTest app = new PrimeTest();
}
private PrimeTest()
{
long n = 53; // Change to any number. 53 is prime, but is reported as composite
if (checkPrime(n, 10))
{
System.out.println(n + " is prime.");
}
else
{
System.out.println(n + " is not prime.");
}
}
// Check if n is prime with 4^(-k) change of error
private boolean checkPrime(long n, int k)
{
// Factor n-1 as d*2^s
long d = n - 1;
int s = 0;
while (d % 2 == 0)
{
d /= 2;
s++;
}
// Repeat k times for 4^-k accuracy
for (int i = 0; i < k; i++)
{
long a = (long) ((Math.random() * (n - 3)) + 2);
long x = modPow(a, d, n);
if (x == 1 || x == (n - 1))
{
continue;
}
int r;
for (r = 0; r < s; r++)
{
x = modPow(x, 2, n);
if (x == 1)
{
return false;
}
if (x == (n - 1))
{
break;
}
}
if (r == s)
{
return false;
}
}
return true;
}
// Return (base^exp) % mod
private long modPow(long base, long exp, long mod)
{
if (mod == 1)
{
return 0;
}
long result = 1;
base = base % mod;
while (exp > 0)
{
if ((exp & 1) == 0)
{
result = (result * base) % mod;
}
exp = exp >> 1;
base = (base * base) % mod;
if (base == 1)
{
break;
}
}
return result;
}
}
This line in modPow:
if ((exp & 1) == 0)
is wrong and should instead be
if ((exp & 1) == 1)

Prime Number test in java

public static boolean isPrime(int number)
{
boolean result = true;
if (number == 0)
{
result = false;
}
for (int i=2; i < number/2; i++)
{
if (number % i == 0)
{
result = false;
}
}
return result;
}
Any ideas why when int number = 4, the result returns as true? What can I do to fix this? I am happy with the code I have but why does 4 return as true?
for (int i=2; i < number/2; i++)
If you enter 4 here, it will never enter the loop because
2 < 4 / 2
never equates to true (2 is not smaller than 2).
Instead, use <=.
Effective way how to do this method is :
(sorry for duplication, but after some time, someone can find this topic and not previous one)
public static boolean isPrime(int number) {
//Everything less or equal 1 is not prime number
if (number <= 1) {
return false;
}
//2 is very special case, so I check it separately
if (number == 2) {
return true;
}
//This will help me rid off all even numbers
if (number % 2 == 0) {
return false;
}
//It is important to count the sqrt before using it in for-loop condition.
//If you use it in for-loop condition, it will be counted every single iteration.
int square = (int) Math.sqrt(number);
//I already checked %2, so now I need to check only odd numbers
for (int i = 3; i <= square; i += 2) {
if (number % i == 0) {
//If I find one number, I do not have to continue
return false;
}
}
return true;
}
Your for loop never executes when number = 4. This is because:
for (int i=2; i < number/2; i++)
Executes when i == 2 and i < 2. That will never happen if number == 4, because then it'll be i < 4 / 2 which is i < 2. To solve this, remove the /2 or do something else... not quite sure what you're going for there.
When number==4 the boolean condition of the first if is false and the first evaluation of the for condition is also false. So the result is the value you used to initialize result (true).
Also, you might want to return the result instead of storing it in a variable because the code will continue to execute after the first conditional... So here is the working solution:
public static boolean isPrime(int number) {
if (number <= 1) { /* Since 1 isn't technically a prime number */
return false;
}
for (int i=2; i <= number/2; i++) {
if (number % i == 0) {
return false;
}
}
return true;
}
Another solution:
public static boolean isPrime(int number) {
if (number < 2) {
return false;
}
for (int i=2; i <= (int)Math.sqrt(number); i++) {
if (number % i == 0) {
return false;
}
}
return true;
}
Better and faster version of your code:
public static boolean isPrime(int number) {
if (number <= 1) {
return false;
}
for (int i=2; i*i <= number; i++) {
if (number % i == 0) {
return false;
}
}
return true;
}

Trouble with factor generator

I'm having some trouble in completing this factor generator from my programming class. It's supposed to take a number, and print out all the factors using the nextFactor method. When I set the number to factor to let's say 150, it prints out "1 2 3 5", where it's supposed to print "2 3 5 5". So, where should I go from here? I've looked at Java - Factor Generator program nextfactor method, but it didn't awnser any of my inqueries
public class FactorGenerator
{
//user inputs int from scanner in FactorTester class
public FactorGenerator(int i)
{
num = i;
}
//Checks to see if num can be factored, but does not factor it.
//Goes through all possible factors of num and returns true if the remainder == 0
public boolean hasMoreFactors()
{
for(int i = 1; i < num; i++)
{
//check if the remainder is anything other then 0
if(num % i == 0)
{
return true;
}
}
return false;
}
//Actually factors num and prints out the factor at the end of every loop.
public void nextFactor()
{
for(int i = 1; i < num; i++)
{
//check if the remainder is anything other then 0
if(num % i == 0)
{
System.out.println(i);
num /= i;
}
}
System.out.println("Done.");
}
private int num;
}
try this factors can duplicate so you need to loop until you have extracted all the instances of that factor
public void nextFactor()
{
for(int i = 2; i <= num; i++)
{
//check if the remainder is anything other then 0
while (num >= i && num % i == 0)
{
System.out.println(i);
num /= i;
}
}
System.out.println("Done.");
}
an alternative way is to do the increment in the body of the loop
public void nextFactor()
{
for(int i = 2; i <= num;)
{
//check if the remainder is anything other then 0
if (num % i == 0)
{
System.out.println(i);
num /= i;
} else {
i++;
}
}
System.out.println("Done.");
}
For starters, it will always print out 1 because any integer / 1 will always have remainder of zero. You can start i from 2 instead of 1 in your for if you want to skip 1.
I'd suggest something like this: (note this is based in part on BevynQ's answer below):
for(int i = 2; i <= num; i++){
while (num >= i && num % i == 0) {
System.out.println(i);
num /= i;
}
}

Writing A Recursive Function That Counts Zeros

It is possible to count the number of zeros in an integer through a recursive method that takes a single int parameter and returns the number of zeros the parameter has.
So:
zeroCount(1000)
Would Return:
3
You can remove the last digit from an integer by doing: "12345 / 10" = 1234
You can get the last digit from an integer by doing: "12345 % 10" = 5
This is what I have so far:
public static int zeroCount(int num)
{
if(num % 10 == 0)
return num;
else
return zeroCount(num / 10);
}
Does anyone have any suggestions or ideas for helping me solve this function?
public static int zeroCount(int num)
{
if(num == 0)
return 0;
if(num %10 ==0)
return 1 + zeroCount(num / 10);
else
return zeroCount(num/10);
}
this would work
Run through your code in your head:
zeroCount(1000)
1000 % 10 == 0, so you're going to return 1000. That doesn't make sense.
Just pop off each digit and repeat:
It sounds like homework, so I'll leave the actual code to you, but it can be done as:
zeroes(0) = 1
zeroes(x) = ((x % 10 == 0) ? 1 : 0) + zeroes(x / 10)
Note that without the terminating condition, it can recurse forever.
There are three conditions here:
1. If number is single digit and 0 , then return 1
2. If number is less than 10 i.e. it is a number 1,2,3...9 then return 0
3. call recursion for zeros(number/10) + zeros(n%10)
zeros(number){
if(number == 0 ) //return 1
if(number < 10) //return 0
else
zeros(number/10) + zeros(number%10)
}
n/10 will give us the n-1 digits from left and n%10 gets us the single digit.
Hope this helps!
Check this out for positive integers:
public static int zeroCount(int number) {
if (number == 0) {
return 1;
} else if (number <= 9) {
return 0;
} else {
return ((number % 10 == 0) ? 1 : 0) + zeroCount(number / 10);
}
}
it is a simple problem and you don't need to go for recursion
I think a better way would be converting the integer to a string and check for char '0'
public static int zeroCount(int num)
{
String s=Integer.toString(num);
int count=0;
int i=0;
for(i=0;i<s.length;i++)
{
if(s.charAt(i)=='0')
{
count++;
}
}
return count;
}
You have to invoke your recursive function from both if and else. Also, you were missing a Base Case: -
public static int zeroCount(int num)
{
if(num % 10 == 0)
return 1 + zeroCount(num / 10);
else if (num / 10 == 0)
return 0;
else
return zeroCount(num / 10);
}
import java.util.*;
public class Count
{
static int count=0;
static int zeroCount(int num)
{
if (num == 0){
return 1;
}
else if(Math.abs(num) <= 9)
{
return 0;
}
else
{
if (num % 10 == 0)
{ // if the num last digit is zero
count++;
zeroCount(num/10);
} // count the zero, take num last digit out
else if (num%10 !=0){
zeroCount(num/10);
}
}
return count;
}
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Input: ");
int num = sc.nextInt();
System.out.println("Output: " +zeroCount(num));
}
}
public static int count_zeros(int n)
{
if(n<=9)
{
if(n==0)
{
return 1;
}
else
{
return 0;
}
}
int s=n%10;
int count=0;
if(s==0)
{
count=1;
}
return count+count_zeros(n/10);
}
int countZeros(int n){
//We are taking care of base case
if(n<=9){
if(n==0){
return 1;
}
else
{
return 0;
}
}
int last=n%10; //last element of number for e.g- 20403, then last will give 3
int count=0; //Initalsizing count as zero
if(last==0){ //We are checking either the last digit is zero or not if it will
will update count from 0 to 1
count=1;
}
return count+countZeros(n/10); //Recursive call
}
int check(int n){
if(n==0)
return 1;
return 0;
}
int fun(int n)
{
if(n/10==0)
{
if(n==0){
return 1;
}
else{
return 0;
}
}
return check(n%10)+fun(n/10);
}
Check this out, this is the solution I came up with.
int countZeros(int input){
//base case
if(input == 0){
return 1;
}
int count = 0;
int lastDigit = input%10;
if(lastDigit == 0){
count = 1;
}
//calc the smallInput for recursion
int smallInput = input/10;
//set smallAns = 0, if i/p itself is not 0 and no 0 is present then return smallAns = 0
int smallAns = 0;
//recursion call
if(smallInput != 0){
smallAns = countZerosRec(smallInput);
}
//if we get lastDigit = 0 then return smallAns + 1 or smallAns + count, else return smallAns
if(lastDigit == 0){
return smallAns+count;
}
else{
return smallAns;
}}
You know that x % 10 gives you the last digit of x, so you can use that to identify the zeros. Furthermore, after checking if a particular digit is zero you want to take that digit out, how? divide by 10.
public static int zeroCount(int num)
{
if(num == 0) return 1;
else if(Math.abs(num) < 9) return 0;
else return (num % 10 == 0) ? 1 + zeroCount(num/10) : zeroCount(num/10);
}
I use math.Abs to allow negative numbers, you have to import java.lang.Math;
static int cnt=0;
public static int countZerosRec(int input) {
// Write your code here
if (input == 0) {
return 1;
}
if (input % 10 == 0) {
cnt++;
}
countZerosRec(input / 10);
return cnt;
}
CPP Code using recursion:
int final=0;
int countZeros(int n)
{
if(n==0) //base case
return 1;
int firstn=n/10;
int last=n%10;
int smallop=countZeros(firstn);
if(last==0)
final=smallop+1;
return final;
}
int countZeros(int n)
{
if(n==0)
{
return 1;
}
if(n<10) // Needs to be java.lang.Math.abs(n)<10 instead of n<10 to support negative int values
{
return 0;
}
int ans = countZeros(n/10);
if(n%10 == 0)
{
ans++;
}
return ans;
}
public static int countZerosRec(int input){
// Write your code here
if(input == 0)
return 1;
if(input <= 9)
return 0;
if(input%10 == 0)
return 1 + countZerosRec(input/10);
return countZerosRec(input/10);
}

Categories

Resources