What relevance is 1.307 to the series 1 + 1/2 + 1/3 + 1/4... + 1/n [closed] - c#

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I'm currently working my way through the Nakov book, Fundamentals of Computer Programming in C#. In Chapter 4 question 12 states:
Write a program that calculates the sum (with the precision of 0.001) of the following sequence: 1 + 1/2 - 1/3 + 1/4 - 1/5 + …
It seemed to me to be a relatively straightforward question. The series is a diminishing fraction that does not have an asymptote. Stopping the loop at a certain point due to diminished changes in value meets the precision requirements AFAIC. However, the solution given in both the Hungarian and English versions of the book makes reference to an obscure (to me) value of 1.307. As follows:
Accumulate the sum of the sequence in a variable inside a while-loop (see the chapter "Loops"). At each step compare the old sum with the new sum. If the difference between the two sums Math.Abs(current_sum – old_sum) is less than the required precision (0.001), the calculation should finish because the difference is constantly decreasing and the precision is constantly increasing at each step of the loop. The expected result is 1.307.
Can someone explain what this might mean?

Note that header contains "harmonic sequence" that has no limit.
But question body shows alternate sign sequence that converges towards value 2 - ln(2)

The expected result is 1.307.
I think they are simply saying what the result of the calculation is, so you can check your answer.

The sequence you've got
1 + 1/2 - 1/3 + 1/4 + ...
is the same as the Alternating Harmonic Series on Wikipedia, except with the signs from 1/2 onwards flipped:
1 - 1/2 + 1/3 - 1/4 + ... = ln 2
and the natural logarithm of 2, ln 2, = 0.693. Hence your 1.307 here = 2 - ln 2.

Related

Why is Math.Round() not resulting as I expect in C# [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
Why does Math.Round(1.444445M, 2, MidpointRounding.AwayFromZero) return 1.44 instead of 1.45?
Because this is how rounding works.
Like #Sweeper said, 1.444445 is closer to 1.44 than to 1.45.
You take a number 1.444445.
Now you want to round it to 2 decimal places, so select 2 digits after dot: 1.[44]4445
Then look at the next digit after [44], which is 4 also
4 < 5, so rounding should not be applied. Edit: I mean it will stay the same [44], rest of number will be zeroed ofcourse
Your expectation seems to be similar to this recursive function that will result in 1.45 after a sequence of Math.Round.
double RecursiveMathRound(double val, int precisionLevel, int decimalPlace)
{
return (precisionLevel <= decimalPlace)
? Math.Round(val, precisionLevel, MidpointRounding.AwayFromZero)
: RecursiveMathRound(Math.Round(val, precisionLevel, MidpointRounding.AwayFromZero), precisionLevel - 1, decimalPlace);
}
RecursiveMathRound(1.444445d, 5, 2);
However, any form of rounding is less 'accurate' or 'correct' then a value that was not rounded at all. The RecursiveMathRound snippet above produces a less accurate or correct approximation because of the series of rounding occurrences.

