Skip to content

Instantly share code, notes, and snippets.

@singularitti
Created February 4, 2024 01:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save singularitti/fccb2fe6d3ffd9ee4023438b5e00e524 to your computer and use it in GitHub Desktop.
Save singularitti/fccb2fe6d3ffd9ee4023438b5e00e524 to your computer and use it in GitHub Desktop.
Checking for digits in an integer in Julia #Julia #numbers #algorithm
# See https://youtu.be/ZYzlhp-W0a8
function digitsin(digits::Integer, number)
# Decimal representation of `digits` has N digits
base = 10
while (digits ÷ base > 0)
# `digits ÷ base` is same as `floor(Int, digits/base)`
base *= 10
end
# `base` is now the first integer power of 10 above `digits`, used to pick last N digits from `number`
while number > 0
if (number % base) == digits # Last N digits in `number` == digits
return true
end
number ÷= 10 # Remove the last digit from `number`
end
return false
end
if !digitsin(9, integer)
# Do somthing
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment