|
Hi, I am trying to write a program that finds the average of all prime numbers between 1 and 100 and prints the average to the screen.
But i can't use numbers from a predetermined prime numbers list.
Can any one help me?
Ryan H
|
|
|
|
|
Sounds like homework to me
Ill give you some help but unless you have a special problem you will have to implement it yourself.
1. To find all prime numbers between 1 and 100 you will have to make a loop and check every number if its a prime number (ok if you are clever you only test the unequal numbers).
2. To check if a number is prime you will have make another loop checking if the number to test can be divided by another number which is bigger than 1 and smaller than itself (again if you are clever you only test up to n/2).
3. To test if a number can be deivided by another one use the mod (%) operator. If a % b == 0 then the numbers can be divided.
4. Every successfully tested number could stored in an ArrayList.
5. After youve tested all numbers just loop through the list and caculate the average.
Just as a note to the perfectionists: I know there are better algorithms for that but for such small numbers that will do it perfectly well.
|
|
|
|
|
HOMEWORK!!!!!
Here is the *simplest* algorithm
i - Loop through all odd numbers. That eliminates all even numbers which by definition CANNOT be prime.
ii - Write a function that loops thru from 3 - sqrt(number) and if the mod results in 0 at any point then that number is not a prime. No need to test dividing by 1 and 2 since every number can be divided by 1 and you have eliminated even numbers so no need to divide by 2. You could go up to n/2 but it is unnecessary. eg. you are testing 47 for prime:
* sqrt(47) = 7 (roughly)
* if any number from 3-7 cannot divide 47 then it is surely a prime so u tested 5 numbers instead of 24.
iii - Store every successfully tested number in some sort of collection.
iv - Iterate thru the collection and do the average
|
|
|
|
|
Simplest? I don't understand this very much. Write a function that loops thru from 3 - sqrt(number) and if the mod results in 0 at any point then that number is not a prime. What??? This is really hard to understand I am only in my 4th week of programming and its moving to fast. I haven't done any programming before. Could you please help me?
|
|
|
|
|
Hey don't shout directly into my ear! Now im hearing a ringing allaround
And please explain why this algorithm should be easier to understand then mine
(its nearly the same except a bit more optimization)
|
|
|
|
|
I know I have to make a loop and check every number if it is a prime number,
But I'm still lost. I'm not sure on how to set it up. I don't know what an array list is either. This is only my fourth week in my programming class and I have never done any programming before, this stuff is really hard to understand.
|
|
|
|
|
Ok, as its Sunday and I have nothing really important to do...
public ArrayList FindPrimes(int maxNumber)
{
ArrayList result = new ArrayList();
for (int i = 3; i <= maxNumber; i += 2)
if (IsPrime(i))
result.Add(i);
return result;
}
public double GetPrimesAverage(int maxNumber)
{
return CalcAverage(FindPrimes(maxNumber));
}
public bool IsPrime(int number)
{
double max = Math.Sqrt(number);
for (int i = 3; i <= max; i += 2)
if (number % i == 0)
return false;
return true;
}
public double CalcAverage(ArrayList list)
{
double sum = 0;
foreach (int number in list)
sum += number;
return sum / list.Count;
}
Generally its the the algorithm I already stated (including some optimizations I initially didnt want you to take care of ).
I tried to split up the functionality. The function names should be descriptive enough to tell you want each part is doing. Note that IsPrime gives only valid results for uneven numbers.
As GetPrimesAverage gets an argument you can calculate the average for as much prime numbers as you want (cpu is the limit - dont test it with more than about 20 millions). To get the result for a maximum of 100 the following call had to be done:
GetPrimesAverage(100)
Do yourself the favor and read it carefully until you understand it. If you already know how to use a debugger it would be good if you just step through the code until you get it.
|
|
|
|
|
Dear Friends
I am developing my website in ASP.NET with C-Sharp I want to include my html file which contains the background image. how i can include the .html file in my index.aspx file.
Your quick response will be appriciated
Thanks
Murtuza.
|
|
|
|
|
hi,
take a look at <a href = "http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemwebhttpresponseclasswritefiletopic.asp" rel="nofollow">Response.WriteFile(string)</a>[<a href = "http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemwebhttpresponseclasswritefiletopic.asp" target = "_blank" rel="nofollow">^</a>]
David
Never forget: "Stay kul and happy" (I.A.)
David's thoughts / dnhsoftware.org / MyHTMLTidy
|
|
|
|
|
to obfuscate any application on dot.net by add-ins i m geting all namespace ,class, memberfuntion,datamember.... but i m not getting the local variables and local funtions and the object derived from class... how to get that..
thanks in advance
|
|
|
|
|
Hi... i am an amateur programmer trying to do a context menu for a listbox in visual studio with C#. In the listbox there are several items and the context menu popups where ever you click in the listbox. I need that the context menu pops up only when the user right clicks a selected item from the listbox.
Please help
thanks
|
|
|
|
|
That's fairly simple. Create a context menu by drag/dropping a ContextMenu from the Toolbox. Subscribe to the MouseDown event of the listbox. Check if there are any items selected (listBox.SelectedIndex != -1). If true, then call contextMenu.Popup().
Regards
Senthil
_____________________________
My Blog | My Articles | WinMacro
|
|
|
|
|
For this to function you will have to first disable the default functionality. For this set the ContextMenu property of the ListBox to null (which is default when adding it to the form).
Then subscribe to the MouseUp event of the ListBox and add the following code to it:
private void listBox1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
int index = listBox1.IndexFromPoint(e.X, e.Y);
if (listBox1.SelectedIndices.Contains(index))
contextMenu1.Show(listBox1, new Point(e.X, e.Y));
}
}
You can implement every thinkable kind of logic this way. You could even modify the ContextMenu depending on which item was clicked.
|
|
|
|
|
Hello,
I have a send method which sends data to a socket. To send the data first I read it to a byte array, but after the read it consists only of zeros. It's very strange to me because the buffer of the stream, which is a memory stream, contains the right values. I'm very confused, please help !
code:
public static void Send(Stream data)
{
byte[] byteData = new byte[data.Length];
data.Read(byteData,0, (int)data.Length);
Socket handler = state.workSocket;
handler.BeginSend(byteData, 0, byteData.Length, 0,
new AsyncCallback(SendCallback), handler);
}
|
|
|
|
|
Hmmm... the code seems definitely correct. Recheck if the buffer of the Stream is really correct (you can do this by browisng through its properties in the watch window) and do this right IN THE LINE where Read is called. This is important as the buffer could theoretically be changed from some code outside this block.
|
|
|
|
|
i have a temporary folder which contains DBF files, it might be 1 dbf file up too 40 dbf files
the structure of these DBF file are the same, i wrote a small app with C# to read these files one by one and add it to a main DBF file so i can run a report from one big DBF file. but this is a very slow solution.
this is a small example just adding two fields TYPE,EMPLOYEE
DirectoryInfo TMP_ReportDirectory = new DirectoryInfo(Application.StartupPath+"\\TMP");
FileInfo [] DbfFiles = TMP_ReportDirectory.GetFiles("*.dbf");
if (DbfFiles.Length != 0)
{
string ITMConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Application.StartupPath+ "\\TMP;Extended Properties=dBase IV";
OleDbConnection ITMConnection = new OleDbConnection(ITMConnString);
string ITMSelectQuery = "Select TYPE,EMPLOYEE from " + DbfFiles[iloop].ToString();
//MessageBox.Show(ITMSelectQuery);
OleDbCommand ITMCommand = new OleDbCommand(ITMSelectQuery,ITMConnection);
ITMConnection.Open();
OleDbDataReader ITMReader = ITMCommand.ExecuteReader();
string STOCKConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Application.StartupPath+ ";Extended Properties=dBase IV";
OleDbConnection STOCKConnection = new OleDbConnection(STOCKConnString);
while (ITMReader.Read())
{
frmwait.statusBar1.Text = " Loading . . . . . . .";
string newID = ITMReader.GetValue(0).ToString();
string newLONGNAME = ITMReader.GetValue(1).ToString();
string STOCKInsertQuery = "INSERT INTO GNDITEM.DBF (TYPE, EMPLOYEE) VALUES (" + newID +",\"" + newLONGNAME + "\")";
//MessageBox.Show(STOCKInsertQuery);
OleDbCommand STOCKInsertCommand = new OleDbCommand(STOCKInsertQuery,STOCKConnection);
STOCKConnection.Open();
STOCKInsertCommand.ExecuteNonQuery();
STOCKConnection.Close();
}
ITMConnection.Close();
ITMReader.Close();
STOCKConnection.Close();
}
|
|
|
|
|
This has to be slow. Dbf files dont have a very good performance and copying all this together for just some queries is probably a bit overhead.
Some hints you might try out:
1. Try out the Visual Fox Pro Ole Db Provider. It can be found somewhere on MSDN. I dont know it but its probably faster (I used it once and never had performance issues).
2. What kind of report are you making? Do you really need one big DBF file? Consider getting all data at once into one DataTable and then calculating whatever you need on this DataTable . You can use the UNION ALL statement to get all tables in one statement:
Select TYPE,EMPLOYEE from MyDbfFile1 UNION ALL Select TYPE,EMPLOYEE from MyDbfFile2 UNION ALL Select TYPE,EMPLOYEE from MyDbfFile3 ...
3. Use some real Database
|
|
|
|
|
thx man
i used UNION ALL STATMENT a lot faster than reading DBF files one by one and adding Records.
this code is with loop
string ITMConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Application.StartupPath + ";Extended Properties=dBase IV";
OleDbConnection ITMConnection = new OleDbConnection(ITMConnString);
string ITMSelectQuery = "Select * from "+ALOHA_PATH+"\\GNDITEM.DBF UNION ALL Select * from "+ALOHA_PATH+"\\"+datefolders.ToString()+"\\GNDITEM.DBF" ;
MessageBox.Show(ITMSelectQuery);
OleDbDataAdapter da= new OleDbDataAdapter(ITMSelectQuery ,ITMConnection);
da.Fill (dsrefresh,"GNDITEM");
dv = new DataView(dsrefresh.Tables["GNDITEM"]);
dataGrid1.DataSource = dv;
ITMConnection.Close();
i have a small problem to generate a report out of dataset, before i was using the main DBF to Generate Report. how can i solve this problem.
|
|
|
|
|
What do you mean with 'report'? Crystal report? If yes Im sorry to say I dont have much knowledge in making crystal reports
I can only help you if you state how your report creation functions so far. That includes what kind of report you are creating, what data it should present and how you currently bind the datasource to it.
|
|
|
|
|
Yup i want to Create a Crystal Report,
can i create a DBF file from my DATASET , if yes, then i can easily create a Crystal Report.
i tried to create a blank report, then set Report data source to DATASET but it didn't work.
thx Robert
|
|
|
|
|
As I said I have no experience with Crystal Report. But a quick googling showed up this.
It basically says you have to bind your report at design time to a XSD schema file and then bind the DataTable at runtime.
Hope this helps.
|
|
|
|
|
dataset i created with some merges, i want to put that dataset into another empty database that has the same datastruture already.
|
|
|
|
|
Is it possible to automatically set up a default "shortcut key" for a C# application at the time of its installation on a user machine, so that the user could launch the application by directly using the shortcut key combination such as ctrl + alt + S.
|
|
|
|
|
windows has a hot key registration process. Try the link below:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc/platform/commctls/hotkey/hotkey.asp
|
|
|
|
|
g'day folks,
well i am stuck on a problem with regards to obtaining security permissions on folders. At the moment I have created a simple treeview example which shows all sub-directories in a tree form for a particular directory.
Then I wanted to see how the application would handle a sub-directory(test1) if the current user logged on win2k box didnt have the permissions to access test1. The application throws an exception which somethign along the lines of unauthorized access etc. I changed the permissions for test1 by right clicking and accessing the security tab.
I did some googling and came across couple of links on NTFS security permissions in .NET.
http://www.gotdotnet.com/Community/UserSamples/Details.aspx?SampleGuid=e6098575-dda0-48b8-9abf-e0705af065d9
http://weblogs.asp.net/cumpsd/archive/2004/02/08/69403.aspx
My problem is that I am having a hard time understanding those links. Does anyone have an alternative tutorial that would explain things a lot clearly interms of say obtaining the current ntfs security permissisons for a selected folder ?
thanks
|
|
|
|