473,669 Members | 2,385 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 1456
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
8381
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, but a prime number tester would also be nice. I'm dealing with numbers in the 10^6-10^8 range so it would have to fairly efficient Dag
7
7388
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 integer is prime if it is not divisible by an odd integer less than or equal to the square root of the number.) I was given this assignment in my c++ class and I'm having trouble with it. I've looked at the professors lab solutions but I just...
19
7265
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
9924
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 the following calculation: z = x! / (x- y)! The following code is my attempt and i was hoping for a point in the right direction as too where i am going wrong.. cheers
17
2499
by: arindam.mukerjee | last post by:
I was running code like: #include <stdio.h> int main() { printf("%f\n", 9/5); return 0; }
1
8760
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 program should warn the user if the
4
5218
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 and 0 if it isn't. I have spent ages just getting this code to work and i'm worried i need to rip it to bit to get this function to work ! The function is: int prime(int number) here is my code so far: #include...
59
55722
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 want. cout<<"Enter No. ";
3
8012
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 very big to be accomodated in int or long datatype. Regards, Sugandh
0
12297
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 use factorial on integers (that should be the input), the output could be a string or a double, anyway you will alwas have the factorial. The only restriction is in the input. Yes, i mean this function works fine for values up to 170! or something...
0
8894
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8803
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8587
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8658
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7407
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5682
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4384
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2792
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1787
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.