473,508 Members | 2,008 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

getting prime factorial of an integer

2 New Member
Can anyone please help me with the code of prime factorization. Example input:135
The problem is I want to be an output of (3^3) (5^1) instead of 3,3,3,5.
here's the code:
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <cmath>
  4. using namespace std;
  5.  
  6. void get_divisors(int n);
  7.  
  8. int main() 
  9. {
  10.     int n = 0;
  11.  
  12.     cout << "Enter a number:";
  13.     cin >> n;
  14.     get_divisors(n);
  15.     cout << endl; 
  16. }
  17.  
  18. void get_divisors(int n)
  19.  {
  20.      int i;
  21.      double sqrt_of_n = sqrt(n);
  22.  
  23.      for (i = 2; i <= sqrt_of_n; i++)
  24.          if (n % i == 0) 
  25.          {
  26.             cout << i << ", "; 
  27.             get_divisors(n / i);
  28.             return; 
  29.                  }
  30.  
  31.  
  32.      cout << n;
  33.  }
  34.  
Sep 23 '13 #1
3 1440
Nepomuk
3,112 Recognized Expert Specialist
The reason you are only getting a list of factors is that you calculate one by one without remembering your last result. There are a few possible solutions to this but the one I'd suggest (as it needs the least changes in your code) is passing two further arguments to your get_divisors function: The last factor found and the number of times you've found it so far. Then do the output depending on those values.
Sep 23 '13 #2
Sheilani
2 New Member
thank for your reply,but i dont know how to do it.
Sep 25 '13 #3
Nepomuk
3,112 Recognized Expert Specialist
OK, here are a few hints:
  • The signature of the changed function will be something like
    Expand|Select|Wrap|Line Numbers
    1. void get_divisors(int n, int lastFactor, int power)
  • The initial call should be something like
    Expand|Select|Wrap|Line Numbers
    1. get_divisors(n, 2, 0)
  • You can reduce the number of calls in your for-loop given the new parameters
Sep 25 '13 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

36
8350
by: Dag | last post by:
Is there a python module that includes functions for working with prime numbers? I mainly need A function that returns the Nth prime number and that returns how many prime numbers are less than N,...
7
7363
by: brian.digipimp | last post by:
Write a program that prompts the user to input a positive integer. It should then output a message indicating whether the number is a prime number. (Note: An even number is prime if it is 2. An odd...
19
7235
by: shanx__=|;- | last post by:
hi i need some help regarding use of very very long integer datatype in 'c'.. i need it to store result of large number's factorial.. if someone can healp it would be a delight..
33
9901
by: patrick_woflian | last post by:
hey guys, im just writing a basic calculation at the moment, before building on it for an A-Level piece of work. i can add/divide etc... two numbers together yet i am having a major problem with...
17
2469
by: arindam.mukerjee | last post by:
I was running code like: #include <stdio.h> int main() { printf("%f\n", 9/5); return 0; }
1
8732
by: Tin | last post by:
Dear Sir/Madam, I have one assignment , which need me to write a program to calculate the factorial of an integer, then press the button that would display the result in a label.3 & the...
4
5206
by: SweetLeftFoot | last post by:
Hello, i have designed some code that works out the first 250 prime numbers and prints them to the screen. However i need to implement 2 functions, one of which returns a 1 if the number is a prime...
59
55670
by: Umesh | last post by:
i wrote the following program to calculate factorial: #include<stdio.h> #include<iostream.h> void main() { int i,n; long int p=1; // or long double p=1; for exponential result which I don't...
3
8004
by: Sugandh Jain | last post by:
Hi. How to write a function that will return me the factorial (say in a string) for the any positive integer it takes? When we find a factorial of even say 2000 or a higher number, it will be...
0
12257
kadghar
by: kadghar | last post by:
Hi, I saw that Killer posted a simple Factorial Function that allows you to calculate up to 13!, well, you can use this for bigger numbers by changing the variable type. Why is this? You can...
0
7227
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7127
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7331
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
5633
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4713
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3204
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3188
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
768
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
424
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.