Created
February 4, 2024 01:35
-
-
Save singularitti/fccb2fe6d3ffd9ee4023438b5e00e524 to your computer and use it in GitHub Desktop.
Checking for digits in an integer in Julia #Julia #numbers #algorithm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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