File tree Expand file tree Collapse file tree 1 file changed +42
-0
lines changed Expand file tree Collapse file tree 1 file changed +42
-0
lines changed Original file line number Diff line number Diff line change
1
+ // coderbyte solution for prime checker
2
+ // splashinn
3
+
4
+ function isPrime ( num ) {
5
+ for ( var i = 2 ; i <= num / 2 ; i ++ ) {
6
+ if ( num % i === 0 ) {
7
+ return false ;
8
+ }
9
+ }
10
+ return num !== 1 ;
11
+ }
12
+ function FindAllPermutations ( str , index , buffer ) {
13
+ if ( typeof str == "string" )
14
+ str = str . split ( "" ) ;
15
+ if ( typeof index == "undefined" )
16
+ index = 0 ;
17
+ if ( typeof buffer == "undefined" )
18
+ buffer = [ ] ;
19
+ if ( index >= str . length )
20
+ return buffer ;
21
+ for ( var i = index ; i < str . length ; i ++ )
22
+ buffer . push ( ToggleLetters ( str , index , i ) ) ;
23
+ return FindAllPermutations ( str , index + 1 , buffer ) ;
24
+ }
25
+
26
+ function ToggleLetters ( str , index1 , index2 ) {
27
+ if ( index1 != index2 ) {
28
+ var temp = str [ index1 ] ;
29
+ str [ index1 ] = str [ index2 ] ;
30
+ str [ index2 ] = temp ;
31
+ }
32
+ return str . join ( "" ) ;
33
+ }
34
+ function PrimeChecker ( num ) {
35
+ var nums = FindAllPermutations ( num + '' ) ;
36
+ for ( var i = 0 ; i < nums . length ; i ++ ) {
37
+ if ( isPrime ( parseInt ( nums [ i ] ) ) ) {
38
+ return 1 ;
39
+ }
40
+ }
41
+ return 0 ;
42
+ }
You can’t perform that action at this time.
0 commit comments