Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Displaying factors using Recursion

Reply
Thread Tools

Displaying factors using Recursion

 
 
smshinde@gmail.com
Guest
Posts: n/a
 
      07-29-2006
Hello,
I have written a Recursive function to display integer factors of an
integer.
I am not getting the exit condition for recursion.
Can anyone suggest one.

class Factorize{
static void factorize(int n){
int i=2;
while(true){

if (n%i==0)
{
System.out.print(i+",");
n=n/i;
factorize(n);
}
else i++;
}
}

public static void main(String args[]) {
int n= Integer.parseInt(args[0]);
System.out.println("Genereting Perfect Numbers....");
System.out.print(1+",");
PerfectNumber.factorize(n);
}
}


Output:
C:\sameer>java Factorize 100
Genereting Perfect Numbers....
1,2,2,5,5,
It hangs after this.

 
Reply With Quote
 
 
 
 
Andrey Kuznetsov
Guest
Posts: n/a
 
      07-29-2006
> I have written a Recursive function to display integer factors of an
> integer.
> I am not getting the exit condition for recursion.
> Can anyone suggest one.
>
> class Factorize{
> static void factorize(int n){
> int i=2;
> while(true){
>
> if (n%i==0)
> {
> System.out.print(i+",");
> n=n/i;
> factorize(n);
> }
> else i++;
> }
> }


there are 2 conditions, i must be lesser than or equals to n and n must be
greater than 0.

Andrey

--
http://uio.imagero.com Unified I/O for Java
http://reader.imagero.com Java image reader
http://jgui.imagero.com Java GUI components and utilities


 
Reply With Quote
 
 
 
 
Stefan Ram
Guest
Posts: n/a
 
      07-29-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) writes:
>Can anyone suggest one.


public class Main
{
static void printFactorsOf( final int n )
{ boolean found = false; int factor;
for( factor = 2; factor <= n && !( found = n % factor == 0 ); ++factor );
if( found )
{ java.lang.System.out.print( "*" + factor );
printFactorsOf( n / factor ); }}

public static void printTheFactorsOf( final int n )
{ java.lang.System.out.print( "1" );
printFactorsOf( n );
java.lang.System.out.println( "." ); }

public static void main( final java.lang.String[] commandLineArguments )
{ for( int n = 1; n < 20; ++n )
{ java.lang.System.out.print( n + ": " ); printTheFactorsOf( n ); }}}


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
va_arg... recursion: changing arguments and the using recursion jononanon@googlemail.com C Programming 8 04-26-2012 08:37 PM
Top Ten SEO Factors zak Java 1 02-19-2007 07:12 PM
Prime Factors Freyr C++ 7 02-28-2006 11:33 PM
Magnification Factors Gary Morrison Digital Photography 3 05-30-2005 03:43 PM
Power Factors,,,Hmmmmm Nick Beard Digital Photography 10 01-11-2005 01:17 AM



Advertisments