mapping numbers on a scale [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 3 years ago.
Improve this question
I have an array of numbers up to 20000 and I'm trying to assign a weight to these numbers:
The closer a number is to 0 the higher should the weight be. My problem is that I'm trying to make it such that the higher the number is, the smaller should the difference in weight be, for example the weight difference between 1-100 might be 1.5 but the difference between 100-10000 might be 0.5.
I think it's a logarithmic scale, isn't it? I'm not great at math at all.. this is not a homework question, school was out long ago just a hobby question.
What I've tried is that I've mapped weights to my number array by doing a square root on 25000-value but this isn't what I'm looking for. I just put that in so I could see a gradient of weights coming back plus the numbers are just to big, ideally I want the weights between 0.01 and 3.
I don't have any code to show, any help would be appreciated.
While your question isn't really a C# question, I may have an answer for you.
To scale a value with logarithmic spacing, you can use the following formula:
You said you maximum value is 20000 and you want to scale the values from 0.01 to a maximum of 3, so we need to insert the max and scale our formula:
// edit: also the values should be reversed, so subtract the log from 1:
This gives the following values f(x) for values of x:
f(0) = 3
f(1) = 2.79
f(10) = 2.27
f(100) = 1.60
f(1000) = 0.91
f(10000) = 0.21
f(20000) = 0
Would that suffice for your case?

Fastest Algorithm for Shortest Paths with negative values? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 4 years ago.
Improve this question
I'm currently using Bellman Ford algorithm to find the shortest paths with negative value. Is there any faster algorithm that would outperform Bellman Ford for finding shortest paths with negative values?
A simple improvement is to only check for "active" nodes instead of iterating on all of them as the naive implementation does.
The reason is that if a node didn't lead to improvements on any of its neighbors and didn't change value in last iteration there is no need to redo the computation again (it will still produce no improvements).
Pseudocode (Python, actually):
A = set([seed])
steps = 0
while len(A) > 0 and steps < number_of_nodes:
steps += 1
NA = set()
for node in A:
for nh in neighbours(node):
x = solution[node] + weight(node, nh)
if x < solution[nh]:
# We found an improvement...
solution[nh] = x
pred[nh] = node
NA.add(nh)
A = NA
A is the "active" node set, where an improvement was found on last step and NA is the "next-active" node set that will need to be checked for improvements on next iteration.
Initially the solution is set to +Infinity for all nodes except the seed where the solution is 0. Initially only the seed is in the "active" set.
Note that in case of negative-sum loops reachable from the seed the problem has no "minimum path" because you can get the total as low as you want by simply looping; this is the reason for the limit on the "steps" value.
If when coming out of the loop A is not empty then there is no solution to the minimum cost problem (there is a negative-sum loop and you can lower the cost by simply looping).

Algo to check if a web page content changed significantly [duplicate]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 3 years ago.
Improve this question
I've been researching on finding an efficient solution to this. I've looked into diffing engines (google's diff-match-patch, python's diff) and some some longest common chain algorithms.
I was hoping on getting you guys suggestions on how to solve this issue. Any algorithm or library in particular you would like to recommend?
I don't know what "longest common [[chain? substring?]]" has to do with "percent difference", especially after seeing in a comment that you expect a very small % difference between two strings that differ by one character in the middle (so their longest common substring is about one half of the strings' length).
Ignoring the "longest common" strangeness, and defining "percent difference" as the edit distance between the strings divided by the max length (times 100 of course;-), what about:
def levenshtein_distance(first, second):
"""Find the Levenshtein distance between two strings."""
if len(first) > len(second):
first, second = second, first
if len(second) == 0:
return len(first)
first_length = len(first) + 1
second_length = len(second) + 1
distance_matrix = [[0] * second_length for x in range(first_length)]
for i in range(first_length):
distance_matrix[i][0] = i
for j in range(second_length):
distance_matrix[0][j]=j
for i in xrange(1, first_length):
for j in range(1, second_length):
deletion = distance_matrix[i-1][j] + 1
insertion = distance_matrix[i][j-1] + 1
substitution = distance_matrix[i-1][j-1]
if first[i-1] != second[j-1]:
substitution += 1
distance_matrix[i][j] = min(insertion, deletion, substitution)
return distance_matrix[first_length-1][second_length-1]
def percent_diff(first, second):
return 100*levenshtein_distance(a, b) / float(max(len(a), len(b)))
a = "the quick brown fox"
b = "the quick vrown fox"
print '%.2f' % percent_diff(a, b)
The Levenshtein function is from Stavros' blog. The result in this case would be 5.26 (percent difference).
In addition to difflib and other common subsequence libraries, if it's natural language text, you might look into stemming, which normalizes words to their root form. You can find several implementations in the Natural Language Toolkit ( http://www.nltk.org/ ) library. You can also compare blobs of natural language text more semantically by using N-Grams ( http://en.wikipedia.org/wiki/N-gram ).
Longest common chain? Perhaps this will help then: http://en.wikipedia.org/wiki/Longest_common_subsequence_problem
Another area of interest might be the Levenshtein distance described here.

Adding two different digit Numbers in c# ( without using BigInteger) [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
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.
Closed 9 years ago.
Improve this question
I have a Task to do C#. I need to add two numbers.
The first number contains around 100 digits like "12822429847264872649624264924626466826446692............"
and second number also with 100 digits or more or less
by using this numbers i need task like add/sub/multiply/div
I done this using BigInteger in C#
But do I need to do this using arrays or strings?
Since they are both 100 digits just start with the last digit and in a for loop just add each one, but if the value is > 10 then remember to add one to the next digit.
This is how children learn to add, you just need to follow the same steps, but the answer should be in an array of 101 characters.
UPDATE:
Since you have shown some code now, it helps.
First, don't duplicate the code based on if str1 or str2 is larger, but make a function with that logic and pass in the larger one as the first parameter.
Determine the largest size and make certain the smaller value is also the same size, to make math easier.
The smaller one should have leading zeroes (padding), again to help keep the code simple.
You can also start by looking at the source code for structures such as BigInteger. They would provide you more insight into aspects such as computational efficiency and storage, particularly about multiplication and division. You can take a look at here or here.

Categories

Resources