|
I found a good manual in codeproject forum, if you want I can email it to you. But I don't remember where I found it.
Sourie
|
|
|
|
|
I have this code, but I need to find out how to make it so the program will check if its a prime number or not.
For Example:
If user types Number Is prime?
1 No – by definition
2 Yes – the only even prime! ?
3 Yes
4 No
5 Yes
7 Yes
17 Yes
18 No
629 ( = 17 * 37) No
static void Main(string[] args)
{
bool current = false;
int j;
Console.WriteLine("Enter any number/integer");
int num = Int32.Parse(Console.ReadLine());
for (int i = 2; i <= num; i++)
{
for (j = 2; j < i; j++)
{
if (i % j == 0)
{
current = true;
break;
}
}
if (current == false)
Console.Write("{0} ", j);
else
current = false;
}
Console.ReadLine();
}
}
}
|
|
|
|
|
Where are you stuck? People won't do your homework for you.
"I guess it's what separates the professionals from the drag and drop, girly wirly, namby pamby, wishy washy, can't code for crap types." - Pete O'Hanlon
|
|
|
|
|
There's something pretty screwy going on in that code. You can adapt this to suit your needs...
static void Main(string[] args)
{
Console.Write("Enter any integer: ");
int inputValue;
int.TryParse(Console.ReadLine(), out inputValue);
if (isPrimeNumber(inputValue))
{
Console.WriteLine(inputValue + " is a prime number.");
}
else
{
Console.WriteLine(inputValue + " is not a prime number.");
}
Console.Write("Any key to exit... ");
Console.ReadKey();
}
public static bool isPrimeNumber(int num)
{
if (num < 2)
{
return false;
}
for (int i = 2; i < num; i++)
{
if (num % i == 0)
{
return false;
}
}
return true;
}
Paul... I'm not doing his homework - he was nearly there by himself (unless that was a non working example that he was given to fix)
|
|
|
|
|
DaveyM69 wrote: Paul... I'm not doing his homework
No worries. Your code looks like something the OP can work off of and get some well valued learning experience out of
"I guess it's what separates the professionals from the drag and drop, girly wirly, namby pamby, wishy washy, can't code for crap types." - Pete O'Hanlon
|
|
|
|
|
yep.
a major performance improvement can be obtained by replacing
for (int i = 2; i < num; i++) by
if (num==2) return true;
for (int i = 3; i*i <= num; i+=2)
i.e. only test odd dividers less/equal the square root of num.
Luc Pattyn [Forum Guidelines] [My Articles]
This month's tips:
- before you ask a question here, search CodeProject, then Google;
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get;
- use PRE tags to preserve formatting when showing multi-line code snippets.
|
|
|
|
|
Thanks a lot. I appreciate a lot. It's not a homework, because I'm learning C# by a book and there was a problem and I could not figure out. Thanks a lot Davey.
|
|
|
|
|
I wrote a prime class for myself some time ago - so I just dug this out of my archives.
It's not the best algorithm, in terms of speed, however it works.
Make sure you understand how it works because when your tutor comes to ask you how it works...
class Prime
{
public static bool isPrime(double numIn)
{
double testNum;
double testLimit;
testLimit = numIn;
testNum = 3;
if (0 == testLimit % 2)
{
return false;
}
while (testLimit > testNum)
{
if (0 == numIn % testNum)
{
return false;
}
testLimit = numIn / testNum;
testNum += 2;
}
return true;
}
}
You always pass failure on the way to success.
|
|
|
|
|
why would you use doubles both in the API and inside the method, when the problem at hand
is basically an integer one?
Luc Pattyn [Forum Guidelines] [My Articles]
This month's tips:
- before you ask a question here, search CodeProject, then Google;
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get;
- use PRE tags to preserve formatting when showing multi-line code snippets.
|
|
|
|
|
Luc Pattyn wrote: why would you use doubles both in the API and inside the method, when the problem at hand
is basically an integer one?
Possible reasons:
Because it was nearing midnight when I dug out this class that I wrote as I was learning C#.
My gerbils wrote the code.
Not all primes have yet been discovered - I may actually discover a non whole prime.
(Embarassed clearing of throat )
You always pass failure on the way to success.
|
|
|
|
|
Thanks a lot. It was very helpful.
|
|
|
|
|
Determining whether or not each number is prime as it's entered seems inefficient.
As is holding a huge Sieve that may not get fully used.
So what about a more dynamic Sieve that you only extend when a new highest number is entered?
Might that work?
|
|
|
|
|
So far the linear sieve has not been mentioned nor shown in code in this thread, and it is
not the way I would recommend to have a program determine primality for a specific number.
The sieve methods are fine to explain the concept, and to execute by hand (i.e. for small
numbers) since no divisions are required at all; successive division attempts are the normal
first-order attempt in a program.
For large numbers (say above 100 digits) completely different techniques are available
and are getting refined all the time to investigate primality; most of them use shortcuts
that reduce the computational effort tremendously at the expense of a little uncertainty
(false positives for some tests, false negatives for other tests). And special libraries
("big integer" or "multiple precision" are used to operate on those numbers).
Luc Pattyn [Forum Guidelines] [My Articles]
This month's tips:
- before you ask a question here, search CodeProject, then Google;
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get;
- use PRE tags to preserve formatting when showing multi-line code snippets.
|
|
|
|
|
hi
is that anyWay or any Compiler or ... that convert or transfer my c# code to IC or MicroController ?
thanks ..
|
|
|
|
|
Yes - it's still zeros and ones at the end of the day so could be stored on an eeprom.
What use it would be I don't know as it will never run without the Operating System plus .net framework. You would need to develop a hardware driver as well to communicate between your eeprom/microcontroller and the operating system/framework.
If you want it as a standalone thing without a computer connected then no as the operating system and .net framework need a PC to run plus attempting to copy them would be highly illegal.
|
|
|
|
|
DaveyM69 wrote: What use it would be I don't know as it will never run without the Operating System plus .net framework. You would need to develop a hardware driver as well to communicate between your eeprom/microcontroller and the operating system/framework.
You'd only need this if you were trying to run a MSCLR C# app complied to MSIL (Built in MSVS IDE) application on a µC. You could in theory write in C# syntax/style in a special IDE for a µC that translates C# syntax to machine code for your µC. I don't really see the point in such an IDE because standard C syntax is close enough for anyone who knows C# to switch to.
DaveyM69 wrote: If you want it as a standalone thing without a computer connected then no as the operating system and .net framework need a PC to run plus attempting to copy them would be highly illegal.
The C# syntax/styling isn't protected else the Mono CLR and monoIL would have been shutdown years ago, not that MS didn't try before actually supporting them...
Some things that are close would be J2ME (Java µ Edition) which is close to C# and Java...
Why not just learn C to run on a µC anyway?
|
|
|
|
|
thanks SpacixOne
you mean that i don't use any ide for programmer to microController with c# support ?
|
|
|
|
|
hello everyone ,
i would like to know if someone have any code example for the next situation :
i want to write a program that will write events to a log file.
there are 2 kind of events ( high , normal (priority))
two queues ( for each event type )
all the events will be inserted to the queue first( the queue manage the log file writing )
for each queue there is "max events per queue" parameter , wich means
that if the queue is full , i need to create a new one ( and so on ...)
each new queue will create a new thread...
all this will run with threads ( number from the user)
ofcurse that the high event will get more priority, but also take care of starvation ...
i'm lost ...
Thanks,
kris
|
|
|
|
|
kriskris wrote: i'm lost ...
Where are you lost?
"I guess it's what separates the professionals from the drag and drop, girly wirly, namby pamby, wishy washy, can't code for crap types." - Pete O'Hanlon
|
|
|
|
|
kriskris wrote: the next situation
what was the previous one?
Do rate the reply, if it helps or even if it doesnot, because it helps the members to know, what solved the issue. Thanks.
|
|
|
|
|
How can I load an assembly (dll) as non domain-neutral in my own created AppDomain?
Background:
After doing his work I have to unload these assembly by code. But jet these assembly stays in the AppDomain of my main-application.
Thanks for your help!
Olaf
|
|
|
|
|
hi all.
i write a bho to find useful hyperlink clicks of user in a page.
in other case how times user click in a page that occurred to open a new external page.
i try to do it in Befornavigate2 event. but i found when it raised I'm in new page, not in parent page .
help me where i should do it?
sepel
modified on Saturday, January 26, 2008 5:36:50 AM
|
|
|
|
|
How can I write a program that ads an item on windows right click menu?
I've searched for articles in this topic but no result...
_____________________________
...and justice for all
APe
|
|
|
|
|
This is very simple to do in .NET 2.0
From ToolBox [ Menu and ToolBar Section ]
ContextMenuStrip
Create Your Menu As simple as create normal menu bar.
Now If you want it on rightClick on an item..
e.g : ListView
Now in PropertSection , Select ContextMenuStrip and select your selected menu.
Thats all.
if you check this article http://www.codeproject.com/KB/cs/NetWorkSpy.aspx[^]
here i have used Similar thing
|
|
|
|
|
If you mean the Shell's own context menu it's a little trickier.
See here[^]
|
|
|
|