Spoj FACT0 - Integer Factorization (15 digits) Solution




Problem Link:  https://www.spoj.com/problems/FACT0/ 

 Solution in C++: 

///La ilaha illellahu muhammadur rasulullah
///******Bismillahir-Rahmanir-Rahim******///
///Abul Hasnat  Tonmoy
///Department of CSE,23rd batch
///Islamic University,Bangladesh
///**********ALLAH IS ALMIGHTY************///
#include <bits/stdc++.h>
using namespace std;
void factorize(long long n)
{
    int count = 0;
    while (!(n % 2))
    {
        n >>= 1;
        count++;
    }
 
    if (count)
        cout<<"2^"<<count<<" ";
 
    for (long long i = 3; i <= sqrt(n); i += 2)
    {
        count = 0;
        while (n % i == 0)
        {
            count++;
            n = n / i;
        }
        if (count)
            cout<<i<<"^"<<count<<" ";
    }
    if (n > 2)
        cout<<n<<"^1"<<endl;;
}
 
int main()
{
    long long n;
    while(cin>>n)
    {
        if(n==0)
            break;
        factorize(n);
    }
      return 0;
}
  








No comments

Most View Post

Recent post

Codeforces Round 971 (Div. 4) 2009C. The Legend of Freya the Frog Solution

  Problem Link    https://codeforces.com/contest/2009/problem/C S olution in C++: /// Author : AH_Tonmoy #include < bits / stdc ++. h &g...

Powered by Blogger.