Scripting Exercise: Program calculating prime numbers [C#]

Updated on May 4, 2017 in  [S] Works in progress
Share on Facebook0Tweet about this on Twitter0Share on Google+0Share on Reddit0
10 on April 25, 2017

Hey there,

Introduction:
I have always been fascinated with prime numbers. The simple fact that every single number can be broken down into them but they can’t be broken down anymore always was intriguing to me. Their importance in encryption and that there is no formula (yet, although I believe there never will be) to reliably calculate new prime numbers also fueled this fascination.

So from the beginning for programing exercises I tasked myself with I created programs that were calculating prime numbers. But usually I stopped at some point after getting them to work a little because the process of calculating prime numbers is a bit let’s say memory consuming. However this time I actually took it so far that the calculated prime numbers are saved into a .txt file and then read from that at the next start of the program.

This little program obviously has little to no applicable uses since average computers are just not equipped to calculate large numbers of big prime numbers in a reasonably fast way. But maybe for whatever reason one of you needs a list of medium sized prime numbers for whatever reason. If that is the case feel free to use my program to calculate them, or use the provided list, although I don’t guarantee, that it is fully correct since it was partially calculated with earlier versions of the program. As far as I can judge the numbers seem to be in order and complete, though. I just can’t be asked to check 1 million + numbers manually.

Why am I sharing this here:
As a rather unexperienced coder I am sure I can benefit from the experience and opinions of individuals more accustomed to coding. I am simply hoping for some feedback on how to improve the code and my coding skills in general.

About the program:
Facts:

Current Version: 1.3.1
Current Features:
– Calculating prime numbers
– Saving prime numbers into data base
– Reading prime numbers from data base
– Check whether certain number is a prime number
– Feature to continuously calculate prime numbers until stopped by user
– Checking data base for errors and clearing these

Short Explanation:
On first use the program creates the “PrimeNumbers.txt” file with the numbers 2 and 3 saved into in the same directory as it is located. It then or at each restart reads from this data base to include previously calculated prime numbers into a list, which then can either be read from or is used to calculate new prime numbers. The amount of prime number calculated in one sitting is manually chosen by the user. The newly calculated prime numbers are then saved into the .txt file created prior.

Future Features:
– Graphical User Interface (I have started the work on this but currently I am working on other optimizations)

Download:
The link leads to a Google Drive folder which contains the current version of the .exe file, the source code as .cs file and the PrimeNumbers.txt file with currently 12,000,000 calculated prime numbers (date: 04.05.2017).

Download

Version History:
1.0.1:
 – fixed source code regarding usage of “GoTo”.

1.1.0:
 – added “Test” function to test if any number is a prime number.
 – minor source code clean up.

1.2.0:
 – added “Procedural” function which lets the program calculate numbers until it is stopped by the user.

1.2.1
 – added a feedback for how many prime numbers have been found by using the “Procedural” function after ending it.

1.2.2
 – source code clean up.

1.3.0
 – Added “Scan” function which scans databases for errors and removes them.

1.3.1
– Fixed multiple minor bugs, that led to the program crashing when an unknown command was typed.
– Optimized calculation to be much faster. (Before it took me 3 seconds to calculate 200 prime numbers on my computer at around 3,000,000 numbers in storage, after I calculated 1,000,000,000 in 54 seconds.) The optimization was possible due to a to me formerly unknown property of prime numbers: the first number each prime number disqualifies is it’s square.
– Code Cleanup: I have moved snippets of codes, that were not needed in the Main method into sub methods and even created a few new ones to make it look better.
– Added a display for the time needed to perform calculations, the scan and loadind the data base.h

Known Issues:
– When using the tool to continuously calculate prime numbers a problem might occur after long periods of time (or lots of prime numbers calculated).

Current Questions:
– Is there a way to more efficiently save the data base onto the hard drive to make loading time and saving faster?

Thank you for reading and if you find a bug you can keep it. No obviously I am happy about each bug reported and fixed.

See you around,
InSicK

  • Liked by
Reply
5 on April 25, 2017

Hey there, your code seems fine for the most.
But there’s one thing you do that you should avoid AT ALL COST.
Repeat after me: Never ever use GoTo’s.

Helpful
on April 25, 2017

Ok, why is that?
To me it seems like they are an efficient way to skip certain code parts in some situations.

Guru
on April 25, 2017

Because when a program gets more complex you won’t be able to follow the flow of the code and it’s easier to make mistakes.
Please believe me, even when you currently don’t see why.

Helpful
on April 25, 2017

Good point.
I’ll try to fix that.
Of course I believe you I mean I asked for opinions.

Helpful
on April 25, 2017

Fixed. Replaced it all with while loops. Is that better?

Guru
on April 25, 2017

Yes 😀

Show more replies
  • Liked by
Reply
Cancel
1 on April 25, 2017

I have a suggestion, if the user wants to look for a specific prime number (nth prime), if the puts in a too large number he will get an error and the program will crash.

Check if the number user typed in is bigger that the list size, if it is then write some kind of an error.

Helpful
on April 25, 2017

Already had something like that in mind.
Basically what happens is similar to what you suggested. If the user enters a number bigger than the largest prime number currently in the list it will tell him the amount of numbers he will have test and ask him to continue. If he dedides to continue it will test number for number until the largest prime number in the list is the same size or larger as the number the user wanted to test. 
I will put this up anytime now. It will be version 1.1.0
Didn’t think I would get this done today. But at this rate I will be done with all my goals by the end of the week. I think tomorrow I will mainly do code clean up though. I am a little annoyed by how messy the code is 😛

Show more replies
  • Liked by
Reply
Cancel
0 on April 26, 2017

Hello there,

while having my new function that continuously calculates new numbers until the “Escape” key is pressed running over night I have encountered a problem and I have no idea where it comes from.

This is what happened:
This morning I wanted to look at the program and see how far it got. But my computer wouldn’t react at all. So I forced it to shut down and restarted it. After this I started the program and immediately encountered an Error message.
While it was trying to convert the saved strings in the .txt file into long integers it encountered a string it couldn’t convert. So I looked in the .txt file to see what was wrong and luckily the Error was in the last line.

00/00/00/00/00/00/00/00

I sadly didn’t copy the exact amount of how many times this repeated. I first thought it was the end of the .txt file but that didn’t turn out to be true. After removing the line and prompting it to calculate new prime numbers it just worked fine.
I’ll put up the new version with the new code, but I don’t really see which part of the code could have done it.

Hopefully you guys can help me.
InSicK

Edit: The error occured in line 2,378,725 of the .txt file.

  • Liked by
Reply
Cancel
0 on May 4, 2017

Version 1.3.1 is out and I did a lot, check it out.

  • Liked by
Reply
Cancel