Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
How do you do subtraction inside a string? I get zero instead of 90 percent.
#!/bin/ruby
puts " some text #{100%10} some text "
% is an alias for modulo.
10 / 3 #=> 3
10 % 3 #=> 1
I guess you want to write:
puts " some text #{100 - 10}% some text "
Answer is zero in this case because 100%10 is 0, i.e. the remainder of the division is 0. 100 is evenly divisable by 10. Maybe you meant to use - instead?
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I have a float that has eight digits after the decimal. I want to convert this to a whole number/integer, keeping the digits after the decimal. For example: my float is 0.06870697, and I want it to be 006870697.
Everything I've tried so far removes all the digits after the decimal.
Just multiply it to shift the digits, then use a printf-style formatter:
v = 0.06870697
'%09d' % (v * 10e7)
# => "006870697"
Multiplying a value by 10e7 (10 x 107 = 108) shifts the decimal place eight digits to the right.
The below solution is solving problem for the number in the question. You can do as follows.
value = 0.06870697
value.to_s.delete('.')
=> "006870697"
Not as elegant as tadman's but below versions seems working -))
1.
a = 0.06870697
a.to_s.split(".").join("")
2.
a.to_s.gsub(/[^\d]/, '')
3.
a.to_s.slice! "."
Yet another option:
val = 0.06870697
num = (val * 10 ** 8).to_i.to_s
num.rjust(9, '0')
=> "006870697"
You can shift the decimal point eight digits to the right by multiplying by 10^8.
(0.06870697 * 10 ** 8).to_i
# => 6870697
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
The prime factors of 13195 are 5, 7, 13 and 29.
What is the largest prime factor of the number 600851475143 ?
def largest_prime(number)
i = 2
largest_divisor = 0
while i < number
if number % i == 0
largest_divisor = i
number = number / i
i = 2
else
i += 1
end
end
number
end
The while-loop always looks for the smallest divisor of number (except 1).
If we find one, we store it, divide number and start anew.
Since we take the smallest divisor, this always stores a prime.
The loop can only end, if number == 1, which means we have divided by every factor.
And the last factor, that remained had to be the largest.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
I was using this formula to calculate last m digits of 2^n.
pow=2+(n-m)%(4*5^(m-1))
ans =(2^pow)%(10^m)**
But this is not working for n=2009 and m=3.
Suggest any error in my calculation or a better formula if there is.
I don't understand what your formula is doing, but the simplest way is to calculate (2^2009)%(10^m) . Here is a pseudo code to find (x^y)%mod in O(log y). Put x=2, y=2009 and mod=10^m
power(x,y)
{
if( y == 0)
return 1
temp = power(x, y/2)
if (y%2 == 0)
return (temp*temp)%mod
else
return ((x*temp%mod)*temp)%mod
}
Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 11 years ago.
Improve this question
i am wondering if it is possible to simplify:
T*V + V*T // V = V^(t) symmetric
where both operands are matrixes
I don't think this is possible due to the following considerations:
If we multiply two matrices A and T, where A is symmetric (i.e. A(i,j) = A(j,i)), we have the following:
For A*T we have that the item in row z and column s is computed as:
__n__
\
/ A(z,i)*T(i,s)
-----
i=1
For the other way around, T*A, we get for row z, column s:
__n__ __n__
\ \
/ T(z,i)*A(i,s) = / T(z,i)*A(s,i)
----- -----
i=1 i=1
So, as long as we do not know anything about the entries T(i,j) in T, I think we can not say how these sums relate to each other.
Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
How can I solve this riddle programmatically? Could someone help me with some pseudo-code or something?
Nine 9s
Combining nine 9's with any number of the operators +, -, *, /, (, ), what is the smallest positive integer that cannot be expressed?
Hints:
The answer isn't zero. You can express zero like this:
(9 - 9) * (9 + 9 + 9 + 9 + 9 + 9 + 9).
Also, zero isn't a positive integer.
The answer isn't one. You can express one like this:
9 - (9 * 9 - 9)/9 + 9 - 9 + 9 - 9
It's not a trick question.
Be sure to handle parentheses correctly.
Notes:
You cannot use exponentiation.
You cannot concatenate (for example,
put two 9's together to make 99).
The - operator can be used in either
its binary or unary form.
Assume base 10.
This is actually a famous puzzle and there are probably many solutions hovering around the internet. I am not sure if any of them is correct or not. Does anybody have a well explained solution?
The answer is 195, here is some Python code that simply builds up all possible expressions by forming new expressions from exp1 OP exp2. It runs in 0.165s on my PC.
exp = [set() for _ in xrange(10)]
exp[0].add(0)
exp[1].update([9, -9])
for i in xrange(1, 10):
for a in list(exp[i]):
for j in xrange(i, 10):
for b in list(exp[j-i]):
exp[j].update([a+b, a-b, a*b])
if b != 0:
exp[j].add(a/b)
n = 0
while n in exp[9]:
n += 1
print n
EDIT:
If the answers must be exact integers (and not just the rounded result of integer division) then a check must be done when division is done.
if ((b != 0) and ((a/b) == float(a)/b)):
exp[j].add(a/b)
Under this interpretation of the rules, the new answer is 138.
(the existing version computes 1386/10 [or -1386/-10] and gets 138)
195, http://members.iinet.net.au/~tmorrow/mathematics/ninenines/ninenines.html