Substring factor
Does a positive integer have a substring as a factor?
Input
- A positive integer.
Output
- One of 2 distinct values to indicate whether the input has a factor that is a strict substring of its base 10 (decimal) representation.
- Only strict substrings count - a string does not count as a strict substring of itself.
- A substring must be contiguous, unlike a subsequence.
- The factor does not need to be a prime factor - any divisor will do, including a composite factor or 1.
- Zero is not a factor of any number.
Examples
Example with a single digit input
Input: 4
Output: false
A single digit integer has no non-empty strict substrings, so the output for any single digit input will be false
(or whichever value you choose to consistently represent this).
Example with a substring factor
Input: 370
Output: true
The input has a substring 37, and 37 is a factor of 370 (that is, 370 can be divided by 37 with no remainder).
Example with only a subsequence factor
Input: 253
Output: false
The input has a subsequence 23, and 23 is a factor of 253, but 23 is not a substring (that is, not a contiguous subsequence), so this does not count. None of the substrings of 253 is a factor of 253.
Example with a composite factor
Input: 44
Output: true
The input has a substring 4, and 4 is a factor of 44. Factors do not need to be prime.
Example with 1 as a factor
Input: 31
Output: true
The input has a substring 1, and 1 is a factor of 31.
Test cases
- Test cases are in the format
input : output
. - You may use any 2 distinct values in place of
true
andfalse
.
4 : false
31 : true
44 : true
253 : false
370 : true
123456 : true
345678 : true
456789 : false
300007 : false
700003 : false
672297 : true
828477 : true
836537 : true
963833 : true
4506337 : true
5480778 : true
6355037 : true
23278699 : true
22222223 : false
Scoring
This is a code golf challenge. Your score is the number of bytes in your code. Lowest score for each language wins.
Explanations are optional, but I'm more likely to upvote answers that have one.
[Haskell], 101 95 bytes ``` …
5mo ago
JavaScript, 42 bytes s= …
4mo ago
Japt v1.4.5, 7 bytes ⬠…
4mo ago
3 answers
Haskell, 101 95 bytes
import Data.List
r a=or$map(a?)$(map read$tail.inits=<<tails(show a))\\[a]
a?b=b/=0&&a`mod`b==0
Changelog:
- Saved 4 bytes by replacing
any id
withor
. - Saved 2 bytes by η-expanding
map read.(tail.inits=<<).tails.show$a
tomap read$tail.inits=<<tails(show a)
Explanation:
-
tails
returns all suffixes of a given list (note that a string is a list).inits
returns all prefixes of a given list. By binding (aka=<<
) the two together, we get all of the contiguous substrings. By also bindingtail
, which drops the first item from a list, we remove the empty string from this list (sinceinits
always puts the empty string first in its output). This gives us\a->tail.inits=<<tails a
- We need to first convert our Int to a string, and convert our list of strings back to a list of ints at the end. We do this by adding
show
andmap read
:\a->map read$tail.inits=<<tails(show a)
- But we still need to get rid of the input number itself. Since we've already imported Data.List for
inits
andtails
, we can use the list difference operator(\\)
, which saves us some bytes over theremove
function:(map read$tail.inits=<<tails(show a))\\[a]
- Next we need to check if any of the numbers in the list are factors of the input. For this, we create the
(?)
operator, which checks if its second argument is a factor of its first. Firstly,0
is not a factor of any number, so we check if the second argument is not zero. (Notice that the "not equal" operator in Haskell is(/=)
.) Then, we can use themod
function to get the modulo and check if that is zero. Iffb
is not0
anda `mod` b
is0
, thena?b
will returnTrue
:a?b=b/=0&&a`mod`b==0
- Then
map
a partial application of the(?)
operator over our list of substring numbers:\a->map(a?)$(map read$tail.inits=<<tails(show a))\\[a]
- Finally, collapse the list into a single boolean output by using the
or
function, which takes a list of booleans and returnsTrue
only if any member of the list isTrue
.
0 comment threads
Japt v1.4.5, 7 bytes
â¬d!øUs
â¬d!øUs :Implicit input of integer U
⬠:Divisors, excluding itself
d :Are any
!ø : Contained in
Us : The string representation of U
Or, if we could simply output truthy or falsey values:
6 bytes
⬣søX
⬣søX :Implicit input of integer U
⬠:Divisors, excluding itself
£ :Map each X
s : String representation of U
øX : Contains X
0 comment threads
JavaScript, 42 bytes
s=>(g=n=>--n&&s.includes(s%n?g:n)|g(n))(s)
0 comment threads