Exemplo n.º 1
0
Arquivo: 37.c Projeto: higgsd/euler
int istrunc(int d, char *n)
{
    int i;
    if (!is_prime(mknum(d, n)))
        return 0;
    for (i = 1; i < d; i++)
        if (!is_prime(mknum(d-i, n)) || !is_prime(mknum(d-i, n+i)))
            return 0;
    return 1;
}
Exemplo n.º 2
0
Arquivo: 37.c Projeto: higgsd/euler
int main(int argc, char **argv)
{
    int n = 0, m = 0, s = 0;
    int i, j, aa, a, b, c;
    char first[] = {2, 3, 5, 7};
    char last[] = {3, 7};
    char middle[] = {1, 3, 7, 9};
    char num[10] = {0};

    while (n < 11)
    {
        for (aa = 1, i = 0; i < m; i++)
            aa *= sizeof(middle);

        for (a = 0; a < aa; a++)
        {
            for (i = 0, j = a; i < m; i++, j /= sizeof(middle))
                num[i+1] = middle[j % sizeof(middle)];

            for (b = 0; b < sizeof(first); b++)
            {
                num[m+1] = first[b];
                for (c = 0; c < sizeof(last); c++)
                {
                    num[0] = last[c];
                    if (istrunc(m+2, num))
                    {
                        n++;
                        s += mknum(m+2, num);
                    }
                }
            }
        }
        m++;
    }
    printf("%d\n", s);
    return 0;
}
Exemplo n.º 3
0
static cstring_t
printf_doformat(cstring_t start, int32_t* rval)
{
	static const char skip1[] = "#'-+ 0";
	static const char skip2[] = "0123456789";
	cstring_t fmt;
	int32_t fieldwidth;
	int32_t haveprec, havewidth;
	int32_t mod_ldbl, precision;
	char32_t convch;
	char32_t nextch;
	fmt = start + 1;
	/* skip to field width */
	fmt += strspn(fmt, skip1);
	if (*fmt == '*')
	{
		if (getint(&fieldwidth))
			return (NULL);
		havewidth = 1;
		++fmt;
	}
	else
	{
		havewidth = 0;
		/* skip to possible '.', get following precision */
		fmt += strspn(fmt, skip2);
	}
	if (*fmt == '.')
	{
		/* precision present? */
		++fmt;
		if (*fmt == '*')
		{
			if (getint(&precision))
				return (NULL);
			haveprec = 1;
			++fmt;
		}
		else
		{
			haveprec = 0;
			/* skip to conversion char */
			fmt += strspn(fmt, skip2);
		}
	}
	else
		haveprec = 0;
	if (!*fmt)
	{
		warnx("missing format character");
		return (NULL);
	}
	/*
	 * Look for a length modifier.  POSIX doesn't have these, so
	 * we only support them for floating-point conversions, which
	 * are extensions.  This is useful because the L modifier can
	 * be used to gain extra range and precision, while omitting
	 * it is more likely to produce consistent results on different
	 * architectures.  This is not so important for integers
	 * because overflow is the only bad thing that can happen to
	 * them, but consider the command  printf %a 1.1
	 */
	if (*fmt == 'L')
	{
		mod_ldbl = 1;
		fmt++;
		if (!strchr("aAeEfFgG", *fmt))
		{
			warnx("bad modifier L for %%%c", *fmt);
			return (NULL);
		}
	}
	else
	{
		mod_ldbl = 0;
	}
	convch = *fmt;
	nextch = *++fmt;
	*fmt = '\0';
	switch (convch)
	{
		case 'b':
		{
			size_t len;
			cstring_t p;
			int32_t getout;
			p = strdup(getstr());
			if (p == NULL)
			{
				warnx("%s", strerror(ENOMEM));
				return (NULL);
			}
			getout = escape(p, 0, &len);
			*(fmt - 1) = 's';
			PF(start, p, havewidth, haveprec, fieldwidth, precision);
			*(fmt - 1) = 'b';
			free(p);
			if (getout)
				return (fmt);
			break;
		}
		case 'c':
		{
			// create a nullterminated char32_t character
			uint64_t ch;
			ch = getchr();
			PF(start, (cstring_t)&ch, havewidth, haveprec, fieldwidth, precision);
			break;
		}
		case 's':
		{
			const_cstring_t p;
			p = getstr();
			PF(start, (cstring_t)p, havewidth, haveprec, fieldwidth, precision);
			break;
		}
		case 'd':
		case 'i':
		case 'o':
		case 'u':
		case 'x':
		case 'X':
		{
			cstring_t f;
			intmax_t val;
			uintmax_t uval;
			int32_t signedconv;
			signedconv = (convch == 'd' || convch == 'i');
			if ((f = mknum(start, convch)) == NULL)
				return (NULL);
			if (getnum(&val, &uval, signedconv))
				*rval = 1;
			if (signedconv)
				PF(f, val, havewidth, haveprec, fieldwidth, precision);
			else
				PF(f, uval, havewidth, haveprec, fieldwidth, precision);
			break;
		}
		case 'e':
		case 'E':
		case 'f':
		case 'F':
		case 'g':
		case 'G':
		case 'a':
		case 'A':
		{
			long double p;
			if (getfloating(&p, mod_ldbl))
				*rval = 1;
			if (mod_ldbl)
				PF(start, p, havewidth, haveprec, fieldwidth, precision);
			else
				PF(start, (double)p, havewidth, haveprec, fieldwidth, precision);
			break;
		}
		default:
			warnx("illegal format character %c", convch);
			return (NULL);
	}
	*fmt = nextch;
	return (fmt);
}
Exemplo n.º 4
0
static char *
printf_doformat(char *fmt, int *rval)
{
	static const char skip1[] = "#'-+ 0";
	int fieldwidth, haveprec, havewidth, mod_ldbl, precision;
	char convch, nextch;
	char start[strlen(fmt) + 1];
	char **fargv;
	char *dptr;
	int l;

	dptr = start;
	*dptr++ = '%';
	*dptr = 0;

	fmt++;

	/* look for "n$" field index specifier */
	l = strspn(fmt, digits);
	if ((l > 0) && (fmt[l] == '$')) {
		int idx = atoi(fmt);
		if (idx <= myargc) {
			gargv = &myargv[idx - 1];
		} else {
			gargv = &myargv[myargc];
		}
		if (gargv > maxargv)
			maxargv = gargv;
		fmt += l + 1;

		/* save format argument */
		fargv = gargv;
	} else {
		fargv = NULL;
	}

	/* skip to field width */
	while (*fmt && strchr(skip1, *fmt) != NULL) {
		*dptr++ = *fmt++;
		*dptr = 0;
	}

	if (*fmt == '*') {

		fmt++;
		l = strspn(fmt, digits);
		if ((l > 0) && (fmt[l] == '$')) {
			int idx = atoi(fmt);
			if (fargv == NULL) {
				warnx("incomplete use of n$");
				return (NULL);
			}
			if (idx <= myargc) {
				gargv = &myargv[idx - 1];
			} else {
				gargv = &myargv[myargc];
			}
			fmt += l + 1;
		} else if (fargv != NULL) {
			warnx("incomplete use of n$");
			return (NULL);
		}

		if (getint(&fieldwidth))
			return (NULL);
		if (gargv > maxargv)
			maxargv = gargv;
		havewidth = 1;

		*dptr++ = '*';
		*dptr = 0;
	} else {
		havewidth = 0;

		/* skip to possible '.', get following precision */
		while (isdigit(*fmt)) {
			*dptr++ = *fmt++;
			*dptr = 0;
		}
	}

	if (*fmt == '.') {
		/* precision present? */
		fmt++;
		*dptr++ = '.';

		if (*fmt == '*') {

			fmt++;
			l = strspn(fmt, digits);
			if ((l > 0) && (fmt[l] == '$')) {
				int idx = atoi(fmt);
				if (fargv == NULL) {
					warnx("incomplete use of n$");
					return (NULL);
				}
				if (idx <= myargc) {
					gargv = &myargv[idx - 1];
				} else {
					gargv = &myargv[myargc];
				}
				fmt += l + 1;
			} else if (fargv != NULL) {
				warnx("incomplete use of n$");
				return (NULL);
			}

			if (getint(&precision))
				return (NULL);
			if (gargv > maxargv)
				maxargv = gargv;
			haveprec = 1;
			*dptr++ = '*';
			*dptr = 0;
		} else {
			haveprec = 0;

			/* skip to conversion char */
			while (isdigit(*fmt)) {
				*dptr++ = *fmt++;
				*dptr = 0;
			}
		}
	} else
		haveprec = 0;
	if (!*fmt) {
		warnx("missing format character");
		return (NULL);
	}
	*dptr++ = *fmt;
	*dptr = 0;

	/*
	 * Look for a length modifier.  POSIX doesn't have these, so
	 * we only support them for floating-point conversions, which
	 * are extensions.  This is useful because the L modifier can
	 * be used to gain extra range and precision, while omitting
	 * it is more likely to produce consistent results on different
	 * architectures.  This is not so important for integers
	 * because overflow is the only bad thing that can happen to
	 * them, but consider the command  printf %a 1.1
	 */
	if (*fmt == 'L') {
		mod_ldbl = 1;
		fmt++;
		if (!strchr("aAeEfFgG", *fmt)) {
			warnx("bad modifier L for %%%c", *fmt);
			return (NULL);
		}
	} else {
		mod_ldbl = 0;
	}

	/* save the current arg offset, and set to the format arg */
	if (fargv != NULL) {
		gargv = fargv;
	}

	convch = *fmt;
	nextch = *++fmt;

	*fmt = '\0';
	switch (convch) {
	case 'b': {
		size_t len;
		char *p;
		int getout;

		p = strdup(getstr());
		if (p == NULL) {
			warnx("%s", strerror(ENOMEM));
			return (NULL);
		}
		getout = escape(p, 0, &len);
		fputs(p, stdout);
		free(p);
		if (getout)
			return (end_fmt);
		break;
	}
	case 'c': {
		char p;

		p = getchr();
		PF(start, p);
		break;
	}
	case 's': {
		const char *p;

		p = getstr();
		PF(start, p);
		break;
	}
	case 'd': case 'i': case 'o': case 'u': case 'x': case 'X': {
		char *f;
		intmax_t val;
		uintmax_t uval;
		int signedconv;

		signedconv = (convch == 'd' || convch == 'i');
		if ((f = mknum(start, convch)) == NULL)
			return (NULL);
		if (getnum(&val, &uval, signedconv))
			*rval = 1;
		if (signedconv)
			PF(f, val);
		else
			PF(f, uval);
		break;
	}
	case 'e': case 'E':
	case 'f': case 'F':
	case 'g': case 'G':
	case 'a': case 'A': {
		long double p;

		if (getfloating(&p, mod_ldbl))
			*rval = 1;
		if (mod_ldbl)
			PF(start, p);
		else
			PF(start, (double)p);
		break;
	}
	default:
		warnx("illegal format character %c", convch);
		return (NULL);
	}
	*fmt = nextch;
	/* return the gargv to the next element */
	return (fmt);
}