Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3430114
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T07:09:55+00:00 2026-05-18T07:09:55+00:00

So I wrote a quick app to check my son’s homework and it appears

  • 0

So I wrote a quick app to check my son’s homework and it appears to be working fine. It takes a number, finds all the primes, then finds all the product combinations. So, for example, you pass it “210”, and it will return:
2 x 3 x 5 x 7
5 x 6 x 7
7 x 30
6 x 35
5 x 42
3 x 7 x 10
10 x 21
3 x 70
3 x 5 x 14
14 x 15
2 x 7 x 15
2 x 105
2 x 5 x 21
2 x 3 x 35

The problem I’m having is when doing large numbers. It will handle 256 (2^8) after only a moment’s hesitation, but when I do 512 (2^9), I get a OutOfMemoryException. Can someone recommend a better way of finding the product sets? My code is below.

class Program
{
    private static List<List<int>> coll = new List<List<int>>();

    public static List<int> PrimeFactors(int i)
    {
        if (i < 2)
        {
            throw new ArgumentOutOfRangeException("Numbers less than 2 don't have prime factors");
        }

        List<int> result = new List<int>();

        int divisor = 2;

        while (divisor <= i)
        {
            if (i % divisor == 0)
            {
                result.Add(divisor);
                i /= divisor;
            }
            else
            {
                divisor++;
            }
        }

        return result;
    }

    private static void GetFactors(List<int> l)
    {
        for (int i = 0; i < l.Count - 1; i++)
        {
            for (int x = i + 1; x < l.Count; x++)
            {
                List<int> loopList = new List<int>(l);
                int newProd = l[i] * l[x];
                loopList.RemoveAt(i);
                loopList.RemoveAt(x-1);
                loopList.Add(newProd);
                loopList.Sort();
                coll.Add(loopList);
                if (loopList.Count > 2)
                {
                    GetFactors(loopList);
                }
            }
        }
    }

    static void Main(string[] args)
    {
        int target = Convert.ToInt16(args[0]);
        List<int> results = PrimeFactors(target);
        results.Sort();
        coll.Add(results);
        GetFactors(results);
        List<string> factors = new List<string>();
        foreach (List<int> lst in coll)
        {
            string listString = "";
            for (int i = 0; i < lst.Count; i++)
            {
                if (i == lst.Count - 1)
                {
                    listString += String.Format("{0}", lst[i]);
                }
                else
                {
                    listString += String.Format("{0} x ", lst[i]);
                }
            }
            factors.Add(listString);
        }

        foreach (String factorString in factors.Select(x => x).Distinct())
        {
            Console.WriteLine(factorString);
        }
    }
}

EDIT: Here is the new code, based on answers below. Works for any Int16 I throw at it.

    class Program
{
    private static List<List<int>> coll = new List<List<int>>();

    public static List<int> PrimeFactors(int i)
    {
        if (i < 2)
        {
            throw new ArgumentOutOfRangeException("Numbers less than 2 don't have prime factors");
        }

        List<int> result = new List<int>();

        int divisor = 2;

        while (divisor <= i)
        {
            if (i % divisor == 0)
            {
                result.Add(divisor);
                i /= divisor;
            }
            else
            {
                divisor++;
            }
        }

        return result;
    }

    private static void GetFactors(List<int> l)
    {
        for (int i = 0; i < l.Count - 1; i++)
        {
            for (int x = i + 1; x < l.Count; x++)
            {
                List<int> loopList = new List<int>(l);
                int newProd = l[i] * l[x];
                loopList.RemoveAt(i);
                loopList.RemoveAt(x-1);
                loopList.Add(newProd);
                bool existsInCollection = false;
                foreach (List<int> existingList in coll)
                {
                    if (ListEquality(existingList, loopList))
                    {
                        existsInCollection = true;
                        break;
                    }
                }
                if (!existsInCollection)
                {
                    coll.Add(loopList);
                    if (loopList.Count > 2)
                    {
                        GetFactors(loopList);
                    }
                }
            }
        }
    }

    private static bool ListEquality(List<int> listA, List<int> listB)
    {
        if (listA.Count != listB.Count)
            return false;

        listA.Sort();
        listB.Sort();

        for (int idx = 0; idx < listA.Count; idx++)
        {
            if (listA[idx] != listB[idx])
                return false;
        }
        return true;
    }

    static void Main(string[] args)
    {
        int target = Convert.ToInt16(args[0]);
        List<int> results = PrimeFactors(target);
        results.Sort();
        coll.Add(results);
        GetFactors(results);
        foreach (List<int> lst in coll)
        {
            string listString = "";
            for (int i = 0; i < lst.Count; i++)
            {
                if (i == lst.Count - 1)
                {
                    listString += String.Format("{0}", lst[i]);
                }
                else
                {
                    listString += String.Format("{0} x ", lst[i]);
                }
            }
            Console.WriteLine(listString);
        }
    }
}

Thanks again to everyone.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T07:09:55+00:00Added an answer on May 18, 2026 at 7:09 am

    There are so many things that could be better with this program.

    Here are a few:

    • Don’t use a global (coll)
    • Don’t use bad variable names (i,l,x)
    • Don’t use List<int> when int [] would work as well or better.
    • Use join to print out your list.

    Some things that would make this program faster

    • Don’t recurse, iterate. (Hint – an abstract stack object or abstract set object would help.)
    • Don’t sort so much, should not matter and in fact might slow you down.
    • Prune your temporary storage as you go — waiting till the end to do the distinct just means you have done WAY TO MANY of the same thing multiple times. Before you add it to the collection check if it is already there.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I recently wrote a quick VB.NET app that injects a DLL into a running
i wrote a quick little application that takes a base file of code with
Wrote a quick Java proggy to spawn 10 threads with each priority and calculate
I wrote a quick program in python to add a gtk GUI to a
I wrote a quick and dirty wrapper around svn.exe to retrieve some content and
I wrote a quick Perl script to query the local DNS servers for an
I wrote a quick program which executes every statement before giving a seg fault
Hey guys i wrote a quick test. I want delete to call deleteMe which
Wrote the following in PowersHell as a quick iTunes demonstration: $iTunes = New-Object -ComObject
See also C Tokenizer Here is a quick substr() for C that I wrote

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.