| 
  • If you are citizen of an European Union member nation, you may not use this service unless you are at least 16 years old.

  • Stop wasting time looking for files and revisions. Connect your Gmail, DriveDropbox, and Slack accounts and in less than 2 minutes, Dokkio will automatically organize all your file attachments. Learn more and claim your free account.

View
 

PE036_c

Page history last edited by Kenneth Finnegan 11 years, 11 months ago

// Solution to Project Euler problem #36

// palindromic in base10 and base2

#include <stdio.h>

int rev10(int num);

int rev2(int num);

main() {

    int sum=0, i;

    for (i=1; i<1000000; i+=2) { // skip evens since they have a leading zero in at least binary

        if (i==rev10(i) && i == rev2(i)) {

            sum += i;

            printf("New solution: %d on %d\n", sum, i);

        }

    }

}

int rev10(int num) {

    int newn=0;

    while (num) {

        newn *= 10; // shift already reveresed digits

        newn += num%10; // put new digit on end

        num /=10; // pull left digits in

    }

    return newn;

}

int rev2(int num) {

    int newn = 0;

    while (num) {

        newn *= 2; // shift already reversed digits

        newn += num & 1; // put new digit on end

        num /= 2; // pull left digits in

    }

    return newn;

}

Comments (0)

You don't have permission to comment on this page.