Problem 74. Balanced number
Given a positive integer find whether it is a balanced number. For a balanced number the sum of first half of digits is equal to the second half.
Examples:
Input n = 13722 Output tf is true
because 1 + 3 = 2 + 2.
Input n = 23567414 Output tf = true
All palindrome numbers are balanced.
This is partly from Project Euler, Problem 217.
Solution Stats
Problem Comments
-
1 Comment
there is a problem with the test.
it does not accept the following code:
s1=[]
s2=[]
l=length(s)
if mod(l,2)~= 0
for i = 1:ceil(length(s)/2)-1
s1 = [s1,str2double(s(i))]
end
for i = ceil(length(s)/2)+1:l
s2=[s2,str2double(s(i))]
end
else
for i = 1:ceil(length(s)/2)
s1 = [s1,str2double(s(i))]
end
for i = ceil(length(s)/2)+1:l
s2=[s2,str2double(s(i))]
end
end
if sum(s1)==sum(s2)
tf = 1
else
tf = 0
end
Solution Comments
Show commentsProblem Recent Solvers2988
Suggested Problems
-
Return the 3n+1 sequence for n
8270 Solvers
-
How to find the position of an element in a vector without using the find function
2728 Solvers
-
All your base are belong to us
540 Solvers
-
Find nearest prime number less than input number
712 Solvers
-
Basics: 'Find the eigenvalues of given matrix
416 Solvers
More from this Author96
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!