Showing posts with label Theory. Show all posts
Showing posts with label Theory. Show all posts

Thursday, 28 February 2013

Finding Prime Numbers

Thought I'd test out some programming I'd learnt so I'm now writing a program to find large prime numbers. Here's the basic program which I'll improve over time:


main()
function main () {
    print ("hello");
    printPrimeNumbers (1, 100);
    return 0;
    }

function printPrimeNumbers (startNumber, maxNumber) {
    var counter = startNumber;
    while (counter < maxNumber){
        if (isPrime(counter)) {
            print (counter);
            }
        counter++;
        }
    return 0;
    }

function isPrime (number) {
    var prime = true;
    if (number > 3){
        if (number !== 0){
            var i = (number-1)/2;
            while (i>1) {
                if (number % i == 0) {
                    prime = false;
                    return prime;
                    }
                    i--;
            }
        }
         else {
                prime = false;
                }
    }
    return prime;
    }

Basically, isPrime (number) is the function which is doing all the work at the moment. If the number is greater then 3 and not divisible by 2 then: see if it can be divided by any number smaller then itself. This number starts from itself divided by two to reduce the number.

Found this little site which gives nice little examples of small C programs

So far this works. It is in fact already optimised slightly with the:
 if (number !== 0)
and
var i = (number-1)/2;

I havent actually read up on any optimisations anyone else has done. I think I'll carry on and when I get stuck I'll do some research.

My next main idea is to record all prime numbers starting from one up to the maxNumber. Then, to find out if the next one is a prime, just divide by all other primes before it. Should then not have to do as much work, perhaps by 3/4? The two down sides are that:

  1. I'd have to store these numbers somewhere
  2. I'd have to have all the primes up to the number you want to know whether is a prime or not.
Heres the script I came up with in C:

#include
#define true 1
#define false 0
#define STARTNUM 1
#define ENDNUM 100000000


int printPrimes (int startNumber, int maxNumber);
int isPrime (int number, int primesArray[], int numberOfPrimes);

int main (int argc, const char * argv[]) {
    printf ("hello \n");
printPrimes (STARTNUM, ENDNUM);
    return 0;
}

int printPrimes (int startNumber, int maxNumber) {
    int counter = startNumber;
int numberOfPrimes = 0;
int primesArray [100000];
    while (counter < maxNumber){
        if (isPrime(counter, primesArray, numberOfPrimes)) {
            printf ("%d\n" , counter);
primesArray[numberOfPrimes] = counter;
numberOfPrimes++;
}
        counter++;
}
/*printf("Printing the Array of Primes\n");
for (int c = 0; c < numberOfPrimes; c++) {
printf("%d\n", primesArray[c]);
}*/
    return 0;
}

int isPrime (int number, int primesArray[], int numberOfPrimes) {
    int prime = true;
    if (number <= 3){
            for (int i=1; i
if (number % primesArray[i] == 0) {
                    prime = false;
                    return prime;
}
}
    }
    return prime;
}

Works pretty well.

A future idea is to find the frequency of primes and find a relationship. Perhaps taking it one step further and finding the frequency of frequencies. I'm not sure how to represent frequencies though?

Wednesday, 3 November 2010

Confucius

Well, having seen the film, I thought I'd quite like to know of his 'teachings'. Can't find the book at the library though. Here's a good start:

http://www.bbc.co.uk/iplayer/console/p00547k8/In_Our_Time_Confucius

I'm 11 minutes in and it's quite interesting. I shall add to this post later.

Friday, 23 July 2010

one gear engines

Quite a while ago (over 10 years) I heard of a formula 1 car with only 1 gear, which changed its ratio without needing another gear. I've heard nothing since yet I think it revolutionary. I would like to develop the idea.

Doh! Just read up on it. It's called Continuously Variable Transmission - CVT. Conceptualised in the 1440's by Leonardo da Vinci!!! Used in the 1930's. Drawbacks are that they transmit power through friction and so cannot handle infinite torque.

On the wiki page there is also a link to a page which has newer information about a D-Drive which doesn't use friction but uses gears utilising planetary epicycle. Here's a youtube video about it, and the webpage I found about it - http://fuel-efficient-vehicles.org/energy-news/?p=960

It seems to work very well.  A small drawback is the fact you need to have another engine rotating two shafts to control which ratio you want. Very, very interesting concept, although I think I would like a system where you pull a lever and it changes the ratio (using my own power).

There is room here for a new invention!! Although it'd have to be clever!!

Yay! Just found out about it here. Apparently in 1993 Williams F1 team developed it and made the test car seconds faster in a lap. But it was banned by the FIA. Have to use between 4 and 7 gears!

Another interesting youtube video is a guy who's made a planetary gear system (or cyclic) from technics lego stuff - http://www.youtube.com/watch?v=VuGGbAbd_Ts&feature=PlayList&p=789804284EA4BB9C&index=32&playnext=2