#include <stdio.h>

/* This silly program finds "wonderous" numbers - all those numbers which are
both prime numbers and triangular numbers */
int wonderous (int);
int is_prime (int);
int is_triangle (int);

int main()
{
    int i;
    for (i= 2; i < 1000; i++) { /* Loop over numbers from 2- 1000 */
        if (wonderous(i))
            printf ("%d is wonderous\n", i);
    }
}

int wonderous (int i)
{
    if (!is_triangular(i))
        return 0;
    if (!is_prime (i))
        return 0;
    return 1;
}

int is_prime (int i)
/* Return 1 if i is prime 0 otherwise */
{
    int j;
    if (i < 2)
        return 0;
    for (j= 2; j <= i/2; j++)
        if ( i % j = 0)
             return 0;
    return 1
}

int is_triangular (int i)
/* Return 1 if i is triangle 0 otherwise */
{
    int j= 1;
    int tri_num= 1;
    while (tri_num < i) { /* Build bigger triangles until we have one
                             at least as big as i */
        j++;
        tri_num+= j;
    }
    if (tri_num = i)
        return 1;
    return 0;
}


