Click here to Skip to main content
15,888,113 members
Home / Discussions / C#
   

C#

 
AnswerRe: This is a confession Pin
Sergey Alexandrovich Kryukov10-Nov-14 13:05
mvaSergey Alexandrovich Kryukov10-Nov-14 13:05 
AnswerRe: This is a confession Pin
Richard MacCutchan10-Nov-14 21:32
mveRichard MacCutchan10-Nov-14 21:32 
QuestionHow to find the nearest number that divides evenly in to another number. Pin
Member 1122095210-Nov-14 0:03
Member 1122095210-Nov-14 0:03 
QuestionRe: How to find the nearest number that divides evenly in to another number. Pin
Richard MacCutchan10-Nov-14 2:14
mveRichard MacCutchan10-Nov-14 2:14 
AnswerRe: How to find the nearest number that divides evenly in to another number. Pin
BillWoodruff10-Nov-14 2:53
professionalBillWoodruff10-Nov-14 2:53 
AnswerRe: How to find the nearest number that divides evenly in to another number. Pin
Dominic Burford10-Nov-14 2:54
professionalDominic Burford10-Nov-14 2:54 
AnswerRe: How to find the nearest number that divides evenly in to another number. Pin
Ravi Bhavnani10-Nov-14 6:00
professionalRavi Bhavnani10-Nov-14 6:00 
AnswerRe: How to find the nearest number that divides evenly in to another number. Pin
BillWoodruff10-Nov-14 21:52
professionalBillWoodruff10-Nov-14 21:52 
Here's an idea based on the idea that your problem is one of finding the nearest prime divisor of integer 'n by integer 'd:
C#
// use a nullable int result to distinguish
// between case that d is a prime of n
// and the case that it was necessary to find a nearest divisor
private bool findNearestPrimeDivisor(int n, int d, out int? result)
{
    int mod = n % d;

    if (mod == 0)
    {
        result = null;
        return true;
    }

    bool primeFound = false;

    int prime = 0;

    for (int i = mod; i < d + mod; i++)
    {
        if (n % i == 0)
        {
            primeFound = true;
            prime = i;
            if (i > d) break;
        }
    }

    if (primeFound)
    {
        result = prime;
        return true;
    }

    result = null;
    return false;
}

// test in some method or EventHandler:

int? factor;

int testn = 12700000;

int testd = 6269;

if (findNearestPrimeDivisor(testn, testd, out factor))
{
    if (factor == null)
    {
        Console.WriteLine("{0} is an integral divisor of {1}", testd, testn);
    }
    else
    {
        Console.WriteLine("{0} is the nearest integral divisor of {1} to {2}", factor, testn, testd);  
    }
}
else
{
    Console.WriteLine("No near prime divisor found.");
}
Note: I am not advanced enough in computer science to evaluate how "robust" this may be; this is written "off the top of my head." I welcome any feedback, or correction.
« I am putting myself to the fullest possible use which is all, I think, that any conscious entity can ever hope to do » HAL (Heuristically programmed ALgorithmic computer) in "2001, A Space Odyssey"

AnswerRe: How to find the nearest number that divides evenly in to another number. Pin
Member 1122095210-Nov-14 22:49
Member 1122095210-Nov-14 22:49 
AnswerRe: How to find the nearest number that divides evenly in to another number. Pin
Member 1122095210-Nov-14 23:03
Member 1122095210-Nov-14 23:03 
GeneralRe: How to find the nearest number that divides evenly in to another number. Pin
BillWoodruff11-Nov-14 5:16
professionalBillWoodruff11-Nov-14 5:16 
QuestionAccess network drive though windows services in c# or vc++ Pin
Member 109313459-Nov-14 21:38
Member 109313459-Nov-14 21:38 
AnswerRe: Access network drive though windows services in c# or vc++ Pin
Garth J Lancaster9-Nov-14 21:54
professionalGarth J Lancaster9-Nov-14 21:54 
QuestionDownload youtube with C#2005 ? Pin
Member 24584679-Nov-14 21:20
Member 24584679-Nov-14 21:20 
AnswerRe: Download youtube with C#2005 ? Pin
M.Scheeren10-Nov-14 1:46
professionalM.Scheeren10-Nov-14 1:46 
GeneralRe: Download youtube with C#2005 ? Pin
Member 245846722-Nov-14 20:21
Member 245846722-Nov-14 20:21 
GeneralRe: Download youtube with C#2005 ? Pin
M.Scheeren23-Nov-14 19:57
professionalM.Scheeren23-Nov-14 19:57 
GeneralRe: Download youtube with C#2005 ? Pin
Member 245846726-Nov-14 20:09
Member 245846726-Nov-14 20:09 
GeneralRe: Download youtube with C#2005 ? Pin
M.Scheeren1-Dec-14 22:31
professionalM.Scheeren1-Dec-14 22:31 
QuestionHow to find duplicate Value from Datagridview Pin
Member 107170859-Nov-14 16:27
Member 107170859-Nov-14 16:27 
AnswerRe: How to find duplicate Value from Datagridview Pin
Richard MacCutchan9-Nov-14 21:48
mveRichard MacCutchan9-Nov-14 21:48 
QuestionWaiting for thread to exit? Pin
SledgeHammer018-Nov-14 17:07
SledgeHammer018-Nov-14 17:07 
GeneralRe: Waiting for thread to exit? Pin
PIEBALDconsult8-Nov-14 17:19
mvePIEBALDconsult8-Nov-14 17:19 
GeneralRe: Waiting for thread to exit? Pin
SledgeHammer018-Nov-14 17:24
SledgeHammer018-Nov-14 17:24 
GeneralRe: Waiting for thread to exit? Pin
PIEBALDconsult8-Nov-14 17:32
mvePIEBALDconsult8-Nov-14 17:32 

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.