Monday, 11 March 2013

converting numbers/primes into binary

I want to compare some different number systems together with respect to primes. Wanted to know how to convert to binary. Thought this would be in-built to the computers/language, as all numbers are already stored as binary?

Anyway, found this forum with lots of different answers. Smallest answer was:
#include
int main(void) {
  int i, target;
  printf("Enter a number: ");
  scanf("%d", &target);
  for(i = 32; i--;) {
    if(target & (1 << i))
      putc('1', stdout);
    else
      putc('0', stdout);
  }
  return 0;
}

although they suggest altering part of it to


for(i=(j-1);i >= 0; i--)
/* or 'for(i=j;i--; )' would also work but is a bit too cryptic for me to get my head round */
{
  if(target & (1 << i))
    putc('1', stdout);
  else
    putc('0', stdout);
}

I shall try this out.

I ended up doing it my own way, mostly so that I could practice programming. I used the divide by 2 way, and if there is remainers then print 1, else print 0:

#define BINARYARRAYSIZE 1000

//converts a number into Binary and prints with greatest bit first
void convertAndPrintBinary (int number) {
    //printf("converting to binary\n");
    
    
    int binaryArray [BINARYARRAYSIZE] = {0};
    int i = 0;
    for (int result = number; result > 0; result = result/2) {
        
        if (result % 2 == 0 ) {
            binaryArray[i]=0;
            //printf("0");
        } else {
            binaryArray[i]=1;
            //printf("1");
        }
        
        i++;
    }
    
    //printf("%d\n", i );
    
    //prints out array in reverse
    for (int c = i-1; c >=0 ; c--) {
        printf("%d", binaryArray[c]);
    }
    
}

Worked really well in the end:

Type a starting number: 1
Type an end numbers: 20
1 = 1
2 = 10
3 = 11
4 = 100
5 = 101
6 = 110
7 = 111
8 = 1000
9 = 1001
10 = 1010
11 = 1011
12 = 1100
13 = 1101
14 = 1110
15 = 1111
16 = 10000
17 = 10001
18 = 10010
19 = 10011
20 = 10100

I used BINARYARRAYSIZE because I didn't want to run the first for loop twice; once to measure how big the array needed to be, and the second to fill it. When I didn't give the array a size to begin with, it kept erroring. But 1000 digits for binary is a large number!

MacBook Pro 15" Unibody keyboard trouble

Just had a look at a MacBook Pro with keyboard issues. Whenever you tried to type anything it'd make a "nope" noise. Restarted in single user mode "cmd + s", and whenever you tried to type it'd come up with all sorts of stuff.

I rebooted into Windows XP and could not use the ctrl + alt + del key to login. I assume it was a hardware failure at this point.

I tried to update firmware, I might have figured that there would be a keyboard firmware update but couldn't find any such thing.

I rebooted and connected an external keyboard which worked quite well, although when this was connected, occasionally it'd make a 'nope' noise.

I opened up the keyboard viewer in the top right corner and noticed that cmd + shift keys were always held down:

Looks like it thinks these keys are being constantly pressed.

Looking into replacing the keyboard, but after looking at this take-down tutorial and this video:

I think perhaps not.

Will update this when its' fixed.

Sunday, 10 March 2013

Prime Factorisation - programming

Thought I'd test my programming skills by making a program which tells you the prime factorisation of a number or range of numbers. i.e:

4 = 2 x 2
10 = 2 x 5
20 = 2 x 2 x 5

etc...

Here's the code which works it out for a series of numbers:

//
//  main.c
//  printInPrimeFactors
//
//  Created by Monkey on 10/03/2013.
//  Copyright (c) 2013 Monkey. All rights reserved.
//

#include

#define TRUE 1
#define FALSE 0

int isPrime (int number);
int printInPrimeFactors (int number);

int main(int argc, const char * argv[])
{
    //scan in a number
    int startNumber = 0;
    printf("Type a starting number: ");
    scanf("%d", &startNumber);
    
    int endNumber = 0;
    printf("Type an end number: ");
    scanf("%d", &endNumber);
    
    //find the prime factors of range of numbers
    for (int i=startNumber; i<=endNumber; i++) {
        printInPrimeFactors(i);
    }
    
        return 0;
}

int printInPrimeFactors (int number) {
    //printf("finding the prime factors of %d.....\n", number);
    
    //if it's a prime just print it and return to main
    if (isPrime(number)) {
        printf("%d\n", number);
        return 0;
    }
    
    //for nice formatting I'm printing the number =
    printf("%d = ", number);
    
    //testing to find a factor of the number, which is also prime
    for (int i =2; i < number; i++) {
        if (number %i == 0 && isPrime(i)) {
            //printf("a prime factor of %d is %d\n", number, i);
            
            //testing to see how many times the number can be divided by 'i' and printing i each time
            //printf("testing to see how many times %d can divide by %d\n", number, i);
            int result = number;
            while (result % i == 0) {
                printf("%d x ", i);
                result = result / i;
            }
            
            //print an extra line to make it look nice
            //printf("\n");
        }
    }
    
    //print new line for next number
    printf("1\n");
    return 0;
}



//a function which returns true or false if number is prime
int isPrime (int number) {
    //printf("testing to see if %d is a prime\n", number);
    int prime = TRUE;
    
    //test to see if it is divisible by any other numbers
    for (int i = 2; i < number; i++) {
        if (number %i ==0) {
            prime = FALSE;
            //printf("%d is not a prime number\n", i);
            return prime;
        }
    }
    return prime;
}

output:
Type a starting number: 100
Type an end number: 120
100 = 2 x 2 x 5 x 5 x 1
101
102 = 2 x 3 x 17 x 1
103
104 = 2 x 2 x 2 x 13 x 1
105 = 3 x 5 x 7 x 1
106 = 2 x 53 x 1
107
108 = 2 x 2 x 3 x 3 x 3 x 1
109
110 = 2 x 5 x 11 x 1
111 = 3 x 37 x 1
112 = 2 x 2 x 2 x 2 x 7 x 1
113
114 = 2 x 3 x 19 x 1
115 = 5 x 23 x 1
116 = 2 x 2 x 29 x 1
117 = 3 x 3 x 13 x 1
118 = 2 x 59 x 1
119 = 7 x 17 x 1
120 = 2 x 2 x 2 x 3 x 5 x 1

after all that I found this nice website which basically does the same!

Thursday, 7 March 2013

iPad 3 apple logo on black screen cycle

A friend brought his iPad round. It was constantly switching on, showing the apple logo on black background, going slightly grey for a split second and then switching off.

Holding down the power button did nothing.

Holding down the power button and front button put it in "Connect to iTunes" mode. If you leave it for more then a minute not connected to itunes it begins its' cycle again.

Connecting to iTunes asks if you want to reset to factory defaults. Clicked 'yes' and it's downloading the update as I type.

When it was updating/reseting the ipad it came up with error 1611. After looking into it I found this help page on Apples website describing possible reasons:

  1. Security software problem
  2. Hardware fault

I concluded that it was a hardware fault. Taking it back to apple store.

....

took it to Apple Store and was replaced - hardware failure