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

  • You already know Dokkio is an AI-powered assistant to organize & manage your digital files & messages. Very soon, Dokkio will support Outlook as well as One Drive. Check it out today!

View
 

PE036_c

Page history last edited by Kenneth Finnegan 15 years, 7 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.