Is it a Sphenic Number?

29

4

A Sphenic Number is a number that is the product of exactly three distinct primes. The first few Sphenic numbers are 30, 42, 66, 70, 78, 102, 105, 110, 114. This is sequence A007304 in the OEIS.

Your Task:

Write a program or function to determine whether an inputted integer is a Sphenic number.

Input:

An integer between 0 and 10^9, which may or may not be a Sphenic Number.

Output:

A truthy/falsy value indicating whether the input is a Sphenic Number.

Examples:

30  -> true
121 -> false
231 -> true
154 -> true
4   -> false
402 -> true
79  -> false
0   -> false
60  -> false
64  -> false
8   -> false
210 -> false

Scoring:

This is , shortest code in bytes wins.

Gryphon

Posted 2017-07-23T15:13:59.473

Reputation: 6 697

Is 60 a sphenic number? 2 × 2 × 3 × 5 – Erik the Outgolfer – 2017-07-23T15:30:23.783

1@EriktheOutgolfer that's not the product of 3 distinct primes though, that's the product of 3 distinct and 1 duplicate prime. – Rɪᴋᴇʀ – 2017-07-23T15:31:43.840

1@Riker I'm not really sure if "3 distinct primes" means "3 primes that are all distinct" or "when uniquified there should remain 3 primes". EDIT: Oh I see, 60 isn't a sphenic number. (waiting for OP clarification) – Erik the Outgolfer – 2017-07-23T15:32:58.853

@EriktheOutgolfer According to the definition of sphenic numbers, 60 is not one of them. I do not know however if 60 is valid for this challenge. – Post Rock Garf Hunter – 2017-07-23T15:59:16.703

@WheatWizard, 60 is not a sphenic number (e.g. output/return falsy). – Gryphon – 2017-07-23T18:31:04.183

Can we output a falsy value if the input is sphenic and a truthy value if not? – Okx – 2017-07-24T08:28:46.963

@No, sorry. Strictly Sphenic=truthy. – Gryphon – 2017-07-24T23:06:09.310

You should add a test case with 4 or more prime factors like 210 – edc65 – 2017-07-25T10:27:08.290

Related – Okx – 2017-08-07T19:16:06.570

Answers

8

Brachylog, 6 3 bytes

ḋ≠Ṫ

Try it online!

Explanation

ḋ        The prime factorization of the Input…
 ≠       …is a list of distinct elements…
  Ṫ      …and there are 3 elements

Fatalize

Posted 2017-07-23T15:13:59.473

Reputation: 32 976

2And then there's the one language which has a builtin like . – Erik the Outgolfer – 2017-07-24T16:24:41.840

And the builtin as well. – Zacharý – 2017-07-25T18:17:24.217

1@Zacharý is not really a built-in predicate though; it is a built-in variable: a list of 3 variable elements. It's a fairly useful pre-constrained variable in many different challenges. – Fatalize – 2017-07-26T06:33:03.237

Congratulations on the shortest answer. – Gryphon – 2017-07-28T11:56:24.750

11

bash, 43 bytes

factor $1|awk '{print $2-$3&&$3-$4&&NF==4}'

Try it online!

Input via command line argument, outputs 0 or 1 to stdout.

