C, 91 102 bytes, corrected (again), golfed, and tested for real this time:
<strike>s(c){p,f,d;for(p=2,f=d=0;p<c&&!d;){if(c%p==0){c/=p;++f;if(c%p==0)d=1;}++p;}c==p&&f==2&&!d;}</strike>
s(c){int p,f,d;for(p=2,f=d=0;p<c&&!d;){if(c%p==0){c/=p;++f;if(c%p==0)d=1;}++p;}return c==p&&f==2&&!d;}
/* This also works in 93 bytes, but since I forgot about the standard rules barring default int type on dynamic variables, and about the not allowing implicit return values without assignments, I'm not going to take it:
p,f,d;s(c){for(p=2,f=d=0;p<c&&!d;){if(c%p==0){c/=p;++f;if(c%p==0)d=1;}++p;}p=c==p&&f==2&&!d;}
(Who said I knew anything about C? ;-)
Here's the test frame with shell script in comments:
/* betseg's program for sphenic numbers from
*/
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <math.h> /* compile with -lm */
/* l,j;a(i){for(l=1,j=0;l<i;i%++l?:(i/=l,j++));l=i==1&&j==3;} */
#if defined GOLFED
l,j;a(i){for(l=1,j=0;l++<i;fmod((float)i/l,l)?i%l?:(i/=l,j++):(j=9));l=i==1&&j==3;}
#else
int looker, jcount;
int a( intval ) {
for( looker = 1, jcount = 0;
looker++ < intval;
/* Watch odd intvals and even lookers, as well. */
fmod( (float)intval/looker, looker )
? intval % looker /* remainder? */
? 0 /* dummy value */
: ( inval /= looker, jcount++ /* reduce the parameter, count factors */ )
: ( jcount = 9 /* kill the count */ )
)
/* empty loop */;
looker = intval == 1 && jcount == 3; /* reusue looker for implicit return value */
}
#endif
/* for (( i=0; $i < 100; i = $i + 1 )) ; do echo -n at $i; ./sphenic $i ; done */
I borrowed betseg's previous answer to get to my version.
This is my version of betseg's algorithm, which I golfed to get to my solution:
/* betseg's repaired program for sphenic numbers
*/
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
int sphenic( int candidate )
{
int probe, found, dups;
for( probe = 2, found = dups = 0; probe < candidate && !dups; /* empty update */ )
{
int remainder = candidate % probe;
if ( remainder == 0 )
{
candidate /= probe;
++found;
if ( ( candidate % probe ) == 0 )
dups = 1;
}
++probe;
}
return ( candidate == probe ) && ( found == 2 ) && !dups;
}
int main( int argc, char * argv[] ) { /* Make it command-line callable: */
int parameter;
if ( ( argc > 1 )
&& ( ( parameter = (int) strtoul( argv[ 1 ], NULL, 0 ) ) < ULONG_MAX ) ) {
puts( sphenic( parameter ) ? "true" : "false" );
}
return EXIT_SUCCESS;
}
/* for (( i=0; $i < 100; i = $i + 1 )) ; do echo -n at $i; ./sphenic $i ; done */
60
จำนวน sphenic?2 × 2 × 3 × 5