Click here to Skip to main content
15,903,724 members
Home / Discussions / .NET (Core and Framework)
   

.NET (Core and Framework)

 
AnswerRe: Factoring ???? Pin
Richard MacCutchan8-Mar-11 23:21
mveRichard MacCutchan8-Mar-11 23:21 
GeneralRe: Factoring ???? Pin
Johnkokk9-Mar-11 0:38
Johnkokk9-Mar-11 0:38 
GeneralRe: Factoring ???? Pin
Richard MacCutchan9-Mar-11 2:26
mveRichard MacCutchan9-Mar-11 2:26 
GeneralRe: Factoring ???? Pin
Johnkokk9-Mar-11 5:37
Johnkokk9-Mar-11 5:37 
GeneralRe: Factoring ???? Pin
Richard MacCutchan9-Mar-11 5:47
mveRichard MacCutchan9-Mar-11 5:47 
AnswerRe: Factoring ???? Pin
Luc Pattyn9-Mar-11 6:17
sitebuilderLuc Pattyn9-Mar-11 6:17 
AnswerRe: Factoring ???? Pin
musefan9-Mar-11 4:36
musefan9-Mar-11 4:36 
AnswerRe: Factoring ???? Pin
davidnz20-Mar-11 21:44
davidnz20-Mar-11 21:44 
Could be simplified, but I think this works (assumes ascending order of integer array)...

public static void TestFactors()
{
    List<string> nodes = GetFactors(160, new int[] { 2, 3, 4, 5, 6, 7, 8, 10, 15, 20, 25, 30, 40, 50, 100, 500 });
    foreach (string n in nodes)
    {
        Console.WriteLine(n);
    }
    Console.ReadKey();
}


private static List<string> GetFactors(int number, int[] possValues)
{
    List<string> nodes = new List<string>();
    for (int j = 0; j < possValues.Count(); j++)
    {
        GetFactorsInner(nodes, number, possValues, j, possValues[j], possValues[j].ToString());

    }
    return nodes;
}

private static void GetFactorsInner(List<string> nodes, int number, int[] possValues, int currIndex, int currTot, string currStr)
{
    for (int i = currIndex + 1; i < possValues.Count(); i++)
    {
        int thisTot = currTot;
        string thisStr = currStr;
        if (thisTot * possValues[i] > number)
        {
            break;
        }
        else if (thisTot * possValues[i] == number)
        {
            thisStr += thisStr == null ? possValues[i].ToString() : ", " + possValues[i].ToString();
            nodes.Add(thisStr);
            break;
        }
        else if (number % (thisTot * possValues[i]) == 0)
        {
            thisStr += thisStr == null ? possValues[i].ToString() : ", " + possValues[i].ToString();
            thisTot *= possValues[i];
            GetFactorsInner(nodes, number, possValues, i, thisTot, thisStr);
        }
    }

}


Output is

2,4,20
2,8,10
4,5,8
4,40
8,20
GeneralRe: Factoring ???? Pin
Johnkokk20-Mar-11 23:07
Johnkokk20-Mar-11 23:07 
GeneralRe: Factoring ???? Pin
davidnz21-Mar-11 12:32
davidnz21-Mar-11 12:32 
QuestionWorkflow or Windows Service??? Pin
Priya Prk7-Mar-11 23:04
Priya Prk7-Mar-11 23:04 
AnswerRe: Workflow or Windows Service??? Pin
mocastro8-Mar-11 3:13
mocastro8-Mar-11 3:13 
AnswerRe: Workflow or Windows Service??? Pin
Abhinav S14-Mar-11 18:07
Abhinav S14-Mar-11 18:07 
QuestionHow can we pick dynamic images and rotate them in J-Query Pin
Andrewnick206-Mar-11 19:35
Andrewnick206-Mar-11 19:35 
AnswerRe: How can we pick dynamic images and rotate them in J-Query Pin
Richard MacCutchan6-Mar-11 22:24
mveRichard MacCutchan6-Mar-11 22:24 
QuestionWindows service with FileSystemWatcher Pin
byka4-Mar-11 5:21
byka4-Mar-11 5:21 
AnswerRe: Windows service with FileSystemWatcher Pin
N a v a n e e t h4-Mar-11 5:30
N a v a n e e t h4-Mar-11 5:30 
GeneralRe: Windows service with FileSystemWatcher Pin
byka4-Mar-11 5:38
byka4-Mar-11 5:38 
GeneralRe: Windows service with FileSystemWatcher Pin
jschell4-Mar-11 7:32
jschell4-Mar-11 7:32 
GeneralRe: Windows service with FileSystemWatcher Pin
Eddy Vluggen4-Mar-11 12:23
professionalEddy Vluggen4-Mar-11 12:23 
AnswerRe: Windows service with FileSystemWatcher Pin
Bernhard Hiller7-Mar-11 20:31
Bernhard Hiller7-Mar-11 20:31 
QuestionConnecting SQL server 2008 to Visual studio 2008 Pin
MahaKh3-Mar-11 19:02
MahaKh3-Mar-11 19:02 
AnswerRe: Connecting SQL server 2008 to Visual studio 2008 PinPopular
Roger Wright3-Mar-11 19:47
professionalRoger Wright3-Mar-11 19:47 
GeneralRe: Connecting SQL server 2008 to Visual studio 2008 Pin
PIEBALDconsult4-Mar-11 10:28
mvePIEBALDconsult4-Mar-11 10:28 
GeneralRe: Connecting SQL server 2008 to Visual studio 2008 Pin
Roger Wright4-Mar-11 14:54
professionalRoger Wright4-Mar-11 14:54 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.