Troubleshooting Prime Factors Program

AI Thread Summary
The discussion revolves around writing a C# program to find and display the prime factors of a given number. The initial code provided has issues, as it works for some numbers but fails for others. The main problem identified is the handling of the prime-checking logic, which was corrected in subsequent iterations of the code. The final version of the program optimizes the factorization process by using a loop that checks divisibility up to the square root of the number, improving efficiency. However, a concern is raised about the handling of specific cases, such as when the input is 4, indicating that further testing and refinement may be necessary to ensure the program works correctly for all inputs.
rollcast
Messages
403
Reaction score
0
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:
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();
    }
}
 
Technology news on Phys.org
Sorry I think I just fixed it

Code:
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();
    }
}
 
Code:
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:
@Xitami: nice optimalization... but... what happens if I enter the number 4?
 
<= :-)
 
:wink:
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

Back
Top