•   Posted in: 
  • C#
So - I got it into my head to create a function that would find prime numbers. There was no real reason to do so - it was just one of those coding ear worms that bug you until you actually do something about it. 

After staring at a blank IDE for a while, I decided what I reallywanted to do was create an app that would give me n number of prime numbers in a list.

Rules for optimization:

  1. Don't optimize.
  2. Optimize later.

I decided that the best way to go for this project would be to determine the next prime number from a given integer. That way, I could generate lists and other such nonsense. I settled on the following sequence:

public int nextPrime(int lastNo)
    int currentNo = lastNo;
    bool isNotPrime = true;

while (isNotPrime)
        isNotPrime = false;
        for (int i = 2; i < currentNo; i++)
            if (currentNo % i == 0)
                isNotPrime = true;
        return currentNo;

It's a fairly simple exercise, so let's quickly walk through it.

The premise of the function is that we want to find the next prime number, not whether the input number itself is prime.

  1. We create a copy of the input number (currentNo).
  2. We create a conditional check (is the number not prime?) and set to true. This is so we can pass into the first conditional.
  3. While the conditional is true (the number is not prime)
    1. Set the conditional to false.  We are setting the assumption that the number is indeed a prime.
    2. increment the currentNovalue by 1.
    3. Create a for-loop to go through all of the numbers between 2 and the currentNominus 1.
    4. If the modulus of currentNodivided by the increment value (i) equals 0, then the number cannot be prime, so we set the test flag as true and break from the loop. Otherwise, we continue iterating through the set range of numbers until we exhaust it, or we manage to change the flag.
  4. If the test flag is true, re-run the sequence until it is not.
  5. Return the first value that results in the for-loop not changing the test flag to true.

So - fairly elegant, but far from optimized. For single checks, this function preforms very fast. But what if we want to find the next 100, 1000, 10000, or 1 million prime numbers in a sequence?

(To be continued . . .)

  1. What are the four tenets of an OOP language?
    Abstraction- refers to hiding the internal operations of a class from anything that consumes that class.
    Encapsulation- refers to the bundling of data and methods into a single object or class
    Inheritance- refers to the ability of one class to use properties of another class
    Polymorphism- refers to the ability to use different object types through the same interface.

  2. What is boxing and unboxing?
    Boxing is the ability to store a value type into an object.  
    Unboxing is the ability to take an object variable and convert it to a specific value type.

  3. What is a generic?
    A genericis a placeholder type. It allows the developer to use an object without immediately determining a typeset. Used primarily in collections.

  4. What is LINQ?
    LINQ(Language Integrated Query) is a methodology for querying data and objects.

  5. What is a Lambda Expression?
    A Lambda Expression is the shorthand syntax for writing LINQ statements. 

  6. What are delegates?
    Delegates allow the developer to set variables as function calls. It can be thought of as to a pointer to a function.

  7. What is an Abstract Class?
    An abstract class is a class that cannot be instantiated. Abstract classes can, however, be inherited.

  8. What is an Interface?
    An interfaceis a collection of abstract methods.

  9. What is the difference between overloading and overriding?
    Overloading - multiple functions of the same name, using different input parameters.
    Overriding - use of a subclass to override(replace) the method functionality of a main class.

    Overloading is a static binding, while overriding is a dynamic binding.

  10. What are the Access Modifiers?

  11. What is the difference between Public, Static and Void?
    Public - the object or method is available to anything able to consume it.
    Private - the object is available only to the class that contains it.
    Static- the object is globally accessible without instantiating a class. 
  12. What is a Constructor?
    A constructor is a function that has the same name as its containing class.

  13. What is Serialization?
    Serialization  is the act of turning an object into a stream of bytes. This stream can then be stored into another platform (like a file.)

    A class can be made serializable by declaring it as ISerialize.

  14. What is Deserialization?
    Deserialization is the conversion of a byte array into a set object.

  15. What's the difference between Read-Only variables and Constant variables?
    Constant variables are set once during compile time and cannot be changed. Read-Only variables are used only when setting a value at runtime.

  16. Can you override the methods in an interface?
    No. All of the interface methods are virtual, so they can only be overridden in the class that inherits them.

  17. What is the difference between a Struct and a Class?
    A Struct is a value-type variable, where a Class is a reference type.  Structs cannot be inherited, and are stored on the stack for speed of access.

  18. What is the difference between Stack and Heap memory?
    Stack memory is more structured and managed by the system.  Heap memory is managed by the application.

    Value types are allocated to the Stack.  Reference types are allocated to the Heap.

  19. What is a sealed modifier?
    A sealed modifier prevents the object from being inherited, 

  20. What is Jagged Array?
    A jagged  array is an array of arrays.

  21. What is Managed code?
    Managed Code is any code executed by the CLR. The code is 'managed' by the .NET Framework.

  22. What are the different types of classes in C#?
    Keyword: Partial Class - A 'shared' class allows its members to be used by multiple .cs files.
    Keyword: Sealed class - A class that cannot inherited. To access its members, you need to instantiate the class directly.
    Keyword: Abstract class - A class that cannot be instantiated. This class can only be inherited, and will contain at least one method.
    Keyword: Static class - A class that does not allow inheritance. Members of a static class are also static.

  23. What are the steps taken in C# code compilation?
    1. Compiling the source code to Managed Code
    2. Combining newly created code to Assemblies
    3. Loading the Common Language Runtime (CLR)
    4. Executing the assembly by the CLR

  24. What's the difference between a Virtual Method and an Abstract Method?
    Abstract Method - always has a default implementation. It can be overridden in its derived class, though not necessarily mandatory. It can be overridden using the keyword: override.
    Virtual Method - does not have an implementation, and resides in the abstract class. It is mandatory that the the derived class implements the abstract method.

  25. What are Events?
    Events are actions that generate some type of notification.
(To be continued)