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

Happy Numbers

Name: Tynach 2009-09-03 19:34

Hey, I just found this board on 4chan (didn't know about it before), and I wanted to know what you guys thought of my program that checks to see if a number is happy. It was originally written in Python, but as a means to learn C, I re-wrote it in C a while back. Recently, I decided I wanted to find out if I really understood pointers as well as I thought, so I re-wrote the C version from scratch, though implemented some of the same code (though heavily modified to work with my new format) to use pointers so it uses less RAM.

Here's the source:

#include <stdio.h>

void check(char *);
char checkh(long long int, long long int *);
void usragain(char *, char *);
void getnum(long long int *);
void flushin();

int main()
{
    char run = 1;
    char state = 0;
    printf("Happy Number Checker v 2.0\n\n");
    while(run == 1) {
        switch (state) {
        case 0:
            check(&state);
            break;
        case 1:
            usragain(&run, &state);
            break;
        }
    }
    return 0;
}

void check(char *ret)
{
    long long int orig;
    getnum(&orig);
    *ret = checkh(orig, &orig);
    if (*ret == 0) {
        printf("%lld is not happy. Try again.\n\n", orig);
    } else if (*ret == 1) {
        printf("%lld is happy!\n\n", orig);
    }
}

char checkh(long long int guess, long long int *orig)
{
    long long int tmp = guess;
    long long int sum;
    char digit;
    char loop = 1;
   
    while (loop == 1) {
        sum = 0;
        while (tmp != 0) {
            digit = tmp % 10;
            sum += digit * digit;
            tmp /= 10;
        }
        if (sum == 1) {
            loop = 0;
            return 1;http://www.qso.com/carlautta/colin/Beta/happy.c
        } else if (sum == 42 || sum == *orig) {
            loop = 0;
            return 0;
        } else {
            loop = 1;
            tmp = sum;
        }
    }
}

void usragain(char *yn, char *state)
{
    *yn = 2;
    printf("Would you like to check another number (y/n)? ");
    while (*yn == 2) {
        flushin();
        scanf("%c", yn);
        printf("\n");
        if (*yn == 'y') {
            *yn = 1;
            *state = 0;
        } else if (*yn == 'n') {
            *yn = 0;
        } else {
            printf("Invalid input, type y or n.\n");
            *yn = 2;
        }
    }
}

void getnum(long long int *guess)
{
    int test = 0;
    while (test == 0) {
        printf("Please insert a number: ");
        test = scanf("%lld", guess);
        if (test == 0) {
            flushin();
            printf("\n\nInvalid input. Try again.\n\n");
        }
    }
}

void flushin()
{
    int ch = 0;
    while ((ch = getc(stdin)) != EOF && ch != '\n') {
        continue;
    }
}

I'm a newb to text boards here on 4chan, so I may be missing a way to insert code properly (in some forums it's and). Please tell me if I fail at this and should become an hero.

Name: Anonymous 2009-09-03 23:11

Here's a CL implementation as I'm bored:

(defun split-digits (n)
  (labels ((rec (n acc)
         (multiple-value-bind (quot rem) (truncate n 10)
           (if (zerop quot)
           (cons n acc)
           (rec quot (cons rem acc))))))
    (rec n nil)))

(defun square (x) (* x x))

(defun sum-of-squares (list)
  (apply #'+ (mapcar #'square list)))

(defun square-and-sum-digits (n)
  (sum-of-squares (split-digits n)))

(defun is-happy-number (n)
  (let ((sum n))
    (loop      
       (setf sum (square-and-sum-digits sum))
       (cond
     ((= sum 1) (return t))
     ((= sum 4) (return nil))))))

(defun find-happy-numbers (list)
  (remove-if-not #'is-happy-number list))

(defun range (n m)
  (loop for i from n to m collect i))

(defun find-happy-numbers-under-value (n)
  (find-happy-numbers (range 1 n)))

; Test run:

;(find-happy-numbers-under-value 500)
; =>
;(1 7 10 13 19 23 28 31 32 44 49 68 70 79 82 86 91 94 97 100 103 109 129
; 130 133 139 167 176 188 190 192 193 203 208 219 226 230 236 239 262 263
; 280 291 293 301 302 310 313 319 320 326 329 331 338 356 362 365 367 368
; 376 379 383 386 391 392 397 404 409 440 446 464 469 478 487 490 496)


Code was extended to find happy numbers within a range/list too.

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