Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Prime factors program problem

  1. Dec 8, 2011 #1
    I'm trying to write a program that will find and then display the prime factor of a number.

    I have written this in C# but it works for some numbers but doesn't work for others?

    Code (Text):
    using System;

    class primefactors
    {
        public static void Main()
        {
            int number;
            bool isprime = true;

            Console.WriteLine("Please enter the number you wish to find the prime factors of:");

            number = int.Parse(Console.ReadLine());

            Console.WriteLine("The prime factors of {0} are;", number);

            for (int i = 2; i < number; i++)
            {
                if (number % i == 0)
                {
                    for (int j = 2; j < i; j++)
                    {
                        if (i % j == 0 && i != j)
                        {
                            isprime = false;
                        }
                    }

                    if (isprime == true)
                    {
                        Console.WriteLine(i);
                    }
                }

            }
            Console.WriteLine("Press ANY Key to continue");
            Console.ReadKey();
        }
    }
     
  2. jcsd
  3. Dec 8, 2011 #2
    Sorry I think I just fixed it

    Code (Text):

    using System;

    class primefactors
    {
        public static void Main()
        {
            int number;
            bool isprime = true;

            Console.WriteLine("Please enter the number you wish to find the prime factors of:");

            number = int.Parse(Console.ReadLine());

            Console.WriteLine("The prime factors of {0} are;", number);

            for (int i = 2; i < number; i++)
            {
                if (number % i == 0)
                {
                    for (int j = 2; j < i; j++)
                    {
                        if (i % j == 0 && j != i)
                        {
                            isprime = false;
                        }
                    }

                    if (isprime == true)
                    {
                        Console.WriteLine(i);
                    }

                    isprime = true;
                }

            }
            Console.WriteLine("Press ANY Key to continue");
            Console.ReadKey();
        }
    }
     
     
  4. Dec 8, 2011 #3
    Code (Text):
    using System;
    class primefactors {
        public static void Main() {
            int number;
            Console.WriteLine("Please enter the number you wish to find the prime factors of:");
            number = int.Parse(Console.ReadLine());
            Console.WriteLine("The prime factors of {0} are;", number);

            for ( int i = 2; i*i <= number;  i+=i=='}'-'{'?'/'/'/':1<<1 )
                if ( number % i == 0) {           // i += 1 + i & 1;
                    Console.WriteLine( i );
                    while( number%i == 0 )
                        number /= i; }
            if( number != 1 )
                Console.WriteLine( number );

            Console.WriteLine("Press ANY Key to continue");
            Console.ReadKey(); }
    }
     
    Last edited: Dec 8, 2011
  5. Dec 8, 2011 #4

    I like Serena

    User Avatar
    Homework Helper

    @Xitami: nice optimalization... but... what happens if I enter the number 4?
     
  6. Dec 8, 2011 #5
    <= :-)
     
  7. Dec 8, 2011 #6

    I like Serena

    User Avatar
    Homework Helper

Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Prime factors program problem
  1. Programming problem (Replies: 8)

  2. C program-prime (Replies: 6)

Loading...