Displaying factors using Recursion

Discussion in 'Java' started by smshinde@gmail.com, Jul 29, 2006.

  1. Guest

    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.
    , Jul 29, 2006
    #1
    1. Advertising

  2. > 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
    Andrey Kuznetsov, Jul 29, 2006
    #2
    1. Advertising

  3. Stefan Ram Guest

    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 ); }}}
    Stefan Ram, Jul 29, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. zak

    Top Ten SEO Factors

    zak, Feb 19, 2007, in forum: Java
    Replies:
    1
    Views:
    456
    Andrew Thompson
    Feb 19, 2007
  2. Freyr

    Prime Factors

    Freyr, Feb 28, 2006, in forum: C++
    Replies:
    7
    Views:
    521
    Jerry Coffin
    Feb 28, 2006
  3. srinivas.hyd
    Replies:
    0
    Views:
    548
    srinivas.hyd
    Oct 26, 2007
  4. Chris Gordon-Smith
    Replies:
    2
    Views:
    244
    Miles Bader
    Sep 12, 2011
  5. Replies:
    8
    Views:
    723
    John Reye
    Apr 26, 2012
Loading...

Share This Page