Pascal decimal-point [closed] - pascal

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
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
Closed 8 years ago.
Improve this question
could you please help me with my homework? I am just a beginner and know very little about Pascal :(
I am supposed to write a program for division but have special conditions depending on their decimal numbers. The outputs should look something like this:
no decimals then 20/5=4 (there can only be the 4; it can't be like 4.00)
if decimals then only 1 decimal number 9/4=2.3
if the decimal is an infinite of the same number then 1/3=0.(3)
How can I do it? I was thinking about putting it into an array but I don't know how to find where the decimal point is nor I know how to write the brackets into an output.

I can't manage to solve the 3rd point but here is the code for the first 2:
Var
x,y : integer;
z : real;
Begin
z := x / y;
if z = x div y
then Write(z:4:0) //Write a real var without any decimals
else Write(z:4:1); //Write a real var with just 1 decimal
End.
You'll have to change things around , like reading x and y , but from this code you can almost do your homework .
I'll try to solve the 3rd time , check the answer again after some time.

To solve 3rd point: think about x and y as about a fraction. Here is the basic idea:
A:=A/GCD(A,B);B:=B/GCD(A,B)
Integer part := A div B; A:=A mod B
Non-periodical part: while A mod 2=0 do BEGIN C:=C*2;A:=A div 2; END; while A mod 5=0 do BEGIN C:=C*5;A:=A div 5; END
Find M so that A*M=999..999.

Related

Why are floating-point numbers directly compared in go standard library? [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 8 days ago.
Improve this question
In golang standard source code, file: src/math/dim.go
func max(x, y float64) float64 {
// special cases
switch {
case IsInf(x, 1) || IsInf(y, 1):
return Inf(1)
case IsNaN(x) || IsNaN(y):
return NaN()
case x == 0 && x == y:
if Signbit(x) {
return y
}
return x
}
if x > y {
return x
}
return y
}
related: https://floating-point-gui.de/errors/comparison/
The page you link to seems to suggest that you should avoid comparing floating point numbers. The reason is that if you do two widely different things depending on which one of two floating point numbers is bigger than the other, you might get surprises if the two numbers are almost equal but not quite, because rounding errors might mean that one of them is bigger than the other in a way that you do not expect.
Note that this is only a problem if you could do two very different things when comparing two very close floating point numbers.
What makes this implementation of max acceptable is that when the floating point numbers are very close to each other, you end up doing two things that are very close to each other as well (you are returning one of them), so you won't get any discontinuity issue.
Note however that you may still get unexpected behavior, like for instance
max(0.15 + 0.15, 0.1 + 0.2) == 0.3 may be false. But it's not a problem in the max function, it's a problem in what you do with the result.

Comparing inequalities for Go bigInt? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I am trying to compare two big ints. I'm looking at the docs: https://pkg.go.dev/math/big#Int and I don't see an inequality operator, but I do see an equality one (Cmp).
How am I meant to compare a big int a to a big int b?
Am I meant to subtract b from a and compare the sign? Or is there something I am missing? E.g.
c := (new.bigInt).Sub(a, b)
i := c.Sign()
if i < 0 {
fmt.Println("a < b")
}
It seems a little odd to me there is not an inequality operator, nor much about this online so I think I am doing something wrong.
Cmp returns: -1 if x < y, 0 if x == y, +1 if x > y
Go Playground Example

Check if palindrome can be formed [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
Given a string S how can we check that weather we can convert it to palindrome or not by just changing one single letter. Please help a fast solution to do it.
Example : Let us have S = "mixem". Then the answer is YES as we can change "e" to "i" to make it palindrome.
String of length can be 1000 at max.
Assuming 0-based indexing, it suffices to check, for each 0 <= i < your_string_length, if the number of positions for which the characters at positions i and your_string_length - i - 1 are different is at most 2. If yes then the answer is also yes, otherwise it's not.
Basically, this checks how many pairs of characters with equal distance from the sides of the string are different: if only one such pair is different, then we can change one of its characters and make it equal. If more pairs, then we'd need to change more characters.
If I understood your example correctly it means that you can change a single letter to ANY other letter. This makes the algorythm embarrassingly simple. You need to parse the word from sides into center and calculate number of mistakes. If it is <=1 then you can convert into a palindrome with 1 change or it is already a palindrome.
var s = "mixen", errors = 0;
for(var i = 0; i < s.length/2; i++){
if(s[i] != s[s.length-1-i]){
errors++;
}
var isPalindrom = errors == 0;
var isPalindromConvertible = errors <= 1;
PS. You didn't specify language, so i've written in most convenient for me now: javascript

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 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