Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #15789

factor

From jgk@panix.com (Joe keane)
Newsgroups comp.lang.c
Subject factor
Date 2012-01-12 22:53 +0000
Organization Public Access Networks Corp.
Message-ID <jeno8l$skd$1@reader1.panix.com> (permalink)
References <e8760468-1ea4-4f12-83f6-f0fa7a460379@g41g2000yqa.googlegroups.com> <78ecc52b-cc20-477c-9a83-dadd3b46d1ff@u20g2000yqb.googlegroups.com> <20120110171743.392@kylheku.com> <jeni3v$v49$1@dont-email.me>

Show all headers | View raw


#include <stdio.h>
#include <math.h>


int main(int argc, char **argv)
{
    const char *arg;
    unsigned long ori;
    unsigned long red;
    unsigned long div;
    unsigned long ste;

    if (argc != 2)
    {
	fputs("usage: factor <number>\n", stderr);
	return 1;
    }

    arg = argv[1];
    sscanf(arg, "%lu", &ori);
    red = ori;
    fprintf(stdout, "%lu", ori);
    fputs(" = ", stdout);

    if (red < 4)
    {
	fputc('0' + (int) red, stdout);
	goto done;
    }

    if ((red & 0x1) == 0x0)
    {
	red >>= 1;
	fputc('2', stdout);

	if ((red & 0x1) == 0x0)
	{
	    int x;

	    fputc('^', stdout);
	    red >>= 1;
	    x = 2;

	    while ((red & 0x1) == 0x0)
	    {
		red >>= 1;
		x++;
	    }

	    fprintf(stdout, "%d", x);
	}

	if (red == 1)
	    goto done;

	fputc('*', stdout);
    }

    if (red % 3 == 0)
    {
	fputc('3', stdout);
	red /= 3;

	if (red % 3 == 0)
	{
	    int x;

	    red /= 3;
	    fputc('^', stdout);
	    x = 2;

	    while (red % 3 == 0)
	    {
		red /= 3;
		x++;
	    }

	    fprintf(stdout, "%d", x);
	}

	if (red == 1)
	    goto done;

	fputc('*', stdout);
    }

    div = 5;
    ste = 2;

    for (;;)
    {
	unsigned long lim;

    relim:
	lim = (unsigned long) floor(sqrt((double) red));

	for (;;)
	{
	    if (red % div == 0)
	    {
		red /= div;
		fprintf(stdout, "%lu", div);

		if (red % div == 0)
		{
		    int x;

		    fputc('^', stdout);
		    red /= div;
		    x = 2;

		    while (red % div == 0)
		    {
			red /= div;
			x++;
		    }

		    fprintf(stdout, "%d", x);
		}

		if (red == 1)
		    goto done;

		fputc('*', stdout);
		goto relim;
	    }

	    div += ste;
	    ste ^= 6;

	    if (div > lim)
	    {
		fprintf(stdout, "%lu", red);
		goto done;
	    }
	}
    }

  done:
    fputc('\n', stdout);
    fflush(stdout);
    return 0;
}

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Re: Toughest Game Show Question Bill Reid <hormelfree@gmail.com> - 2012-01-10 06:47 -0800
  Re: Toughest Game Show Question tom st denis <tom@iahu.ca> - 2012-01-10 07:11 -0800
    Re: Toughest Game Show Question falk@rahul.net (Edward A. Falk) - 2012-01-11 00:05 +0000
  Re: Toughest Game Show Question Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-01-10 11:23 -0500
  Re: Toughest Game Show Question John Gordon <gordon@panix.com> - 2012-01-10 16:48 +0000
    Re: Toughest Game Show Question Seebs <usenet-nospam@seebs.net> - 2012-01-10 19:15 +0000
      Re: Toughest Game Show Question Keith Thompson <kst-u@mib.org> - 2012-01-10 13:27 -0800
        Re: Toughest Game Show Question James Kuyper <jameskuyper@verizon.net> - 2012-01-10 16:57 -0500
        Re: Toughest Game Show Question Seebs <usenet-nospam@seebs.net> - 2012-01-10 22:39 +0000
  Re: Toughest Game Show Question Stephen Sprunk <stephen@sprunk.org> - 2012-01-10 11:21 -0600
    Re: Toughest Game Show Question Bill Reid <hormelfree@gmail.com> - 2012-01-10 15:43 -0800
      Re: Toughest Game Show Question Stephen Sprunk <stephen@sprunk.org> - 2012-01-10 22:53 -0600
        Re: Toughest Game Show Question Seebs <usenet-nospam@seebs.net> - 2012-01-11 05:00 +0000
          Re: Toughest Game Show Question Kaz Kylheku <kaz@kylheku.com> - 2012-01-11 06:32 +0000
          Re: Toughest Game Show Question James Kuyper <jameskuyper@verizon.net> - 2012-01-11 07:22 -0500
      Re: Toughest Game Show Question falk@rahul.net (Edward A. Falk) - 2012-01-12 22:51 +0000
        Re: Toughest Game Show Question Ian Collins <ian-news@hotmail.com> - 2012-01-13 12:14 +1300
        Re: Toughest Game Show Question ImpalerCore <jadill33@gmail.com> - 2012-01-12 15:17 -0800
  Re: Toughest Game Show Question Shao Miller <sha0.miller@gmail.com> - 2012-01-10 14:33 -0500
  Re: Toughest Game Show Question Kaz Kylheku <kaz@kylheku.com> - 2012-01-11 01:44 +0000
    Re: Toughest Game Show Question Dann Corbit <dcorbit@connx.com> - 2012-01-10 23:48 -0800
      Re: Toughest Game Show Question James Kuyper <jameskuyper@verizon.net> - 2012-01-11 07:23 -0500
    Re: Toughest Game Show Question "M. Strobel" <sorry_no_mail_here@nowhere.dee> - 2012-01-11 14:33 +0100
      Re: Toughest Game Show Question Willem <willem@toad.stack.nl> - 2012-01-11 13:48 +0000
        Re: Toughest Game Show Question "M. Strobel" <sorry_no_mail_here@nowhere.dee> - 2012-01-11 18:44 +0100
          Re: Toughest Game Show Question Willem <willem@toad.stack.nl> - 2012-01-11 18:10 +0000
            Re: Toughest Game Show Question "M. Strobel" <sorry_no_mail_here@nowhere.dee> - 2012-01-11 19:23 +0100
          Re: Toughest Game Show Question James Kuyper <jameskuyper@verizon.net> - 2012-01-12 18:23 -0500
            Re: Toughest Game Show Question "M. Strobel" <sorry_no_mail_here@nowhere.dee> - 2012-01-15 19:03 +0100
            Re: Toughest Game Show Question Willem <willem@toad.stack.nl> - 2012-01-15 19:42 +0000
              Re: Toughest Game Show Question James Kuyper <jameskuyper@verizon.net> - 2012-01-15 16:57 -0500
              Re: Toughest Game Show Question Kaz Kylheku <kaz@kylheku.com> - 2012-01-16 04:16 +0000
                Re: Toughest Game Show Question gazelle@shell.xmission.com (Kenny McCormack) - 2012-01-16 12:14 +0000
                Re: Toughest Game Show Question Bill Reid <hormelfree@gmail.com> - 2012-01-17 07:31 -0800
                Re: Toughest Game Show Question Tim Rentsch <txr@alumni.caltech.edu> - 2012-01-26 09:33 -0800
    Re: Toughest Game Show Question "BartC" <bc@freeuk.com> - 2012-01-12 21:07 +0000
      factor jgk@panix.com (Joe keane) - 2012-01-12 22:53 +0000
      Re: Toughest Game Show Question pete <pfiland@mindspring.com> - 2012-01-14 23:38 -0500

csiph-web