|
Sergey Alexandrovich Kryukov wrote: This is the very basic approach in OOD every developer should know and use. I'm gonna suffice with a "No".
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
So what? Do you understand that you have confessed in your own ignorance?
If some statement makes you feel bad, it does not make this statement false. Any objections? Only, please, on the matter of the subject under the discussion.
—SASergey A Kryukov
|
|
|
|
|
Sergey Alexandrovich Kryukov wrote: Do you understand that you have confessed in your own ignorance? By not agreeing with you?
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
No, not by not agreeing with me.
—SASergey A Kryukov
|
|
|
|
|
Give up; you'll never win. It's like punching jelly (or jello if you speak American)
|
|
|
|
|
I'm trying to find the nearest number that divides evenly into a pre-defined number.
Examples:
x = 12700000 / y where y is the given number and x is the nearest number that divdes into 12700000 evenly.
y = 6250, x = 6250 (12700000 / 6250 = 2032)
y = 6350, x = 6350 (12700000 / 6350 = 2000)
y = 6260, x = 6250 (12700000 / 6260 = 2028.753994)
y = 6301, x = 6350 (12700000 / 6301 = 2015.553087)
Is there a way to do this using C#?
I'm sorry I didn't explain myself correctly. I have edited the question above.
modified 10-Nov-14 9:10am.
|
|
|
|
|
Member 11220952 wrote: Is there a way to do this using C#? Of course, that's what programming languages are for. Now what part of dividing one number into another are you having problems with?
|
|
|
|
|
In the examples you show, the .NET compiler is doing exactly what you tell it, and assuming you are using the 'Double Type, the two results you think are incorrect, are correct.
I think you have not clearly expressed what you want here; I'd guess you want:
1. ? to take two numbers and return true/false if the first number is integrally divisible by the second number.
2. ? using the same two numbers: return the nearest value to the second value that can integrally divide the first number
3. ? or ... maybe you want to return the integral dividend it the division is integral, but return the closest value that can integrally divide if the division is not integral ?
Try and restate exactly what you want, and I'm sure you'll get a helpful response.
« 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"
|
|
|
|
|
Check out the modulus % operator for this.
"There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies, and the other way is to make it so complicated that there are no obvious deficiencies. The first method is far more difficult." - C.A.R. Hoare
Home | LinkedIn | Google+ | Twitter
|
|
|
|
|
This is just off the top of my head and admittedly not very performant.
int num = 12700000;
int answer = num;
while (--answer > 1) {
if (num % answer == 0) {
break;
}
}
Console.WriteLine ((answer > 1 ) ?
string.Format ("The largest number that divides {0} is {1}.", num, answer) :
string.Format ("Sorry, {0} is a prime number.", num));
}
/ravi
|
|
|
|
|
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:
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;
}
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"
|
|
|
|
|
Thanks to Ravi and Bill for both of your answers. I was hoping there would be a way to get the answer required without checking each individual number. There are gaps between divisers of millions in some cases. I shall play about with your suggestions and see what the performance is like.
|
|
|
|
|
Just out of interest here is my modified code. It will search in a both positive and negative direction so that it not be biased either way.
private int FindNearestDiviser(int n, int d)
{
int positiveSearch = d;
int negativeSearch = d;
if (n % d != 0)
{
while (n % ++positiveSearch != 0
&& n % --negativeSearch != 0) ;
if (n % positiveSearch == 0)
{
d = positiveSearch;
}
else if (n % negativeSearch == 0)
{
d = negativeSearch;
}
}
return d;
}
|
|
|
|
|
I like this approach !
« 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"
|
|
|
|
|
hello every one m stuck in to creating application whose abstract work is to copy some file from host machine to network machins sheard folder,
i m doing something like this in c#
string lscmd = "robocopy "+ lsCurentWorkingDirectory+ " \\\\CPU219\\Test$ " + lsFileName;
ProcessStartInfo ProcessInfo;
ProcessInfo = new ProcessStartInfo("cmd.exe", "/c " + lcmd);
ProcessInfo.CreateNoWindow = true;
ProcessInfo.UseShellExecute = False;
process = Precess.Start(ProcessInfo);
process.waitforExit();
when m runing this code through windows services its get stuck at process.WaitforExit(), and when m runing this by normal console applicatin application it works well,
plz help me
|
|
|
|
|
its most likely a permissions issue (ie the service is running under an account that doesn't have permission to the share) - have a look here and see if this points you in the correct direction http://serverfault.com/questions/177139/windows-service-cant-access-network-share[^]
[edit] one thing you could do, is try using account credentials (Domain, UserName and PassWord) in ProcessInfo - use your account for the moment, but make sure you blank them out after you've tested it (just so no-one sees your credentials) - if it works, that gives you options [/edit]
|
|
|
|
|
Download video on youtube of what documentation do I need ? if there are good examples raid
|
|
|
|
|
|
This was the old material can not be used for current
|
|
|
|
|
Well, I still use this library without any problems.
|
|
|
|
|
If you have examples are included for my reference to it ?
|
|
|
|
|
The examples on the GIT still work perfectly.
the only thing I adjusted was in DownloadUrlResolver.NormalizeYoutubeUrl to also implement google search Links and extract the real url from them. Everything else still works like a charm.
Just follow the Instructions on the repository.
Greet$
|
|
|
|
|
Hi,
I am creating one desktop application.in that application i want to add item without repetition.If i add duplicate item into datagridview this item can not be add but update their existing value.please help me.
|
|
|
|
|
Search the grid for that item already existing, and add or update as necessary.
|
|
|
|
|
I have a scenario where the application starts up and spawns a worker thread. When the main thread exits, I want the PROCESS (and the worker thread) to stay alive until its done doing its thing and then the whole process can exit. So I did something like this:
Thread threadCurrent = Thread.CurrentThread;
Task.Run(() =>
{
while (threadCurrent.IsAlive || (_queue.Count > 0))
Thread.Sleep(250);
});
This does exactly what I want. When the main application exits, the process keeps running until the queue is finished processing (_queue is processed in a separate thread, this code piece keeps the process alive) and then the process shuts down.
I'm not happy with the Sleep(250) portion because that part is going for the entire lifetime of the application. It's not spinning the CPU or anything. I'm just looking for a better way to do it.
I tried threadCurrent.Join(), however, that only works if the app is running in the debugger. I don't get why. If I have a debugger attached, the Join() blocks until the main thread is done as expected. If I don't have a debugger attached, the Join() never returns.
EDIT: I should also note that this code in a library, so I can't really require the developer to do anything special in the main thread to make this work.
|
|
|
|