Hello,

I am trying to write a algoritm that calculates the second largest divisor of a number. But i have absolutely no idea what calculations are required for this.

``````for (ulong i = 1; i < Number; i++)
{
Number = .....?
}``````

Already know the largest common diviser of 2 numbers, but i cannot find anything considering the second largest divisor of a number.

Kind regards.

Construct a list of the divisors of a number,

``````using System;
using System.Collections.Generic;

namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
int number = 143; // number input
List<int> divs = new List<int>(); // the list of divisors

for (int i = 1; i …``````

Yes Medalgod I also assumed it but the question of the OP was : but i cannot find anything considering the second largest divisor of a number. so that was what I responded to;)

When you say you want to find the second largest divisor, are you taking the number itself to be the first? I'm assuming you are, but if not i'll show you the changes you need :)

``````class Program
{
static void Main(string[] args)
{
int input = …``````

## All 10 Replies

Construct a list of the divisors of a number,

``````using System;
using System.Collections.Generic;

namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
int number = 143; // number input
List<int> divs = new List<int>(); // the list of divisors

for (int i = 1; i <= number; i++)
{
if (number % i == 0)
{
}
}
if (divs.Count == 2) Console.WriteLine("{0} is prime!", number);
for (int i = 0; i < divs.Count; i++)
{
Console.WriteLine("A divisor of {0} is : {1}", number, divs[i]);
}
}
}
}``````

Select the second largest one.

I thought you meant selecting the second largest divisor of 2 numbers, in which case something along the lines of:

``````static int GetSecondDivisor(int num1, int num2)
{
List<int> divisors1 = new List<int>();
List<int> divisors2 = new List<int>();
List<int> overlaps = new List<int>();
int divisorIndex = 2;

for (int i = num1; divisors1.Count < divisorIndex || i >= -1; i--)
{
if (i != 0 && num1 % i == 0)
{
}
}

for (int i = num2; divisors2.Count < divisorIndex || i >= -1; i--)
{
if (i != 0 && num2 % i == 0)
{
}
}

for (int i = 0; i < divisors1.Count; i++)
{
for (int j = 0; j < divisors2.Count; j++)
{
if (divisors1[i] == divisors2[j])
{
}
}
}

if (overlaps.Count < divisorIndex)
{
//error
return 0;
}
else
{
return overlaps[divisorIndex-1];

}

}``````

...would do the trick.

Yes Medalgod I also assumed it but the question of the OP was : but i cannot find anything considering the second largest divisor of a number. so that was what I responded to;)

Yes Medalgod I also assumed it but the question of the OP was : but i cannot find anything considering the second largest divisor of a number. so that was what I responded to;)

Either way we've got both grounds covered ;)

Hello,
Thanks both for replying. It was indeed the type of solution dan putted down. The calculation is indeed for 1 number.

Have 2 more questions if possible.

1) If i make the list it keeps saying: The type or namespace name 'List' could not be found (are you missing a using directive or an assembly reference?)

I am still struggling to get into the algoritm flow of programming and i have not really used lists, arrays, etc as of yet.

So my question is how would you do this without using lists or arrays or any fancy datastructures? (basicly im trying to say with just while and for loops).
The number is already set in the code for a value of 143, can also other numbers be calculated or does it has to be initialised on a other value?

I make all the algoritms in a seperate class (primary for console version uses, but also this class could be used in a windows form version without changing anything) , so i never use console.writelines etc in the algoritm.

So i get a clear view of how exactly the logaritm works, so i can study by a means i can understand.

Regards.

To use generic List you have to include the following line: using System.Collections.Generic;
The reason I put in int number = 143; is to keep it simple. Change 143 to anything you like and recompile, see the code sample below how using ReaLline from the console makes it more "complicated".
You could also have number input from a file or a TextBox in a Forms application.
If you want to use the results of your algorithm you will have to put them in some kind of datastructure anyway, in this code example without a list I have to use WriteLine. Don't see any other way to show the results to the user.

``````using System;
using System.Collections.Generic;//not needed here, but needed with List!!!

namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
int number = 0;
bool OK = int.TryParse(numstr, out number);
if (OK)
{
for (int i = 1; i <= number; i++)
{
if (number % i == 0)
{
Console.WriteLine("A divisor of {0} is : {1}", number, i);
}
}
}
else
{
Console.WriteLine(">>>ERROR in input");
}
}
}
}``````

When you say you want to find the second largest divisor, are you taking the number itself to be the first? I'm assuming you are, but if not i'll show you the changes you need :)

``````class Program
{
static void Main(string[] args)
{
int input = 268; //you can get this value from user input/database/file etc
int result;
result = SecondLargestDivisor.Calculate(input);
}
}
class SecondLargestDivisor
{
public static int Calculate(int number)
{
for (int i = number - 1; i > 0; i--)
{
if (number % i == 0)
{
return i;
}
}

return 0;
}
}``````

I have placed the method in a class and declared it as static so it can be called without instantiation. I have used a Console App but you could call it the same way from a Winform. Also, i have used a preset number for input but you can pass any value you like as the input parameter.

commented: Nice improvement! +6

@Ryshad : A very nice improvement on what I and medalgod came up with.
And completely what the OP asked I guess!

console program [1.0 marks]
Create a MaSH console program called GCD which reads two integers, x and y, and prints the largest common divisor, n. In other words, n is the largest number that will divide into both x and y. The program should run until the user quits.

07-ozo, welcome here at daniweb.
If you have a question, please post it in a new thread.