Click here to Skip to main content
15,890,882 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Detect whether a given positive integer N can be written as a
product of powers of 2, 3 and 5. That is, check whether N is exactly
equal to

2^i x 3^j x 5^k

where i>=0, j>=0, and k>=0 are non-negative integers.

If yes, then print
i#j#k
Otherwise, print
no

You can assume that N<9999.


What I have tried:

i tried this-

#include<stdio.h>
#include<stdbool.h>
#include<math.h>

bool isPower(int n)
{
  
  if (n <= 1) return true;
  for (int x=2; x<=sqrt(n); x++)
  {
    unsigned p = x;
    while ( p <= n)
    {
      p *= x;
      if (p==n)
        return true;
    }
  }
  return false;
}

int main()
{
  int x,n;
  for (int i=2; i<100; i++)
    if (isPower(i))
    printf("i");
    return 0;
}
Posted
Updated 14-Apr-22 20:24pm

#include <stdio.h>
void main()
{
int n, i = 0, j = 0, k = 0;
scanf("%d", &n);
while (n % 2 == 0)
{
n = n / 2;
i++;
}
while (n % 3 == 0)
{
n = n / 3;
j++;
}
while (n % 5 == 0)
{
n = n / 5;
k++;
}
if (n > 1)
printf("no");
else
printf("%d#%d#%d", i, j, k);
}
 
Share this answer
 
Read the question again: it needs you do check N for a specific condition, which has three parts: your code doesn't even try to do that.

Think about the question, the C library function - pow()[^], and how you would do it manually. Try a couple of random numbers on paper and get a "feel" for the problem first.

If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900