PrimeTables

所属分类:Leetcode/题库
开发工具:C#
文件大小:30KB
下载次数:0
上传日期:2016-03-30 10:49:41
上 传 者sh-1993
说明:  面试编程测试
(Programming test for interview)

文件列表:
.nuget (0, 2016-03-30)
.nuget\packages.config (441, 2016-03-30)
PrimeTables.Console (0, 2016-03-30)
PrimeTables.Console\App.config (178, 2016-03-30)
PrimeTables.Console\PrimeTableConsoleRenderer.cs (1033, 2016-03-30)
PrimeTables.Console\PrimeTableCsvFileRenderer.cs (1008, 2016-03-30)
PrimeTables.Console\PrimeTables.Console.csproj (3006, 2016-03-30)
PrimeTables.Console\Program.cs (1713, 2016-03-30)
PrimeTables.Console\Properties (0, 2016-03-30)
PrimeTables.Console\Properties\AssemblyInfo.cs (1414, 2016-03-30)
PrimeTables.Model (0, 2016-03-30)
PrimeTables.Model\IPrimeTableRenderer.cs (260, 2016-03-30)
PrimeTables.Model\PrimeGenerator.cs (3202, 2016-03-30)
PrimeTables.Model\PrimeTables.Model.csproj (2503, 2016-03-30)
PrimeTables.Model\Properties (0, 2016-03-30)
PrimeTables.Model\Properties\AssemblyInfo.cs (1410, 2016-03-30)
PrimeTables.Test (0, 2016-03-30)
PrimeTables.Test\PrimeNumbersTests.cs (2660, 2016-03-30)
PrimeTables.Test\PrimeTableTests.cs (2164, 2016-03-30)
PrimeTables.Test\PrimeTables.Test.csproj (4928, 2016-03-30)
PrimeTables.Test\Properties (0, 2016-03-30)
PrimeTables.Test\Properties\AssemblyInfo.cs (1408, 2016-03-30)
PrimeTables.Test\packages.config (129, 2016-03-30)
PrimeTables.sln (2236, 2016-03-30)
programming-test-Oct72015-Primes.docx (15084, 2016-03-30)

# Primes Table #### How to run it Just open it on Visual Studio. Build Solution once. Set console as startup project and Run/Debug it. If input number is less than 15 the prime table will be displayed in the console, otherwise it will create a csv file "primeTable.csv" just beside the exe file in the bin folder of the console project. #### Some comments about the design and the implementation process I Implemented it using TDD style. My approach was the following: * First I started adding simple tests to test/design the prime genration numbers method. I started adding classes/methods as needed and adding logic/refactoring for each test to pass. I ended with a simple naive not optimized but functional prime generation algorithm. * Once it was tested and working and I was satisfied with the initial implementation I moved the classes and methods to their own class library project and out of the test project. * I started then to write tests and design the table generation. Same thought principle as in previous method. * Once this was tested and implemented I started to think in a better way of generate prime numbers as it was clearly not efficient for long numbers. Didn't know much about it so I started to investigate. Found out about Sieve Theory and how it is useful specially for prime numbers generation ([Sieve Theory](https://en.wikipedia.org/wiki/Sieve_theory), [Generate Primes](https://en.wikipedia.org/wiki/Generating_primes)). I decided to use one of the simplest one but efficient enough for this task the [Sieve of Eratosthenes](https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes) which basically means that if you know your limit you then start eliminating multiples starting with 2 until limit, then 3 etc. Then I realised that for that I needed to know the last value of the range, so started to find out and what is used is an approximation to the nth prime number that for numbers greater than 6 can be obtained by [n * (Log(n) + Log(Log(n)))]. So I wrote a decent algorithm for all that and it was a great improvement on performance. * Finally I started to work on the actual display of the table. For this test I decided to do a simple console application. But to showcase some flexibility I created an interface for Rendering the table, and then 2 concrete implementations one to write to console and another to write to a csv file. I decided that best way to showcase was that program after reading input will decide that if is less than 15 it will do console rendering if not it will do csv file rendering. But it is extensible and will be easier for example to create another rendered to render file in sql, in binary, etc. #### What I am pleased with * I think It the prime generation and the table generation is quite efficient and it doesnt have to many lines of code. * It has a good suit of tests using Nunit, and it has strong design due to TDD. * The display of table is extensible and quite easier to change for another. * It has good separation of concerns #### What Would I do if I had more time * The prime generation algorithm could be better. I used one of the simplest sieve implementations. There are others more complex that can do things much more faster. * The table generation algorithm can be improved for example calculating table[i,j] cell is same as calculating table[j,i] cell so that can be improved to calculate once. * The display of data can definitely be improved. Now it little bit slow for more than 7000 and the file end being around 1GB with 10K. I did optimise it a little so it doesnt have any out of memory exception at least until 10K primes. * The Separation of concerns and SOLID principles could be done little bit better,, maybe separating the prime table generation to its own class, creating some interface for prime generation class and injecting it to new class. * Better Error handling * I could have done more performance testing and also more testing in the UI, there is at the moment no tests on the file or console display methods. #### Final comments I did enjoy this excercice as even it seem simple, it has several things to consider. I learned lot of things about sieve theory and about prime numbers that I did not know before. And It also help me think in better design for extensibility, think on performance when I am coding and other things.

近期下载者

相关文件


收藏者