473,406 Members | 2,378 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

recursion

I need help,please tell me how to solve this:
prime factoring program with recursion
example:

16=2^4;
20=2^2*5
24=2^3*3;
Apr 5 '07 #1
8 1594
sicarie
4,677 Expert Mod 4TB
The experts on this site are more than happy to help you with your problems but they cannot do your assignment/program for you. Attempt the assignment/program yourself first and post questions regarding any difficulties you have or about a particular function of the code that you don't know how to achieve.

Please read the Posting Guidelines and particularly the Coursework Posting Guidlines.

Then when you are ready post a new question in this thread.

MODERATOR
Apr 5 '07 #2
this is the problem.I solve this,but without recursion.But I really don't now how to use recursion here:

Expand|Select|Wrap|Line Numbers
  1. #include<iostream.h>
  2. void main()
  3. {
  4.     int n,p;
  5.     cout<<"n=";
  6.     cin>>n;
  7.     p=2;
  8.     do{
  9.         int k=0;
  10.         while(n%p==0 && n!=1){
  11.             n=n/p;
  12.             k=k+1;
  13.         }
  14.         if (k>0)
  15.             cout<<p<<"^"<<k;
  16.         p++;
  17.     }while(n!=1);
  18. }
Apr 5 '07 #3
"thank you" for "help" i solve the problem alone
Expand|Select|Wrap|Line Numbers
  1. CODE EDITED OUT BY ADMIN
  2.  
Apr 6 '07 #4
Ganon11
3,652 Expert 2GB
It doesn't look like this problem is using recursion at all. You've put the operations inside a function, but that function does not call itself, so it is not recursive.
Apr 6 '07 #5
MMcCarthy
14,534 Expert Mod 8TB
Sorry I had to edit out the code in Post #4. Please see the FAQ on Posting guidelines for homework questions.

ADMIN
Apr 6 '07 #6
So?!!Tell me how to do that!!!


It doesn't look like this problem is using recursion at all. You've put the operations inside a function, but that function does not call itself, so it is not recursive.
Apr 6 '07 #7
OK,Sorry :-(
Sorry I had to edit out the code in Post #4. Please see the FAQ on Posting guidelines for homework questions.

ADMIN
Apr 6 '07 #8
Ganon11
3,652 Expert 2GB
Well, once you divide the number by a factor, the prime factors include the number you just divided plus the prime factors in the new number.

Example: For 16, I would start by dividing by 2. Now I know 2 is one of the factors, and I've got to find the prime factors of 16 / 2 = 8. So use the primefactor() function again.

Your end condition will be when the number is 1 - at this point, you have found all the factors.
Apr 6 '07 #9

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

Similar topics

5
by: Peri | last post by:
I'm trying to create Python parser/interpreter using ANTLR. Reading grammar from language refference I found: or_expr::= xor_expr | or_expr "|" xor_expr For me it looks like infinite recursion....
12
by: da Vinci | last post by:
Greetings. I want to get everyone's opinion on the use of recursion. We covered it in class tonight and I want a good solid answer from people in the "know" on how well recursion is accepted...
43
by: Lorenzo Villari | last post by:
I've tried to transform this into a not recursive version but without luck... #include <stdio.h> void countdown(int p) { int x;
2
by: Csaba Gabor | last post by:
I suppose spring fever has hit at alt.math.undergrad since I didn't get any rise from them when I posted this a week ago, so I am reposting this to some of my favorite programming groups: I'm...
75
by: Sathyaish | last post by:
Can every problem that has an iterative solution also be expressed in terms of a recursive solution? I tried one example, and am in the process of trying out more examples, increasing their...
19
by: Kay Schluehr | last post by:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/496691
18
by: MTD | last post by:
Hello all, I've been messing about for fun creating a trial division factorizing function and I'm naturally interested in optimising it as much as possible. I've been told that iteration in...
13
by: robert | last post by:
My code does recursion loops through a couple of functions. Due to problematic I/O input this leads sometimes to "endless" recursions and after expensive I/O to the Python recursion exception. What...
20
by: athar.mirchi | last post by:
..plz define it.
35
by: Muzammil | last post by:
int harmonic(int n) { if (n=1) { return 1; } else { return harmonic(n-1)+1/n; } } can any help me ??
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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
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
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
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...
0
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...
0
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.