Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Challenges

Substring factor

+2
−0

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 and false.
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.

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.
Why should this post be closed?

0 comment threads

3 answers

You are accessing this answer with a direct link, so it's being shown above all other answers regardless of its score. You can return to the normal view.

+2
−0

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

Try it online!

Changelog:

  • Saved 4 bytes by replacing any id with or.
  • Saved 2 bytes by η-expanding map read.(tail.inits=<<).tails.show$a to map 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 binding tail, which drops the first item from a list, we remove the empty string from this list (since inits 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 and map 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 and tails, we can use the list difference operator (\\), which saves us some bytes over the remove 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 the mod function to get the modulo and check if that is zero. Iff b is not 0 and a `mod` b is 0, then a?b will return True: 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 returns True only if any member of the list is True.
History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+1
−0

Japt v1.4.5, 7 bytes

â¬d!øUs

Try it

â¬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

Try it

⬣søX     :Implicit input of integer U
⬠        :Divisors, excluding itself
  £        :Map each X
   s       :  String representation of U
    øX     :  Contains X
History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+1
−0

JavaScript, 42 bytes

s=>(g=n=>--n&&s.includes(s%n?g:n)|g(n))(s)

Try it online!

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

Sign up to answer this question »