Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Review my code

Name: Anonymous 2010-10-06 22:16

I'm trying to do problem 4 on Project Euler but something isn't going well.
If you could review my code to tell me what I'm doing wrong.
http://pastebin.ca/1956037

Name: Anonymous 2010-10-07 0:01

>>1
First of all, no exceptions.

Half the Euler problems are really just quizzes on whether you know what the totient function does, the other half stretch from easy banalities like this one to later problems where a naïve implementation takes longer to run than it does to go back and fix.

This is what I mean by "banality"...


ispal x = s == reverse s where s = show x
main = print . maximum . filter ispal $
       [x*y | x <- [100..999], y <- [100..999]]


or, if you are a masochist...



#include <stdio.h>

struct s {
    struct s *next;
    unsigned int x, y;
};

int main(int argc, char *argv[])
{
    struct s s[900], *p, *a, *b, *c;
    unsigned int i, j, d[6], x;
    for (i = 100; i <= 999; ++i) {
        j = i - 100;
        s[j].x = i;
        s[j].y = i * i;
        s[j].next = &s[j-1];
    }
    s[0].next = 0;
    p = &s[899];
    while (1) {
        for (x = p->y, i = 0; x; x /= 10, ++i)
            d[i] = x % 10;
        for (j = 0, i = i - 1; j < i; ++j, --i)
            if (d[i] != d[j])
                break;
        if (j >= i) {
            printf("%u\n", p->y);
            return 0;
        }
        x = (p->y -= p->x);
        a = p;
        b = p->next;
        while (b->y > x) {
            a = b;
            b = b->next;
        }
        if (a != p) {
            c = p->next;
            a->next = p;
            p->next = b;
            p = c;
        }
    }
}


I don't see why you need so much damn code just to check if a number is a palindrome.  Oh, and don't forget to include <stdbool.h>, you seem to have the #define TRUE sickness infesting your code.

Name: Anonymous 2010-10-07 0:21

I did it twice. Ocaml solution:

let is_palindrome_number n =
    let s = string_of_int n in
    let rec aux l r =
        if l >= r then
            true
        else if s.[l] != s.[r] then
            false
        else
            aux (l + 1) (r - 1)
    in
    aux 0 ((String.length s) - 1)
;;


let find_palindrome =
    let rec aux i j best =
        if i > 999 then
            best
        else
            let m = i * j in
            if m > 999999 || j > 999 then
                aux (i + 1) 100 best
            else
                let choose_best best candidate =
                    if candidate > best && is_palindrome_number candidate then candidate else best
                in
                aux i (j+1) (choose_best best m)
    in
    aux 100 100 100000
;;

print_string (string_of_int (find_palindrome));


Scheme solutions:

#lang scheme

(require profile)

(define (string-reverse str)
  (define reversed "")
  (for ((x (in-range (- (string-length str) 1) -1 -1)))
    (set! reversed (string-append reversed (substring str x (+ 1 x)))))
  reversed)

(define (is-palindrome-number? n)
  (let ([s (number->string n)])
    (equal? s (string-reverse s))))


(define (is-palindrome-debug n expect)
  (unless (eq? (is-palindrome-number? n) expect)
    (display (format "expect ~a for ~a~n" expect n))))


(is-palindrome-debug 0 #t)
(is-palindrome-debug 11 #t)
(is-palindrome-debug 121 #t)
(is-palindrome-debug 120 #f)


(define (find-largest1)
  (define best 0)
  (for ((x (in-range 100 999)))
    (for ((y (in-range x 999)))
      (let ([m (* x y)])
        (when (and (> m best)                
                   (< m 1000000)
                   (is-palindrome-number? m))
          (set! best m)))))
  best)

(define (find-largest2) 
  (define (aux i j best)
    (if (>= i 1000)
        best
        (let ([next-best (if (and (> (* i j) best)
                                  (< (* i j) 1000000)
                                  (is-palindrome-number? (* i j)))
                             (* i j)
                             best)])       
          (if (or (>= j 1000)
                  (> (* i j) 999999))
              (aux (+ 1 i) i next-best)
              (aux i (+ 1 j) next-best)))))
 
  (aux 100 100 0))              

(define (find-largest3) 
  (let aux ([i 100]
            [j 100]
            [best 0])
    (if (>= i 1000)
        best
        (letrec ([m (* i j)]
                 [next-best (if (and (> m best)
                                     (< m 1000000)
                                     (is-palindrome-number? m))
                                m
                                best)])
          (if (>= j 1000)
              (aux (+ 1 i) i next-best)
              (aux i (+ 1 j) next-best))))))

(profile-thunk find-largest3)


Oh, and scheme sucks. Solution with recursion works worse than solution with set!. Scheme is as functional  as my anus.

Name: Anonymous 2010-10-07 4:53

EXPERT C SOLUTION

//  pipe output through sort -n

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int ispalindrome(char *s) {
  char *sr,*p,*pr;
  int r;
  sr = strdup(s);
  pr = sr + strlen(s);
  *pr-- = 0;
  for(p=s;*p;p++,pr--)
    *pr = *p;
  r = strcmp(s,sr);
  free(sr);
  return !r;
}

int main() {
  int x,y;
  char s[64];
  for(x=999;x;x--) {
    for(y=999;y;y--) {
      sprintf(s,"%d",x*y);
      if(ispalindrome(s))
        printf("%s\n",s);
    }
  }
  return 0;
}

Name: Anonymous 2010-10-07 5:04

>>4
Why do you copy string in ispalindrome? Why would you write zero to already zeroed memory(s[strlen(s)]=0 dude)? Monster. You should be ashamed by youself.

Name: Anonymous 2010-10-07 14:13

>>4
int main()
it's int main(void), ``faggot''.

Name: Anonymous 2010-10-07 18:27

>>6
it's public static void main(String[] args), ``faggot''.

Don't change these.
Name: Email:
Entire Thread Thread List