Float to whole integer keeping digits after decimal [closed] - ruby

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

Related

Can someone explain this solution? [closed]

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.

Generate all possible combinations of 5 binary numbers so that there sum is less or equal to 3 [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
How can I generate a matrix in Matlab that has 5 rows and some specific number of columns and the elements may only be binary numbers and the column sum has to be less or equal to 3?
Some possibilites without loops:
Using strings:
D = 5;
S = 3;
numbers = str2mat(dec2bin(0:2^D-1))-'0';
numbers = numbers(sum(numbers,2)<=S,:);
Using combinatorial numbers, one line:
numbers = [zeros(1,D); cell2mat(arrayfun(#(s) fliplr(full(sparse((1:nchoosek(D,s)).'*ones(1,s), nchoosek(1:D,s), 1))), 0:S, 'uni', 0).')];
How about this: The maximum binary number, that you can represent by 5bit is 2^5-1 = 31 and skip through these to find the ones with sum of digits <= 3.
Something like
n = 1:1:31;
for ii = 1:length(ii)
bin = dec2bin(ii)
digitSum = 0
for d = 1:length(bin)
digitSum = digitSum + str2num(bin(d))
end
if (digitSum <= 3)
%store results
end
end
Here is a vecotorized solution to provide all occurences efficiently:
Bstr =dec2bin(1:31);
Bstr(sum(dec2bin(0:31),2)<=sum('00111'),:)=='1'
Inspired by the solution of #pyStarter

How do I interpolate subtraction in a Ruby string [closed]

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?

How can I solve The Riddle of Nine 9's programmatically? [closed]

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

How to calculate the inverse factorial of a real number? [closed]

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 12 years ago.
Improve this question
Is there some way to calculate the inverse factorials of real numbers?
For example - 1.5 ! = 1.32934039
Is there some way to obtain 1.5 back if I have the value 1.32934039?
I am trying
http://www.wolframalpha.com/input/?i=Gamma^(-1)[1.32934039]
but that is a fail.
Using wolframalpha.com, you can ask for
Solve[Gamma[x+1]==1.32934039,x]
As mentioned in the comments, Gamma does not have a unique inverse. True even when you are solving for a conventional factorial, e.g.
Solve[Gamma[x+1]==6,x]
yields several answers, of which one is 3.
Instead of using Gamma[] in WolframAlpha, you can also use Factorial[]:
Solve[Factorial[x]==6,x]
Solve[Factorial[x]==1.32934039,x]
David Cantrell gives a good approximation of Γ-1(n) on this page:
k = the positive zero of the digamma function, approximately 1.461632
c = Sqrt(2*pi)/e - Γ(k), approximately 0.036534
L(x) = ln((x+c)/Sqrt(2*pi))
W(x) = Lambert W function
ApproxInvGamma(x) = L(x) / W(L(x) / e) + 1/2
For integers you can do:
i = 2
n = someNum
while (n != 1):
n /= i
i += 1
return (i==1 ? i : None)
The factorial for real numbers has no inverse. You say that "each function must have an inverse". That is incorrect. Consider the constant function f(x)=0. What is f^-1(42)? For a function to be inverse it must be both an injection and a surjection.

Resources