usingMaths.com
From Theory to Practice - Math You Can Use.







<< PreviousNext >>

C# Code to List Prime Pactors of a Number - Math Project for Primary Students



What are Prime Factors? | Maths Explanation for C# Kids

Finding prime factors is all about selecting those factors of a number that are prime.
The prime factors of 36 as an example, are:

2 X 2 X 3 X 3.

In this guide, we'll thoroughly explain prime factorisation and show how to code a C# algorithm to find prime-factors in a detailed and interactive manner.
This Math exercise and C# algorithm will help young students understand prime factorization by listing all prime factors of a number.


Step-by-step Guide to Prime Factorisation of Numbers in C#

We'll go about the C# algorithm to find prime factors in a simple way:

Step 1:

Starting with 2, find the first factor of the number of interest - this factor will definitely be a prime.
Store this factor away.

Step 2:

Divide number in question by found factor.

Step 3:

Using result from Step 2 as the new number in question (number whose prime factors we are trying to find), repeat Step 1.

Step 4:

Continue recursively until we arrive at 1.

Step 5:

We'll use the square-root of number range.


Create a new C# class file; Project, Add Class.
Call it ListPrimeFactors.
Type out the adjoining C# code for listing prime factors.


All those steps in a few lines of C# code;
Now C# functions come in handy, don't they?

Note: You can comment out the C# code for the main class from the previous lesson if you have been following.


So! C# Fun Practice Exercise - List Prime Factors

As a fun practice exercise, feel free to try out your own different numbers, and see how the C# code lists the prime factors of those numbers.







C# Code for List Prime Factors class.

using System;
using System.Collections.Generic;

namespace Arithmetic_CS
{
    class ListPrimeFactors
    {
        private List<int> found_prime_factors;
        private int find_my_factors;
        private int index;

        public ListPrimeFactors(int val)
        {
            found_prime_factors = new List<int>();
            find_my_factors = val;
            // STEP 1:
            index = 2;
        }

        /**
        * Our function checks 'find_my_factors';
        * If it finds a factor, it records this factor,
        * then divides 'find_my_factors' by the found factor
        * and makes this the new 'find_my_factors'.
        * It continues recursively until all factors are found.
        * @param  - integer.
         */

        private int onlyPrimeFactors(int in_question)
        {
            int temp_limit;
            temp_limit = (int)Math.Ceiling(Math.Sqrt(in_question));

            while (index <= temp_limit)
            {
                // STEP 4:
                if (index != 1 && (in_question % index) == 0)
                { // avoid an infinite loop with the i != 1 check.
                    found_prime_factors.Add(index);
                    // STEP 2, 3:
                    return onlyPrimeFactors(in_question / index);
                }
                index++;
            }
            found_prime_factors.Add(in_question);

            return 0;
        }

        /**
         * Renders the prime factors to screen.
         * @return - a list
         */

        public List<int> getPrimeFactors()
        {
            onlyPrimeFactors(find_my_factors);
            return found_prime_factors;
        }
    }
}

C# Code for List Prime Factors - Main Class.

using System;
using System.Collections.Generic;

namespace Arithmetic
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Welcome to our demonstration sequels");
            Console.WriteLine("Hope you enjoy (and follow) the lessons.");
            Console.WriteLine("\r\n");

            /*
            * List Prime Numbers.
             */

            int candidate = 48;
            List<int> answer;

            PrimeFactors prime_factors = new PrimeFactors(candidate);
            answer = prime_factors.getPrimeFactors();

            Console.WriteLine("Prime factorising " + candidate + " gives:");
            Console.WriteLine(String.Join(" X ", answer));

        }
    }
}



<< PreviousNext >>