Question :
You will be given the starting and ending integers of a sequence and should return the number of integers within it which do not contain the digit 5. The start and end numbers should be included!
Examples:
1,9 → 1,2,3,4,6,7,8,9 → Result 8
4,17 → 4,6,7,8,9,10,11,12,13,14,16,17 → Result 12
50,60 → 60 → Result 1
-59,-50 → → Result 0
The result may contain five.
The start number will always be smaller than the end number. Both numbers can be also negative!
I'm very curious for your solutions and the way you solve it. Maybe someone of you will find an easy pure mathematics solution.
Edit This is a code-golf challenge, so the shortest code wins.
56 Answers 56
JavaScript (ES6), (削除) 36 (削除ここまで) 33 bytes
Takes input with currying syntax (a)(b).
a=>F=b=>b<a?0:!/5/.test(b)+F(b-1)
Formatted and commented
a => // outer function: takes 'a' as argument, returns F
F = b => // inner function F: takes 'b' as argument, returns the final result
b < a ? // if b is less than a
0 // return 0
: // else
!/5/.test(b) + // add 1 if the decimal representation of b does not contain any '5'
F(b - 1) // and do a recursive call to F with b - 1
Test cases
let f =
a=>F=b=>b<a?0:!/5/.test(b)+F(b-1)
console.log(f(1)(9))
console.log(f(4)(17))
console.log(f(50)(60))
console.log(f(-50)(-59))
-
\$\begingroup\$ (I normally prefer
testoverexecwhen you only need a boolean.) \$\endgroup\$Neil– Neil2017年01月20日 09:01:27 +00:00Commented Jan 20, 2017 at 9:01 -
\$\begingroup\$ @Neil That makes more sense indeed. Updated. \$\endgroup\$Arnauld– Arnauld2017年01月20日 09:07:45 +00:00Commented Jan 20, 2017 at 9:07
-
\$\begingroup\$ NB: I couldn't find any tip about ES6 currying syntax, so I wrote one. \$\endgroup\$Arnauld– Arnauld2017年01月20日 10:51:04 +00:00Commented Jan 20, 2017 at 10:51
-
6\$\begingroup\$ @TheLethalCoder
b<ais there to stop the recursion after counting through all numbers frombtoa, so removing it would just cause an infinite recursion. \$\endgroup\$ETHproductions– ETHproductions2017年01月20日 12:37:10 +00:00Commented Jan 20, 2017 at 12:37 -
1\$\begingroup\$ @HristiyanDodov The unnamed outer function takes
aas argument and returns theFfunction, which in turn takesbas argument and -- as you noticed -- is called recursively to iterate frombtoa, incrementing a counter for all integers that do not contain a5in their decimal representation. \$\endgroup\$Arnauld– Arnauld2017年01月20日 14:06:00 +00:00Commented Jan 20, 2017 at 14:06
Jelly, (削除) 8 (削除ここまで) 7 bytes
-1 byte thanks to Dennis (use fact that indexing into a number treats that number as a decimal list)
rAw5ドル¬S
How?
rAw5ドル¬S - Main link: from, to e.g. -51, -44
r - range(from, to) e.g. [-51,-50,-49,-48,-47,-46,-45,-44]
A - absolute value e.g. [51,50,49,48,47,46,45,44]
w€ - first index of... for €ach (0 if not present)
5 - five e.g. [1,1,0,0,0,0,2,0]
¬ - logical not e.g. [0,0,1,1,1,1,0,1]
S - sum e.g. 5
* The absolute value atom, A is necessary since a negative number cast to a decimal list has negative entries, none of which would ever be a 5 (the given example would count all eight rather than two).
-
\$\begingroup\$
rAw5ドル¬Ssaves a byte. \$\endgroup\$Dennis– Dennis2017年01月20日 08:22:22 +00:00Commented Jan 20, 2017 at 8:22 -
\$\begingroup\$ @Dennis thanks! Is my description "treats that number as a decimal list" accurate? \$\endgroup\$Jonathan Allan– Jonathan Allan2017年01月20日 08:36:38 +00:00Commented Jan 20, 2017 at 8:36
-
2\$\begingroup\$ Pretty much.
wcasts an integer argument to its decimal digits. \$\endgroup\$Dennis– Dennis2017年01月20日 08:37:53 +00:00Commented Jan 20, 2017 at 8:37
2sable, (削除) 6 (削除ここまで) 5 bytes
Saved a byte thanks to Adnan
Ÿ5¢_O
Explanation
Ÿ # inclusive range
5¢ # count 5's in each element of the range
_ # negate
O # sum
Note: This works due to a bug in ¢ making the function apply itself to each element instead of counting matching elements in the list.
-
\$\begingroup\$ You can remove the
`as it behaves the same on arrays :p. \$\endgroup\$Adnan– Adnan2017年01月20日 11:34:54 +00:00Commented Jan 20, 2017 at 11:34 -
\$\begingroup\$ @Adnan: Thanks! I was gonna test that but forgot ;) \$\endgroup\$Emigna– Emigna2017年01月20日 12:45:27 +00:00Commented Jan 20, 2017 at 12:45
Python2, (削除) 59 (削除ここまで) (削除) 55 (削除ここまで) (削除) 52 (削除ここまで) (削除) 51 (削除ここまで) (削除) 47 (削除ここまで) (削除) 43 (削除ここまで) 42 bytes
f=lambda a,b:a<=b and-(`5`in`a`)-~f(a+1,b)
A recursive solution. Thanks to @xnor for giving me motivation to find a solution using logical operators! Also, thanks to @JonathanAllan and @xnor for guiding me and chopping the byte from 43 to 42!
Other attempts at 43 bytes
f=lambda a,b:a<=b and-~-(`5`in`a`)+f(a+1,b)
f=lambda a,b:a<=b and 1-(`5`in`a`)+f(a+1,b)
-
\$\begingroup\$ Would
if!`x`.count('5')work? \$\endgroup\$Titus– Titus2017年01月20日 06:29:46 +00:00Commented Jan 20, 2017 at 6:29 -
2\$\begingroup\$ @Titus Python has
notoperator that is!in C-like languages, but that takes 3 bytes :( \$\endgroup\$Yytsi– Yytsi2017年01月20日 06:31:41 +00:00Commented Jan 20, 2017 at 6:31 -
1\$\begingroup\$ Think about using logical short-circuiting with
andandor. \$\endgroup\$xnor– xnor2017年01月20日 07:37:43 +00:00Commented Jan 20, 2017 at 7:37 -
1\$\begingroup\$ Yup, nicely done! Now think about shortening that
not. \$\endgroup\$xnor– xnor2017年01月20日 07:48:22 +00:00Commented Jan 20, 2017 at 7:48 -
1\$\begingroup\$ You're really close! Keep trying stuff. \$\endgroup\$xnor– xnor2017年01月20日 08:28:12 +00:00Commented Jan 20, 2017 at 8:28
05AB1E, (削除) 8 (削除ここまで) (削除) 7 (削除ここまで) 6 bytes
Saved a byte thanks to Adnan
Ÿ5.å_O
Explanation
Ÿ # inclusive range
5.å # map 5 in y for each y in the list
_ # logical negation
O # sum
-
\$\begingroup\$ 05AB1E also has vectorized
å, which is.å, so you can doŸ5.å_Ofor 6 bytes. \$\endgroup\$Adnan– Adnan2017年01月20日 11:24:12 +00:00Commented Jan 20, 2017 at 11:24 -
\$\begingroup\$
negatemeaning-n, orn==0?1:0? \$\endgroup\$ETHproductions– ETHproductions2017年01月20日 12:38:05 +00:00Commented Jan 20, 2017 at 12:38 -
\$\begingroup\$ @ETHproductions: Sorry, that was unclear. I meant logical negation, so
n==0?1:0\$\endgroup\$Emigna– Emigna2017年01月20日 12:46:47 +00:00Commented Jan 20, 2017 at 12:46
Pyth, (削除) 9 (削除ここまで) 8 bytes
Saved a byte thanks to FryAmTheEggman!
lf-5円T}E
Explanation:
Q # Input
}E # Form an inclusive range starting from another input
# order is reversed, but doesn't matter
f-5円T # Filter by absence of '5'
l # Count the number of elements left
Perl 6, 23 bytes
{+grep {!/5/},$^a..$^b}
How it works
{ } # A lambda.
$^a..$^b # Range between the two lambda arguments.
grep {!/5/}, # Get those whose string representation doesn't match the regex /5/.
+ # Return the size of this list.
Haskell, 39 bytes
s!e=sum[1|x<-[s..e],notElem '5'$show x]
Try it online! Usage:
Prelude> 4 ! 17
12
Explanation:
[s..e] -- yields the range from s to e inclusive
x<-[s..e] -- for each x in this range
x<-[s..e],notElem '5'$show x -- if the char '5' is not in the string representation of x
[1|x<-[s..e],notElem '5'$show x] -- then add a 1 to the resulting list
s!e=sum[1|x<-[s..e],notElem '5'$show x] -- take the sum of the list
R, 33 bytes
f=function(x,y)sum(!grepl(5,x:y))
Usage:
> f=function(x,y)sum(!grepl(5,x:y))
> f(40,60)
[1] 10
> f(1,9)
[1] 8
> f(4,17)
[1] 12
Groovy, (削除) 47 (削除ここまで) (削除) 45 (削除ここまで) (削除) 43 (削除ここまで) 40 bytes
{a,b->(a..b).findAll{!(it=~/5/)}.size()}
This is an unnamed closure. findAll is similar to adding an if condition in a list comprehension in python.
PHP 7.1, (削除) 57 (削除ここまで) 55 bytes
for([,$a,$b]=$argv;$a<=$b;)$n+=!strstr($a++,53);echo$n;
Run with php -r '<code>' <a> <b>
-
\$\begingroup\$ Isn't this PHP7.1 syntax? \$\endgroup\$aross– aross2017年01月20日 10:06:44 +00:00Commented Jan 20, 2017 at 10:06
-
\$\begingroup\$ @aross: It is. But PHP 7.1 is older than 5 hours (pubished on December, 1) \$\endgroup\$Titus– Titus2017年01月20日 10:50:47 +00:00Commented Jan 20, 2017 at 10:50
-
1\$\begingroup\$ of course, I just asked because I'm used to specifying the version if it's 7 or up. That's also kind of the convention for Python \$\endgroup\$aross– aross2017年01月20日 11:00:04 +00:00Commented Jan 20, 2017 at 11:00
-
1\$\begingroup\$ Convention for PHP - as far as I have seen - is to use the most recent version unless specified otherwise. \$\endgroup\$Titus– Titus2017年01月20日 16:12:29 +00:00Commented Jan 20, 2017 at 16:12
-
\$\begingroup\$ I don't think many people have the latest minor version. The least common denominator at the moment would probably be 5.5. Personally I'm using FC 25 (considered pretty cutting edge), which currently distributes PHP 7.0. If you're on Windows you probably need to update manually. \$\endgroup\$aross– aross2017年01月20日 16:17:03 +00:00Commented Jan 20, 2017 at 16:17
Mathematica, (削除) 46 (削除ここまで) (削除) 44 (削除ここまで) 42 bytes
Thanks to alephalpha and DavidC for saving 2 bytes each!
Tr@Boole[FreeQ@5/@IntegerDigits@Range@##]&
Unnamed function taking two integer arguments and returning an integer. IntegerDigits@Range@## converts all the numbers between the inputs into lists of digits; FreeQ@5 tests those lists to decide which ones do not contain any 5. Then Boole converts booleans to zeros and ones, and Tr sums the results.
Other solutions (44 and 47 bytes):
Count[Range@##,x_/;IntegerDigits@x~FreeQ~5]&
IntegerDigits@x~FreeQ~5 determines whether the list of digits of a number is free of 5s, and Count[Range@##,x_/;...]& counts how many numbers between the inputs pass that test.
Tr[Sign[1##&@@IntegerDigits@#-5]^2&/@Range@##]&
1##&@@IntegerDigits@#-5 takes the list of digits of a number, subtracts 5 from all of them, and multplies the answers together; Sign[...]^2 then converts all nonzero numbers to 1.
-
1\$\begingroup\$
Count[Range@##,x_/;IntegerDigits@x~FreeQ~5]&\$\endgroup\$DavidC– DavidC2017年01月20日 12:59:11 +00:00Commented Jan 20, 2017 at 12:59 -
1\$\begingroup\$
Tr@Boole[FreeQ@5/@IntegerDigits@Range@##]&\$\endgroup\$alephalpha– alephalpha2017年01月21日 03:42:52 +00:00Commented Jan 21, 2017 at 3:42
Ruby, (削除) 36 (削除ここまで) 35 bytes
->a,b{(a..b).count{|x|!x.to_s[?5]}}
Thx IMP1 for -1 byte
-
1\$\begingroup\$ Doesn't this return the list without the numbers containing 5, rather than the size of that list? \$\endgroup\$IMP1– IMP12017年01月20日 10:49:04 +00:00Commented Jan 20, 2017 at 10:49
-
\$\begingroup\$ You are right, I have copy/pasted the wrong version. \$\endgroup\$G B– G B2017年01月20日 11:00:16 +00:00Commented Jan 20, 2017 at 11:00
-
1\$\begingroup\$ You can also use
?5(the'5'character) instead of/5/ in the search to save a byte. \$\endgroup\$IMP1– IMP12017年01月20日 11:03:28 +00:00Commented Jan 20, 2017 at 11:03
Java 7, (削除) 80 (削除ここまで) 78 bytes
int c(int a,int b){int r=0;for(;a<=b;)r+=(""+a++).contains("5")?0:1;return r;}
Ungolfed:
int c(int a, int b){
int r = 0;
for (; a <= b; ) {
r += ("" + a++).contains("5")
? 0
: 1;
}
return r;
}
Test code:
class M{
static int c(int a,int b){int r=0;for(;a<=b;)r+=(""+a++).contains("5")?0:1;return r;}
public static void main(String[] a){
System.out.println(c(1, 9));
System.out.println(c(4, 17));
}
}
Output:
8
12
PowerShell, (削除) 42 (削除ここまで) 41 bytes
param($a,$b)$a..$b|%{$z+=!($_-match5)};$z
Called from the command line as .\no5s.ps1 1 20
-
1\$\begingroup\$ You can drop the space to save a byte. With strictly numerical regex patterns, you don't need a delimiter (e.g.,
-replace3or-split1or-notmatch5). \$\endgroup\$AdmBorkBork– AdmBorkBork2017年01月20日 15:18:06 +00:00Commented Jan 20, 2017 at 15:18
Regex 🐇 (Perl / PCRE), (削除) 160 (削除ここまで) 147 bytes
^(?(?=-x*,(?!-))-?+x*(,x*(?!$))?|-?(?=(x*),-?(x*))(?=(x*),?(?=2円)(?=3円)(x*,-?|[^,]*)(x*))4,円?+x*)(?!((?=(x+)(8円{9}x*))9円)*(x{10})*x{5}\b)(?(4)4円6円)
Try it online! - Perl
Try it online! - PCRE (faster)
Takes its input in unary, as two strings of x characters whose lengths represent the numbers, with optional - signs on their left side, joined by a , delimiter. Returns its output as the number of ways the regex can match. (The rabbit emoji indicates this output method.)
Most of the logic of this regex is taken up with handling the logic of integer ranges. It's rather unnatural... literally. Natural numbers (including zero) are much easier for a regex to operate on.
^
(?(?=-x*,(?!-))
# Do the following if A is negative and B is nonnegative
-?+ # skip sign of A, if any
x* # handle the range from larger to zero, inclusive
(,x*(?!$))? # handle the range from B inclusive to zero exclusive
|
# Do the following if A is nonnegative or B is negative
-? # skip sign of A, if any
(?=
(x*), # 2円 = abs(A);
-?(x*) # 3円 = abs(B)
)
(?=
(x*),?(?=2円)(?=3円) # 4円 = what to skip so tail = the larger of A or B;
# tail = the larger of A or B
(x*,-?|[^,]*)(x*) # 6円 = {the smaller of A or B} - 4円
)
4,円?+ # tail = the larger of A or B
x* # handle the range from larger to {smaller or zero}
)
(?! # Negative lookahead - assert this can't match
(
(?=(x+)(8円{9}x*)) # 8円 = floor(tail / 10); 9円 = tail - 8円
9円 # tail = tail - 9円 == 8円
)* # Iterate the above any number of times, minimum zero
(x{10})*x{5}\b # Assert tail % 10 == 5
)
(?(4)4円6円) # If 4円 is set, clamp the range at the smaller end
It uses conditionals, which are not supported by ECMAScript, but it'd be easy enough to port (though the regex would be even longer). The 🐇 output method is required for this algorithm to be possible in ECMAScript (there isn't yet a regex engine that can emulate ECMAScript and count possible matches, but I'm planning on adding this to RegexMathEngine soon).
Regex (.NET), 163 bytes
^(?=(-x*,(?!-))?)(?>-?)(?(1)|(?=(x*),-?(x*))(?=(x*),?(?=2円)(?=3円)(x*,-?|[^,]*)(x*))4円(?>,?))(?(,)(?(1),)|(?(((?=(x+)(8円{9}x*))9円)*(x{10})*x{5}\b)|())x?)*(?(4)4円6円)
Returns its output as the capture count of group 11円.
^
(?=
(-x*,(?!-))? # 1円 = set iff A is negative and B is nonnegative
)
(?>-?) # skip sign of A, if any
(?(1)
|
# Do the following if A is nonnegative or B is negative
(?=
(x*), # 2円 = abs(A);
-?(x*) # 3円 = abs(B)
)
(?=
(x*),?(?=2円)(?=3円) # 4円 = what to skip so tail = the larger of A or B;
# tail = the larger of A or B
(x*,-?|[^,]*)(x*) # 6円 = {the smaller of A or B} - 4円
)
4円 # tail = the larger of A or B
(?>,?) # skip sign of B, if any
)
# First handle the range from larger inclusive to
# {smaller inclusive or 0 exclusive}
(?(,)
(?(1),) # If crossing from negative to nonnegative, handle
# the inclusive range from B to 0
|
(?(
(
(?=(x+)(8円{9}x*)) # 8円 = floor(tail / 10); 9円 = tail - 8円
9円 # tail = tail - 9円 == 8円
)* # Iterate the above any number of times
(x{10})*x{5}\b # Assert tail % 10 == 5
)
|
() # Push a capture onto the 11円 stack
)
x? # Advance by 1, but if already at the end, allow the
# loop to iterate once more so as to include 0 in
# the count.
)*
(?(4)4円6円) # If 4円 is set, clamp the range at the smaller end
It could also be ported to an output method of the sum of the lengths of two capture groups (two, because the output can exceed the largest of the two inputs), though this would probably make it significantly longer.
Thunno 2 S, 6 bytes
Id5ȷc~
(No ATO link since it's down at the moment)
Explanation
Id5ȷc~ # Implicit input -> 1, 10
I # Inclusive range -> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
d # Cast to digits -> [[1],[2],[3],[4],[5],[6],[7],[8],[9],[1,0]]
ȷ # To each list: -> [1] [2] [3] [4] [5] [6] [7] [8] [9] [1,0]
5 c # Count 5s -> 0 0 0 0 1 0 0 0 0 0
~ # Logical NOT -> [1, 1, 1, 1, 0, 1, 1 1, 1, 1]
# Implicit sum -> 9
# Implicit output
Python 2, (削除) 61 (削除ここまで) 56 bytes
lambda a,b:len([n for n in range(a,b+1) if not"5"in`n`])
-5 bytes thanks to tukkaaX
-
\$\begingroup\$ Don't get discouraged! Having fun and challenging yourself is what matters. You can remove two whitespaces at
not "5" in:) Also, if you're using Python2, you can surroundxwith `` quotes, instead of doingstr(x). \$\endgroup\$Yytsi– Yytsi2017年01月20日 06:59:54 +00:00Commented Jan 20, 2017 at 6:59 -
\$\begingroup\$ @TuukkaX Thanks! also removed space between in and `x` \$\endgroup\$sagiksp– sagiksp2017年01月20日 07:52:16 +00:00Commented Jan 20, 2017 at 7:52
-
\$\begingroup\$ You can remove the
[]. You also don't need the space beforeif. \$\endgroup\$Dennis– Dennis2017年01月20日 08:16:45 +00:00Commented Jan 20, 2017 at 8:16 -
\$\begingroup\$ @Dennis I tried that already, but it complains that "object of type 'generator' has no len()". \$\endgroup\$Yytsi– Yytsi2017年01月20日 08:26:11 +00:00Commented Jan 20, 2017 at 8:26
-
\$\begingroup\$ @TuukkaX Right.
lambda a,b:sum(not"5"in`n`for n in range(a,b+1))works though. tio.run/nexus/… \$\endgroup\$Dennis– Dennis2017年01月20日 08:29:35 +00:00Commented Jan 20, 2017 at 8:29
Swift 52 bytes
(0ドル...1ドル).filter { !String(0ドル).contains("5") }.count
-
\$\begingroup\$ Since your challenge is a codegolf challenge, you should include your bytecount. Also, in codegolf (at least here), it's a requirement that all programs muse be actually contending (e.g. your function name can be just a single character, your actual function can probably be reduced to a single line). I don't know Swift, you might have to correct me on stuff. \$\endgroup\$clismique– clismique2017年01月20日 05:53:05 +00:00Commented Jan 20, 2017 at 5:53
Batch, 95 bytes
@set/an=0,i=%1
:g
@if "%i%"=="%i:5=%" set/an+=1
@set/ai+=1
@if %i% leq %2 goto g
@echo %n%
Manually looping saves some bytes because I need the loop counter in a variable anyway.
PHP, 56 bytes
for($i=$argv[1];$i<=$argv[2];)trim(5,$i++)&&$x++;echo$x;
Run like this:
php -r 'for($i=$argv[1];$i<=$argv[2];)trim(5,$i++)&&$x++;echo$x;' 1 9 2>/dev/null;echo
> 8
A version for PHP 7.1 would be 53 bytes (credits to Titus):
for([,$i,$e]=$argv;$i<=$e;)trim(5,$i++)&&$x++;echo$x;
Explanation
for(
$i=$argv[1]; # Set iterator to first input.
$i<=$argv[2]; # Loop until second input is reached.
)
trim(5,$i++) && $x++; # Trim string "5" with the characters in the
# current number; results in empty string when
# `5` is present in the number. If that is not
# the case, increment `$x`
echo$x; # Output `$x`
-
\$\begingroup\$ Ah dang I forgot about the second
trimparameter again. \$\endgroup\$Titus– Titus2017年01月20日 10:51:42 +00:00Commented Jan 20, 2017 at 10:51
CJam "easy pure mathematics solution", 60
{{Ab5+_,\_5#)<9円e]);_4f>.m9b}%}:F;q~_:z$\:*0>{((+F:-}{F:+)}?
It takes the numbers in any order, in an array.
Explanation:
One core problem is to calculate f(n) = the number of non-5 numbers from 1 to n (inclusive) for any positive n. And the answer is: take n's decimal digits, replace all digits after the first 5 (if any) with 9, then replace all digits 5..9 with 4..8 (decrement), and convert from base 9. E.g. 1752 → 1759 → 1648 → 1*9^3+6*9^2+4*9+8=1259. Basically, each digit position has 9 acceptable values, and a 5xxxx is equivalent to a 49999 because there are no more valid numbers between them.
Once we solved this, we have a few cases: if the input numbers (say a and b, a<b) are (strictly) positive, then the result is f(b)-f(a-1). If they are negative, then we can take the absolute values, reorder them and use the same calculation. And if a<=0<=b then the result is f(-a)+f(b)+1.
The program first implements the function F as described above (but applied to each number in an array), then reads the input, converts the numbers to the absolute value and reorders them, and uses one of the 2 calculations above, based on whether a*b>0 initially.
-
\$\begingroup\$ Not "pure" but nice method. here, get a +1 :) \$\endgroup\$Matthew Roh– Matthew Roh2017年01月23日 09:13:49 +00:00Commented Jan 23, 2017 at 9:13
-
\$\begingroup\$ @MatthewRoh thanks, but what do you mean not pure? It's a solution that does fairly direct mathematical calculations on the input numbers, without iterating through the range. What else were you expecting? \$\endgroup\$aditsu quit because SE is EVIL– aditsu quit because SE is EVIL2017年01月23日 09:35:29 +00:00Commented Jan 23, 2017 at 9:35
Python 2, 54 bytes
i,j=input();k=0
while i<=j:k+=not"5"in`i`;i+=1
print k
Not the shortest Python answer Uses same algorithm but a different way of implementing with a while loop and is not a lambda function.
-
\$\begingroup\$ It is a program and not a function and it uses while instead of for. What is not different? OK, it is still looking for a string "5" inside the incremented input, agreed. Is there a better way? \$\endgroup\$ElPedro– ElPedro2017年01月20日 13:10:57 +00:00Commented Jan 20, 2017 at 13:10
-
\$\begingroup\$ That's exactly what it is and that's why it is deferent. Sorry, maybe should have made my comment different. \$\endgroup\$ElPedro– ElPedro2017年01月20日 18:34:12 +00:00Commented Jan 20, 2017 at 18:34
-
\$\begingroup\$ Same algorithm, different way of implementing. No problem with your comments. Is that better worded? \$\endgroup\$ElPedro– ElPedro2017年01月20日 18:36:48 +00:00Commented Jan 20, 2017 at 18:36
-
\$\begingroup\$ It is, yes :) I'll remove these comments to make the comment section look clean. \$\endgroup\$Yytsi– Yytsi2017年01月20日 20:58:27 +00:00Commented Jan 20, 2017 at 20:58
C++ | in too many bytes, (削除) 165 (削除ここまで) 125 thanks to Christoph!
int main(){int c=0;for(int i=0;i<=8;i++){int d;int n=i>=0?i:-i;while(n!=0){if(d=n%10==5){break;}n=n/10;c++;break;}}return c;}
I took the liberty of creating a function e_ to determine if a 5(or any other number) is present in an integer instead of using to_string() and .find() so that must count for something. note: e_ is only declared as an extra function in the un-golfed version for readability.
un-golfed:
int e_(int e,int i){
int d;
int n=i>=0?i:-i;
while (n != 0){
d=n%10;
if (d==e){
return 1;}
n=n/10;}
return 0;}
int main() {
int l = 1;int h = 8;int e = 5;int c = 0;
for(int i=l; i<=h; i++){
if (e_(e,i)==0)
c++;}
return c;}
How e_ function works:
int n=i>=0?i:-i; Inverses our number if it is less than 0 so it's always positive. d=n%10; Divides it by 10 and gets remainder (n%base; will always return the last digit of an integer). We check that it equals 5, if it does the number can be discarded, if not n=n/10; removes the end digit and loops again.
-
\$\begingroup\$
#include <iostream>andusing namespace std;can be dropped. Declaring an extra function is not going to save you bytes.if (e_(e,i)==0) c++;could be reduced toc+=e_(e,i)==0;. Come on ! You can do better ! :) \$\endgroup\$Christoph– Christoph2017年01月24日 14:05:32 +00:00Commented Jan 24, 2017 at 14:05 -
1\$\begingroup\$ Your right declaring an extra function is not going to save me bytes which is why It was only left in the un-golfed version for readability,
<iostream>andnamespace std;are unnecessary not sure why I didn't realise, habit I guess... thanks for the input and pointing out my mistake :) \$\endgroup\$GCaldL– GCaldL2017年01月27日 02:52:30 +00:00Commented Jan 27, 2017 at 2:52
Hoon, (削除) 110 (削除ここまで) 94 bytes
Hoon's range function, gulf, doesn't work for signed integers, which increases the length by a bit :(
=+
si^f=:(curr lien test 53)
|=
{a/@s b/@s}
|-
?:
=(a b)
(f <b>)
(add (f <a>) $(a (sum -1 a)))
Use the signed integer library. Create a function f: :(a b c d) is a macro that expands into (a b (a c d)) so this is (curr lien (curr test 53)), aka create a curried function that tests if any element of a list is 53 ('5')
Create a function that takes a and b. Create a loop: if a==b return f(tostring(b)), else return add(f(tostring(a)) recurse(a=a+1))
> =f =+
si^f=:(curr lien test 53)
|=
{a/@s b/@s}
|-
?:
=(a b)
(f <b>)
(add (f <a>) $(a (sum -1 a)))
> (f -1 -9)
8
> (f -4 -17)
12
> (f -50 -60)
1
> (f --59 --50)
0
(Hoon's signed integers use - as a prefix, so --5 is negative 5 and -5 is positive 5)
SmileBASIC, 55 bytes
INPUT S,E
FOR I=S TO E
INC N,INSTR(STR$(I),"5")<0NEXT?N
Nothing special, just uses INSTR and STR$ to check for 5.
Java 7, 77 bytes
This is an improvement of Kevins Answer, but since I don't have the reputation to comment yet, this new answer will have to do.
So what I did was:
- Replace the
indexOfstatements withcontains(-1 byte) - Move the incrementing part of the for-loop into the conditional statement (-2 bytes)
for-loop (77 bytes):
int c(int a,int b){int r=1;for(;a++<b;)r+=(""+a).contains("5")?0:1;return r;}
recursive (79 bytes):
int d(int r,int a,int b){r+=(""+a).contains("5")?0:1;return a!=b?d(r,a+1,b):r;}
Output:
8
12
8
12
Test it here !
-
\$\begingroup\$ Wellcome to PPCG ! Nice findings in an already quite nicely golfed answer. I don't know about Java that much but shouldn't
(""+a).contains("5")?0:1be replacable by!(""+a).contains("5")? \$\endgroup\$Christoph– Christoph2017年01月27日 06:56:08 +00:00Commented Jan 27, 2017 at 6:56 -
1\$\begingroup\$ @Christoph sadly no, since in Java a boolean really is just a boolean. So a ternary operation is the only way to go. \$\endgroup\$Tobias Meister– Tobias Meister2017年01月27日 09:13:15 +00:00Commented Jan 27, 2017 at 9:13
-
\$\begingroup\$ Hm that's sad. What about
(""+a).contains("5")||r++? \$\endgroup\$Christoph– Christoph2017年01月27日 09:15:01 +00:00Commented Jan 27, 2017 at 9:15 -
1\$\begingroup\$ @Christoph that won't work either, because you can't have a boolean expression on its own. I've been trying to make it work in other places (like the for-loop declaration) but not with much success. Nice idea tho ;) \$\endgroup\$Tobias Meister– Tobias Meister2017年01月27日 13:45:08 +00:00Commented Jan 27, 2017 at 13:45
50, 59 -> 0. \$\endgroup\$