top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Function that returns count of prime factors of given number

+5 votes
701 views

1st Example : 18 prime factors are 2*3*3 and it returns count =2 as prime factors includes 2 and 3 only or 2nd example: 27 =3*3*3 and return count =1.

posted Jun 24, 2016 by Shivam Kumar Pandey

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

2 Answers

+2 votes
 
Best answer
int main()
{
    int numb,count1,i,j,count2;
    printf("Please enter the number who's prime factors and their count is to be determined\n");
    scanf("%d",&numb);

    count1=0;


    printf("\nThe prime factors are\n");
    for(i=2;i<numb;i++)
    {

        count2=0;
        j=2;

        while(((numb%i)==0)&&(j<i))
        {
            if((i%j)==0)
            {
                count2++;

            }

            j++;
        }


        if((count2==0)&&((numb%i)==0))
        {
            count1++;
            printf("%d\n",i);
        }
    }
    if(count1==0)
    {
        printf("None\n");
    }

    printf("So there are %d Prime factors\n", count1);

}

Ideone.com link : https://ideone.com/knarkA

answer Jun 25, 2016 by Tejas Naik
Thanks Tejas
0 votes
void primeFactors(int n)
{
    // Print the number of 2s that divide n
    while (n%2 == 0)
    {
        printf("%d ", 2);
        n = n/2;
    }

    // n must be odd at this point.  So we can skip one element (Note i = i +2)
    for (int i = 3; i <= sqrt(n); i = i+2)
    {
        // While i divides n, print i and divide n
        while (n%i == 0)
        {
            printf("%d ", i);
            n = n/i;
        }
    }

    // This condition is to handle the case whien n is a prime number
    // greater than 2
    if (n > 2)
        printf ("%d ", n);
}

Credit: http://www.geeksforgeeks.org/print-all-prime-factors-of-a-given-number/

answer Jun 24, 2016 by Salil Agrawal
Hi,thanks for your ans but you didn't get my ques.  acc to your solution eg.if num is  18 then it prints   2 ,3,3  but I am interested only in prime factors count that is 2.
But that is easy once u have prime factor, choose the unique count within the list...
Similar Questions
0 votes

Given a Calendar class (there are three fields, year, month, day) and a number of N, Implement a function that returns the calendar after N days.

Example:
Input: {2017, 3,20} and 12,
Output: {2017,4, 1}

+6 votes

How can we count Number of Non Leaf Nodes of a given Tree ?

Example
            40
            /\
           /  \
         20    60
         / \    \
       10  30   80
                 \
                 90

Answer => 4

+2 votes

it is illegal to share some certain prime numbers in US and if you do so,you will get arrested.Follow this link to get more details :
https://www.youtube.com/watch?v=LnEyjwdoj7g
My question is : it is very easy to find multiplication of two prime numbers but tough to get what are the prime numbers which multiplication results this number . Example : 7*11= 77 but you are given 77 = ? * ? where ?= two prime numbers only (ans = 7 and 11).Another example : 11*13= 143 but you need to solve it for 143 and you output : 11 13
So I need a program ,efficient one, to get solutions to such type of mathematical problem.
Adding more examples:
input:1. 21 -> 3,7
2. 189 -> 3,7,9
3.1363 -> 29,47 .... and if is not possible then print -1 example 1. 4 and its factors 2*2 where 2 is repeated so print -1
2. 9-> -1
3. 24->-1 ( prime factors are 2^3 * 3= 8*3 where 8 is not prime so print -1).
Thanks in advance.

+2 votes

suppose we have an array of N natural numbers and asks him to solve the following queries:-
Query a:- modify the element present at index i to x.
Query b:- count the number of even numbers in range l to r inclusive.
Query c:- count the number of odd numbers in range l to r inclusive.

input:
First line of the input contains the number N. Next line contains N natural numbers.Next line contains an integer Q followed by Q queries.
a x y - modify the number at index x to y.
b x y - count the number of even numbers in range l to r inclusive.
c x y - count the number of odd numbers in range l to r inclusive.
I tried to solve using simple arrays but it isn't doing well for big constraints so I thought to use other DS with efficient algorithm so please explain appropriate algorithm.Thanks in advance.

...