Showing posts with label maths. Show all posts
Showing posts with label maths. Show all posts

Monday, 18 March 2013

C Programming - convert number to any base

Just to continue with my programming practice I decided to write a function which takes a number and a base and converts that number to that base.

Here's the function:

//takes a number and a base to work to. It converts (just prints) this number to said base
//base has to be greater then 1
void printInBase(int number, int base) {
    
    assert(base > 1);
    
//find out how many units is needed to make the number
//int numberOfUnits = 1;
int units = base;
while (units <= number) {
//numberOfUnits ++;
units = units * base;
}
    //make an array which will be printed out in the end
    //int numberArray [numberOfUnits];
//work out which how many of each unit
    //int i = 0;
    
int result = number;
while (units > 1) {
units = units / base;
printf("%d", result / units);
result = result % units;
        //i++;
}
 
//prints out array
    //for (int c = 0; c <= numberOfUnits ; c++) {
    //    printf("%d", numberArray[c]);
    //}
}
 Output:
Type a starting number: 1
Type an end number: 30
Type a base to count in: 7
1 = 1
2 = 2
3 = 3
4 = 4
5 = 5
6 = 6
7 = 10
8 = 11
9 = 12
10 = 13
11 = 14
12 = 15
13 = 16
14 = 20
15 = 21
16 = 22
17 = 23
18 = 24
19 = 25
20 = 26
21 = 30
22 = 31
23 = 32
24 = 33
25 = 34
26 = 35
27 = 36
28 = 40
29 = 41
30 = 42
The function fails at counting base 1, but it hurts my brain too much to think about it! Also, to count in a base larger then 10, it's best to put a comma between digits. e.g:

Type a starting number: 1
Type an end number: 30
Type a base to count in: 15
1 = 1,
2 = 2,
3 = 3,
4 = 4,
5 = 5,
6 = 6,
7 = 7,
8 = 8,
9 = 9,
10 = 10,
11 = 11,
12 = 12,
13 = 13,
14 = 14,
15 = 1,0,
16 = 1,1,
17 = 1,2,
18 = 1,3,
19 = 1,4,
20 = 1,5,
21 = 1,6,
22 = 1,7,
23 = 1,8,
24 = 1,9,
25 = 1,10,
26 = 1,11,
27 = 1,12,
28 = 1,13,
29 = 1,14,
30 = 2,0,
This way you can distinguish between the 10's and 20's etc. Might be worth putting this in the function. But also, I could make it convert to hexadecimal? Might require more brain hurt.

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?