Troubleshooting Prime Factors Program

Click For Summary

Discussion Overview

The discussion revolves around troubleshooting a C# program designed to find and display the prime factors of a given number. Participants explore different implementations, optimizations, and potential issues with the code.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant shares an initial implementation of a prime factorization program but notes it works inconsistently for certain numbers.
  • A second participant proposes a modified version of the program, suggesting that the issue may have been addressed by resetting the 'isprime' variable within the loop.
  • A third participant presents an alternative implementation that uses a different approach to factorization, including a more complex loop structure.
  • Another participant questions the robustness of the provided solutions by asking what happens when the input number is 4, implying there may be edge cases not handled by the current implementations.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the effectiveness of the different implementations, and there are indications of unresolved issues, particularly regarding specific input values.

Contextual Notes

There are potential limitations in handling specific cases, such as the input number being a perfect square or other edge cases that may not be addressed by the current logic in the programs.

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:
 

Similar threads

  • · Replies 18 ·
Replies
18
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 28 ·
Replies
28
Views
5K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 97 ·
4
Replies
97
Views
10K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 21 ·
Replies
21
Views
3K