cprimesfunction-definitionfactorshamming-numbers# there's a problem when dealing with prime factorization

I've done this exercise, it was about finding if a number is *"ugly"* or not. A number is ugly if it has only 2, 3, or 5 as factors. otherwise, it's not ugly.

this is my solution:

```
include <stdbool.h>
#include <math.h>
bool is_prime(unsigned int num) {
bool result = false;
for (unsigned int i = 2; i <= sqrt(num); i++) {
if (num % i == 0) {
result = false;
}
else {
result = true;
}
}
return result;
}
bool is_ugly(unsigned int num) {
bool result = false;
for (unsigned int i = 1; i <= num; i++) {
if (num % i == 0) {
if (is_prime(i)) {
if (i != 2 || i != 3 || i != 5) {
result = false;
break;
}
else {
result = true;
break;
}
}
}
}
return result;
}
int main(void) {
bool result = is_ugly(30); // it must return true;
return 0;
}
```

is_ugly function works like this:

- it finds a factor and then it uses is_prime function to find if it's a prime factor. Then, I've thought that if the final number isn't equal to 2 or 3 or 5 (it's boolean logic), then it's false, because that means there is at least another number and it makes the initial expression (recall, A number is ugly if it has only 2, 3, or 5 as factors.) false.
, because when I run the algorithm with 30 it returns false, when it should've returned true because 30 = 2 * 3 * 5.**But it doesn't work****Why?**

Solution

Your approach is too complicated.

All you need is to check whether a number contains only divisors 2, 3 and 5.

So the function `is_prime`

is just redundant. Moreover it has a bug because for the number 2 it returns false.

The function is_ugly can look very simply. For exampele

```
bool is_ugly( unsigned int n )
{
if ( n != 0 )
{
n = max_divide( n, 2 );
n = max_divide( n, 3 );
n = max_divide( n, 5 );
}
return n == 1'
}
```

In turn the called function max_divide can look the following way

```
unsigned int max_divide( unsigned int n, unsigned int divisor )
{
if ( n != 0 )
{
while ( n % divisor == 0 )
{
n /= divisor;
}
}
return n;
}
```

- Passing the function *cmp(const *void, const *void) as a parameter in C and using it to create a queue?
- Is it safe to use `strstr` to search for multibyte UTF-8 characters in a string?
- Why am I getting a heap-use-after-free error?
- Catch and compute overflow during multiplication of two large integers
- Access a global static variable from another file in C
- "this" pointer in C (not C++)
- Unknown Output of C code
- Yocto- gcc cannot find shared library
- Artifact in one of the triangles in basic OpenGL texture quadrilateral
- How to check if a pointer is valid?
- cJSON Key-Value Parsing
- hi, I am emplementing LZW here and it work great, the problem is that I cant scan and encode a string if it have a (space)
- pthread clean up int/array/struct type variables using pthread_cleanup_push
- Standard way of getting a bit pattern of all ones
- Why doesn't the use of strictly conforming programs and conforming implementations ensure absolute portability?
- Checking emptiness of X macros
- Is it possible to run WebAssembly code async?
- C suddenly messing up values in 2d array
- Socket opening macOS assembly
- GDB outputs arm mnemonics
- What kind of optimization does const offer in C/C++?
- wcstombs segmentation fault
- Fast implementation of complementary error function with 5-digit accuracy
- Ascii to binary calculation is flawed
- what stops GCC __restrict__ qualifier from working
- Why my sig_int() function can't prevent my function from exit in c?
- Allocating user space memory in Linux Kernel
- Determining realloc() behaviour before calling it
- Is it legal to cast a struct to an integer if it has no padding?
- C macro, something weird