Fairly self-explanatory; parses the output of factor to check that the first and second factors are different, the second and third are different (they're in sorted order, so this is sufficient), and there are four fields (the input number and the three factors).

Doorknob

Posted 2017-07-23T15:13:59.473

Reputation: 68 138

11

MATL, 7 bytes

_YF7BX=

Try it online! Or verify all test cases.

Explanation

_YF   % Implicit input. Nonzero exponents of prime-factor decomposition
7     % Push 7
B     % Convert to binary: gives [1 1 1] 
X=    % Is equal? Implicit display

Luis Mendo

Posted 2017-07-23T15:13:59.473

Reputation: 87 464

@Suever I was thinking about that, but then falsy ouput becomes uglier (either empty with error or an array with some zeros). Not sure if I should... – Luis Mendo – 2017-07-23T16:02:47.723

4X= is the saddest builtin I've ever seen. – Erik the Outgolfer – 2017-07-23T16:25:18.643

9

C, 88 78 126 58 77 73 + 4 (lm) = 77 bytes

l,j;a(i){for(l=1,j=0;l++<i;fmod(1.*i/l,l)?i%l?:(i/=l,j++):(j=9));l=i==1&&j==3;}

Ungolfed commented explanation:

look, div; //K&R style variable declaration. Useful. Mmm.

a ( num ) { // K&R style function and argument definitions.

  for (
    look = 1, div = 0; // initiate the loop variables.
    look++ < num;) // do this for every number less than the argument:

      if (fmod(1.0 * num / look, look))
      // if num/look can't be divided by look:

        if( !(num % look) ) // if num can divide look
          num /= look, div++; // divide num by look, increment dividers
      else div = 9;
      // if num/look can still divide look
      // then the number's dividers aren't unique.
      // increment dividers number by a lot to return false.

  // l=j==3;
  // if the function has no return statement, most CPUs return the value
  // in the register that holds the last assignment. This is equivalent to this:
  return (div == 3);
  // this function return true if the unique divider count is 3
}

Try it online!

betseg

Posted 2017-07-23T15:13:59.473

Reputation: 8 493

1Consider i*1.0/l instead of the cast to float. (And since l,j are global they are initialized to 0 for free, you don't need to do that if the function is only called once. Not sure what the rule is for that.) – Mat – 2017-07-24T17:43:52.577

76 bytes – ceilingcat – 2018-12-07T23:19:41.360

5

Jelly, 8 bytes

ÆEḟ0⁼7B¤

Try it online!

Uses Luis Mendo's algorithm.

Explanation:

ÆEḟ0⁼7B¤
ÆE       Prime factor exponents
  ḟ0     Remove every 0
    ⁼7B¤ Equal to 7 in base 2?

Erik the Outgolfer

Posted 2017-07-23T15:13:59.473

Reputation: 38 134

5

CJam, 11 bytes

rimFz1=7Yb=

Try it online! Or verify all test cases.

Explanation

Based on my MATL answer.

ri    e# Read integer
mF    e# Factorization with exponents. Gives a list of [factor exponent] lists
z     e# Zip into a list of factors and a list of exponents
1=    e# Get second element: list of exponents
7     e# Push 7
Yb    e# Convert to binary: gives list [1 1 1]
=     e# Are the two lists equal? Implicitly display

Luis Mendo

Posted 2017-07-23T15:13:59.473

Reputation: 87 464

4

05AB1E, 7 5 bytes

ÓnO3Q

Try it online!

Uses Dennis's algorithm.

Erik the Outgolfer

Posted 2017-07-23T15:13:59.473

Reputation: 38 134

4

Husk, 6 bytes

≡ḋ3Ẋ≠p

Try it online!

Returns 1 for sphenic numbers and 0 otherwise.

Explanation

≡ḋ3Ẋ≠p    Example input: 30
     p    Prime factors: [2,3,5]
   Ẋ≠     List of absolute differences: [1,2]
≡         Is it congruent to...       ?
 ḋ3           the binary digits of 3: [1,1]

In the last passage, congruence between two lists means having the same length and the same distribution of truthy/falsy values. In this case we are checking that our result is composed by two truthy (i.e. non-zero) values.

Leo

Posted 2017-07-23T15:13:59.473

Reputation: 8 482

4

Mathematica, 31 bytes

SquareFreeQ@#&&PrimeOmega@#==3&

martin

Posted 2017-07-23T15:13:59.473

Reputation: 1 335

Since you're already testing for squarefree-ness, PrimeNu will do just as well as PrimeOmega, and is shorter. – Mark S. – 2017-07-25T10:36:28.170

4

Jelly, 6 bytes

ÆE²S=3

Try it online!

How it works

ÆE²S=3  Main link. Argument: n

ÆE      Compute the exponents of n's prime factorization.
  ²     Take their squares.
   S    Take the sum.
    =3  Test the result for equality with 3.

Dennis

Posted 2017-07-23T15:13:59.473

Reputation: 196 637

2

Actually, 7 bytes

w♂N13α=

Try it online!

Explanation:

w♂N13α=
w       Push [prime, exponent] factor pairs
 ♂N     Map "take last element"
   1    Push 1
    3   Push 3
     α  Repeat
      = Equal?

Erik the Outgolfer

Posted 2017-07-23T15:13:59.473

Reputation: 38 134

2

Haskell, 59 bytes

f x=7==sum[6*0^(mod(div x a)a+mod x a)+0^mod x a|a<-[2..x]]

Try it online!

Post Rock Garf Hunter

Posted 2017-07-23T15:13:59.473

Reputation: 55 382

2

Ruby, 81 49 46 bytes

Includes 6 bytes for command line options -rprime.

->n{n.prime_division.map(&:last)==[1]*3}

Try it online!

daniero

Posted 2017-07-23T15:13:59.473

Reputation: 17 193

2

J, 15 bytes

7&(=2#.~:@q:)~*

Try it online!

Explanation

7&(=2#.~:@q:)~*  Input: integer n
              *  Sign(n)
7&(         )~   Execute this Sign(n) times on n
                 If Sign(n) = 0, this returns 0
          q:       Prime factors of n
       ~:@         Nub sieve of prime factors
    2#.            Convert from base 2
   =               Test if equal to 7

miles

Posted 2017-07-23T15:13:59.473

Reputation: 15 654

Very nice use of ~: and #. An alternative could be (7&(=#.@~:@q:)~*) which I find a little easier to read, but is no shorter. – bob – 2017-07-26T03:44:22.467

2

Dyalog APL, 26 bytes

⎕CY'dfns'
((≢,∪)≡3,⊢)3pco⎕

Try it online!

Uriel

Posted 2017-07-23T15:13:59.473

Reputation: 11 708

2

Python 3, 54 53 bytes

lambda n:sum(1>>n%k|7>>k*k%n*3for k in range(2,n))==6

Thanks to @xnor for golfing off 1 byte!

Try it online!

Dennis

Posted 2017-07-23T15:13:59.473

Reputation: 196 637

You can check squarefreeness with k*k%n rather than n%k**2 – xnor – 2017-07-24T05:50:47.357

Right, I only need one failure. Thanks! – Dennis – 2017-07-24T06:05:49.990

2

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 */

Joel Rees

Posted 2017-07-23T15:13:59.473

Reputation: 141

Does it answer the question, now? – Joel Rees – 2017-07-24T11:42:40.877

Yes, it does. Insert this to link to betseg's answer: [betseg's answer](https://codegolf.stackexchange.com/a/135203/65836). You can also click edit on his answer to suggest an edit to it, if you want, that would include the explanation - no promises on whether it'll be approved or not. – Stephen – 2017-07-24T11:43:30.657

I'm here now, and I fixed my program, it's at 87 bytes now; but your program looks good too. – betseg – 2017-07-24T13:05:09.813

@betseg Interesting that you used floating point this time. Oh, and thanks for letting me borrow your algorithm. ;-) – Joel Rees – 2017-07-24T13:30:13.353

@JoelRees i added explanation to my answer, also your answer has a problem i think? it doesn't seem to work correctly: Try It Online

– betseg – 2017-07-24T13:41:37.097

reason of floats: 105 = 357, but 105/2%2 = 0.5, which C interprets as 0. – betseg – 2017-07-24T13:49:37.600

@betseg I think I called the working uncompressed version and missed the errors mixed with the warnings. (Want to blame it on my pre-C89 compiler, but that's on the other side of the ocean. :-/) I'll fix my code and my count. Nice test rig you have. – Joel Rees – 2017-07-24T14:09:33.733

1

Pyth, 9 bytes

&{IPQq3lP

Try it here.

Erik the Outgolfer

Posted 2017-07-23T15:13:59.473

Reputation: 38 134

1

J, 23 bytes

0:`((~.-:]*.3=#)@q:)@.*

Try it online!

Handling 8 and 0 basically ruined this one...

q: gives you all the prime factors, but doesn't handle 0. the rest of it just says "the unique factors should equal the factors" and "the number of them should be 3"

Jonah

Posted 2017-07-23T15:13:59.473

Reputation: 8 729

This fails for input 60 – Conor O'Brien – 2017-07-23T18:57:06.110

@ConorO'Brien thanks. See my edit -- fixing 60 helped, but i realized i also wasn't handling 0 correctly, and handling that more than doubles the bytes – Jonah – 2017-07-23T19:14:26.877

The last one was my original idea, and that fails for 8. – Conor O'Brien – 2017-07-23T19:21:50.657

I have (6=]#@,~.)@q: as a possible solution – Conor O'Brien – 2017-07-23T19:22:24.490

@ConorO'Brien ah good point about 8. yours will fail for 0, though. – Jonah – 2017-07-23T19:31:55.923

@ConorO'Brien well, i fixed all the edge cases, it can hardly be considered golf anymore... sigh. – Jonah – 2017-07-23T19:58:34.997

(6=]#@,~.)@(0:`q:@.*) for 21 – Conor O'Brien – 2017-07-23T20:05:06.583

@ConorO'Brien, nice. (6=]#@,~.) is some clever exploitation of the parsing rules. – Jonah – 2017-07-23T20:15:41.887

1

Python 2, 135 121 bytes

  • Quite long since this includes all the procedures: prime-check, obtain-prime factors and check sphere number condition.
lambda x:(lambda t:len(t)>2and t[0]*t[1]*t[2]==x)([i for i in range(2,x)if x%i<1and i>1and all(i%j for j in range(2,i))])

Try it online!

officialaimm

Posted 2017-07-23T15:13:59.473

Reputation: 2 739

1

Python 2, 59 bytes

lambda x:6==sum(5*(x/a%a+x%a<1)+(x%a<1)for a in range(2,x))

Try it online!

Post Rock Garf Hunter

Posted 2017-07-23T15:13:59.473

Reputation: 55 382

This gives false positives like 48. I had been trying the same thing. – xnor – 2017-07-23T18:55:24.563

@xnor Fixed, but at the cost of bytes. – Post Rock Garf Hunter – 2017-07-23T19:07:26.363

1

Javascript (ES6), 87 bytes

n=>(a=(p=i=>i>n?[]:n%i?p(i+1):[i,...p(i,n/=i)])(2)).length==3&&a.every((n,i)=>n^a[i+1])

Example code snippet:

f=
n=>(a=(p=i=>i>n?[]:n%i?p(i+1):[i,...p(i,n/=i)])(2)).length==3&&a.every((n,i)=>n^a[i+1])

for(k=0;k<10;k++){
  v=[30,121,231,154,4,402,79,0,60,64][k]
  console.log(`f(${v}) = ${f(v)}`)
}

Herman L

Posted 2017-07-23T15:13:59.473

Reputation: 3 611

1

Japt, 14 bytes

k
k@è¥X ÉÃl ¥3

Try it online!

Justin Mariner

Posted 2017-07-23T15:13:59.473

Reputation: 4 746

@Oliver That would result in passing a function to Number.k(), which would have no effect and just check if the input has 3 prime factors, not 3 distinct prime factors. That would mean 8 (with three prime factors: 2, 2, 2) would pass despite not being in A007304

– Justin Mariner – 2017-07-24T20:00:33.080

Ah, you're right. I was just going by the test cases. – Oliver – 2017-07-24T20:05:59.240

@Oliver Yeah, that really threw me for a loop when working on this solution. I just added 8 to the test cases for that reason. – Justin Mariner – 2017-07-24T20:09:34.910

1

Mathematica, 44 bytes

Plus@@Last/@#==Length@#==3&@FactorInteger@#&

Try it online!

J42161217

Posted 2017-07-23T15:13:59.473

Reputation: 15 931

1

J, 15 14 19 bytes

Previous attempt: 3&(=#@~.@q:)~*

Current version: (*/*3=#)@~:@q: ::0:

How it works:

(*/*3=#)@~:@q: ::0:  Input: integer n
               ::0:  n=0 creates domain error in q:, error catch returns 0
            q:       Prime factors of n
         ~:@         Nub sieve of prime factors 1 for first occurrence 0 for second
(*/*3=#)@            Number of prime factors is equal to 3, times the product across the nub sieve (product is 0 if there is a repeated factor or number of factors is not 3)

This passes for cases 0, 8 and 60 which the previous version didn't.

bob

Posted 2017-07-23T15:13:59.473

Reputation: 131

1why not 3=#~.q: for 7characters? From a J session 3=#~.q: 30 ==> 1 and 3=#~.q: 20 ==> 0 – Richard Donovan – 2017-07-25T19:24:44.163

Richard, your suggestion gives a false positive for n=60 and creates a domain error for n=0, but my previous version failed for n=60 as well. Your comment prompted me to strive for a correct solution! – bob – 2017-07-26T03:14:25.477

1

VB.NET (.NET 4.5), 104 bytes

Function A(n)
For i=2To n
If n Mod i=0Then
A+=1
n\=i
End If
If n Mod i=0Then A=4
Next
A=A=3
End Function

I'm using the feature of VB where the function name is also a variable. At the end of execution, since there is no return statement, it will instead pass the value of the 'function'.

The last A=A=3 can be thought of return (A == 3) in C-based languages.

Starts at 2, and pulls primes off iteratively. Since I'm starting with the smallest primes, it can't be divided by a composite number.

Will try a second time to divide by the same prime. If it is (such as how 60 is divided twice by 2), it will set the count of primes to 4 (above the max allowed for a sphenic number).

Try It Online!

Brian J

Posted 2017-07-23T15:13:59.473

Reputation: 653

1

Dyalog APL, 51 49 48 46 45 43 bytes

1∊((w=×/)∧⊢≡∪)¨(⊢∘.,∘.,⍨){⍵/⍨2=≢∪⍵∨⍳⍵}¨⍳w←⎕

Try it online! (modified so it can run on TryAPL)

I wanted to submit one that doesn't rely on the dfns namespace whatsoever, even if it is long.

Zacharý

Posted 2017-07-23T15:13:59.473

Reputation: 5 710

0

Perl 6, 43 bytes

{3==grep ->\a{a.is-prime&&$_%%(a^a*a)},^$_}

Try it online!

The grep produces a list of the numbers a up to, but not including, the argument to the function $_, such that:

  • a is prime (a.is-prime); and
  • exactly one of a and a*a (a ^ a*a) divide the argument.

The latter condition excludes prime factors that occur in multiplicities of higher than one.

Sean

Posted 2017-07-23T15:13:59.473

Reputation: 4 136

0

Mathematica, 66 57 bytes

Length@#1==3&&And@@EqualTo[1]/@#2&@@(FactorInteger@#)&

Defines an anonymous function.

is Transpose.

Explanation

FactorInteger gives a list of pairs of factors and their exponents. E.g. FactorInteger[2250]=={{2,1},{3,2},{5,3}}. This is transposed for ease of use and fed to the function Length@#1==3&&And@@EqualTo[1]/@#2&. The first part, Length@#1==3, checks that there are 3 unique factors, while the second, And@@EqualTo[1]/@#2 checks that all the exponents are 1.

DanTheMan

Posted 2017-07-23T15:13:59.473

Reputation: 3 140

0

PHP, 66 bytes:

for($p=($n=$a=$argn)**3;--$n;)$a%$n?:$p/=$n+!++$c;echo$c==7&$p==1;

Run as pipe with -nR or try it online.

Infinite loop for 0; insert $n&& before --$n to fix.

breakdown

for($p=($n=$a=$argn)**3;    # $p = argument**3
    --$n;)                  # loop $n from argument-1
    $a%$n?:                     # if $n divides argument
        $p/=$n                      # then divide $p by $n
        +!++$c;                     # and increment divisor count
echo$c==7&$p==1;            # if divisor count is 7 and $p is 1, argument is sphenic

example
argument = 30:
prime factors are 2, 3 and 5
other divisors are 1, 2*3=6, 2*5=10 and 3*5=15
their product: 1*2*3*5*6*10*15 is 27000 == 30**3

Titus

Posted 2017-07-23T15:13:59.473

Reputation: 13 814

0

Python 99 bytes

def s(n):a,k=2,0;exec('k+=1-bool(n%a)\nwhile not n%a:n/=a;k+=10**9\na+=1\n'*n);return k==3*10**9+3

First submission. Forgive me if I did something wrong. Kinda silly, counts the number of factors of n, and then the number of times n is divisible by each (by adding 10**9).

I'm pretty sure there are a few easy ways to cut off ~10-20 characters, but I didn't.

Also this is intractably slow at 10**9. Could be made okay by changing '...a+=1\n'*n to '...a+=1\n'*n**.5, as we only need to go to the square root of n.

Backerupper

Posted 2017-07-23T15:13:59.473

Reputation: 41

0

Pari/GP, 34 bytes

n->if(n,moebius(n)*omega(n)==-3,0)

Try it online!

alephalpha

Posted 2017-07-23T15:13:59.473

Reputation: 23 988

0

Forth, 153

: w swap ;
: d 2dup ;
: o rot ;
: sp 0 w 1 
begin 1+ d mod 0= if w over / w d mod 0= if o 16 + o o then o 1+ o o then 
d > 0= until drop 1 = w 3 = and ;

( 153 including spaces. Spaces are required delimiters in Forth. )

( Test frame for compressed version: )

: testsp
  0 do i 8 .r space
    i sp if ." true" else ." false" then
    cr
  loop
;

( This uses a corrected version of betseg's answer.)

( Uncompressed, with comments: )

: sphenic ( n -- f )
  0 ( counter )
  swap 1 ( counter n probe )
  begin
    1+ ( increment probe )
    2dup mod ( Remainder? )
    0= if
      swap over / ( reduced-n )
      swap ( bring probe back )
      2dup mod 0= if 
        rot 128 + rot rot
      then
      rot 1+ rot rot ( count and put count back )
    then ( counter reduced-n probe )
    2dup > 0= 
  until
  drop 1 = 
  swap 3 = and
;

: testsphenic
  0 do i 8 .r space
    i sphenic if ." true" else ." false" then
    cr
  loop
;

Joel Rees

Posted 2017-07-23T15:13:59.473

Reputation: 141

0

C#, 172 Bytes

I'd appreciate improvement suggestions:

n=>{var r=Enumerable.Range(2,n).Where(i=>Enumerable.Range(2,i-2).All(a=>i%a!=0));return r.SelectMany(x=>r.SelectMany(y=>r.Select(z=>x==z|x==y|y==z?-1:x*y*z))).Contains(n);}

With formatting:

n =>
{
    var r = Enumerable.Range (2, n).Where (i => Enumerable.Range (2, i - 2).All (a => i % a != 0));
    return r.SelectMany (
        x => r.SelectMany (
            y => r.Select (z => x == z | x == y | y == z ? -1 : x * y * z))).Contains (n);
}

And as whole programm:

using System;
using System.Linq;


namespace S
{
    class P
    {
        static void Main ()
        {
            Func<int, bool> s =
                    n =>
                    {
                        var r = Enumerable.Range (2, n).Where (i => Enumerable.Range (2, i - 2).All (a => i % a != 0));
                        return r.SelectMany (
                            x => r.SelectMany (
                                y => r.Select (z => x == z | x == y | y == z ? -1 : x * y * z))).Contains (n);
                    }
                ;

            for (var i = 0; i < 1000; i++)
                if (s (i))
                    Console.WriteLine (i);
            Console.ReadLine ();
        }
    }
}

If you add the byte count of the using directives (which are needed for the code to compile), you'd get 203 Bytes.

Try it online!

MetaColon

Posted 2017-07-23T15:13:59.473

Reputation: 391

Would you be to make a function with a single letter name for Enumerable.Range(2, m)? You call it twice, and it might save some bytes. – Brian J – 2017-07-27T18:52:17.537

@BrianJ The idea doesn't sound bad, but I don't know how I'd do it. Is it possible to define methods inside of statement lambdas? – MetaColon – 2017-07-27T19:03:17.210

You can, but I forgot how verbose it becomes. Quick and dirty, I got Func<int, IEnumerable<int>> e = a => { return Enumerable.Range(2, a); };, plus another using. – Brian J – 2017-07-27T19:11:01.027

0

JavaScript (ES6), 80 bytes

n=>(p=(n,f=2)=>n%f?p(n,f+1):f,(a=p(n))<n&&(b=p(n/=a))<n&&(c=p(n/=b))==n&a<b&b<c)

Using a recursive function to get the smaller factor.
Output 1 if sphenic, false if there are 2 or less factors and 0 otherwise

Test

F=
n=>(p=(n,f=2)=>n%f?p(n,f+1):f,(a=p(n))<n&&(b=p(n/=a))<n&&(c=p(n/=b))==n&a<b&b<c)

;[30,121,231,154,4,402,79,0,60,64,8,210].forEach(
  x=>console.log(x,F(x))
)

edc65

Posted 2017-07-23T15:13:59.473

Reputation: 31 086

0

C#7 122 bytes

bool F(int n){int P(int m,int f=2)=>m%f>0?P(m,f+1):f;int a,b,c;return(a=P(n))<n&&(b=P(n/=a))<n&&(c=P(n/=b))==n&a<b&b<c;}  

porting of my JS answer

edc65

Posted 2017-07-23T15:13:59.473

Reputation: 31 086