algorithms an pseudocode first year IT - algorithm

I am faced with a question that goes like this:
Write pseudocode that allows a user to repeatedly enter positive
integers until an odd number is entered. It would then print the sum
of all numbers entered (excluding the odd number).
Example: given that the user enters 2 24 16 8 7 the program would print 50.
I would like to get some feedback on my algorithm for this problem.
1. Start
2. Declare int number,n,sum=0
3. Do
4. Input number
5. Read number
6. n=number%2
7. If (n==0) then sum+=number
8. while (n==0)
9. If(n==1) then display number and print sum
10. Endif
11. Endwhile
11. Stop

Bugs
You need to move a copy of line 5 (n=number%2) inside of the loop - right now you've got an infinite loop if n == 0 because n is not modified inside of the loop
Possible Bugs
Some languages will return a negative value on the modulo operation if the dividend or divisor is negative, so you may want to take the absolute value of the remainder (n=abs(number%2))
Formatting / Syntax
This is a case where you should use a do while loop - this will let you eliminate lines 3 through 5
Indent the stuff inside the loop

I think you are using do while loop and i made some changes to your algorithm. i think it solves your problem.
1. Start
2. Declare int number,n,sum=0
3. Do
4. Input number
5. Read number
6. n=number%2
7. If (n==0) then sum=sum+number
8. while (n==0)
9. print sum
10.Stop

number+sum should be sum+=numberbecause you need to add the value of the number to the sum, the number+sum isn't assigned anywhere.

You need to add below two inside the loop
6. n=number%2
7. If (n==0) then sum+=number
or else it will be an infinite loop or until some one enters an odd number.

Related

How to use the Pisano Period to find the Last Digit of the Sum of Fibonacci Numbers?

I'm taking an online algorithms course, and I've come across a problem where I need to find the last digit of the sum of the Fibonacci numbers up the nth (current) number.
I need some help connecting the dots. As I understand it, the "Last Digit of the Sum of Fibonacci Numbers" problem has a solution that is somehow related to the Pisano Period.
But I don't really understand what that means.
The Pisano Period was used to calculate the remainder given some value of m for an extremely large Fibonacci Number, which was the focus of a prior problem (I.E., Solve Fn mod m = ???).
Forum posts (and the instruction set) seem to suggest that the length can somehow help us quickly zero in on the sum for the current Fibonacci without having to actually build up to it normally through a loop.
I would rather avoid just looking at someone else's solution if possible, so if anyone has any useful hints that can help me see the missing link, I would really appreciate it.
The last digit of a Fibonacci number is just that number reduced modulo 10. Pisaso periods are the periods of which the sequence of fibonacci numbers, modulo some base, repeat. So, if you're interested in F(x) mod 10, you'd interested in the Pisaso Period p(10).
If we have this period, say it was something like [1, 5, 2, 7, 0] (its not, but for sake of example), we'd know that the 3rd integer in the sequence ended with a "2". And because it repeats, we'd know the 8th integer also ends in a "2", and the 13th...
Generalizing this, we could say that the last digit of the number N could be found at the ith index in our list we just built, for i satisfying N = 5 * k + i, where k is just any integer, and 5 comes from the fact that our list has 5 elements (and thus repeats every 5 values). Rewriting this, we could say i = N mod 5.
Putting that all together (spoilers), we just need to find the actual values of the repeating sequence mod 10, and then take our input value N (for finding the Nth Fibonacci number mod 10), and index into said repeatingSequence at index N mod len(repeatingSequence) for our answer.
For reference, for base 10, the actual repeating sequence is:
011235831459437077415617853819099875279651673033695493257291

Does Ruby already know the Fibonacci Sequence?

The following method is used to find the nth number in the Fibonacci Sequence:
def fibonacci(number)
if number < 2
number
else
fibonacci(number - 1) + fibonacci(number - 2)
end
end
puts fibonacci(6)
The answer is 8 (the sixth number in the sequence if you start counting from 1 and not 0 (0, 1, 1, 2, 3, 5, 8...)
The above code simply deals with the position of numbers in the sequence, right? So how does Ruby already know what the value of a number is at a given position? In the words, how does Ruby know that fibonacci(4) has the value 3 ? Is the Fibonacci sequence already built in to Ruby?
No, the sequence is not built in.
fibonacci is a recursive function. It will be called nine times (!) to compute fibonacci(4) to get 3. (By the way, this is a terrible approach to calculating Fibonacci numbers. 25 calls for fibonacci(6)! It should at least memoize previous calculations.)
So how does Ruby already know what the value of a number is at a given position?
It doesn't already know. It finds out, as part of this method. To find out the value at this position, it first calculates all the values of all the numbers at all the previous positions.
how does Ruby know that fibonacci(4) has the value 3
Because the first thing it does in order to calculate fibonacci(4) is to call fibonacci(3). The algorithm is recursive.

Hashing Elements by Mid-Square

I'm making a hash function for a hash table of size 10 (indexes 0-9), and hashing elements using mid-square method.
The Problem is I'm confused whether i should use 1 middle digit or 2 digits then taking mod 10 of it.
The problem is if I choose to take 2 mid digit then taking mod 10, this method will fail if the squared number has 3 digits. Which two digits will I take then?
and if I choose to have 1 middle digit, then I'll have problem taking mid digit when squared number has even number of digits. I know in this situation both of the mid two element are made from contribution of all elements of the original number.
Right now, I'm going with {floor(n/2+1)}th digit. This way it works for three digit squared number, and when the squared number has even no. of digits, it's taking the later digit of the middle two.
I wanna know what more efficient approach of doing this?
Thanks.
I was taught that the correct way to mid square hash is to use 1 digit if the length of the resulting square is odd, if it's even then use the middle 2 digits.

Can't make/configure the difficult function

While solving sudoku I can remove possibility digits (1) and (2) from the cells D[1,2] and D[2,2]. Because (8) and (9) are possible only in those cells, so those cells are either (8 and 9) or (9 and 8). This means that digits (1) and (2) are at the 3rd line of the D block. Thats why I can eliminate the possibility of the digit (1) from the cell A[3,3].
I have been configuring a function to do this during last 40 hours, but couldn't manage. Is there anyone who can make the function to detect this type of intellectual issue (eliminating some possibilities because some other n count of possibilities can exist only in n count of cells, in our case 2 digits 8 and 9 can exist in 2 cells D[1,2] and D[2,2]).
Please dont advice me about other functions of sudoku; I have already done them, the only algorithm that I couldn't program is this one. Btw you can use r[i] (string which consists the possibilities for the row number i), c[i] for the column, and b[i] for the blocks (ex: b[4] (in this image block A) = 1,2,3,4,5,6,7 because 8 and 9 are already defined). Thanks
I really don't see the problem, you basically already answered your problem.
In general you should do the following:
Step 1: Loop over all 9 cells of one block and check if (1) is contained only in two cells.
Step 2: If not, try the next number. If yes, Loop over all 9 cells and check if (2) is also in those two cells but not in any other of the remaining 7.
Step 3: If not, check the next number. If yes, remove the other possibilities of the two cells except for the two numbers you found and you are basically done.
Step 4: If no matching number could be found for (1) (or any larger number that was chosen in the "not" part of step 2), start over from step 1 but trying the next number, unless you are already at 8, then you can stop.
In the end you could dynamically extend the same pattern for 3 numbers in 3 cells, 4 numbers...

Find number of ones in from 0 to n recursively

I have to make a recursive function that finds the number of ones from 0 to n recursively.
So f(16) = 9
(1,10,11,12,13,14,15,16)
This is obviously homework so I would appreciated if you did NOT post any code, just the reasoning behind it.
What I've reasoned so far is that if you do %10 of a number it will tell you if the least significant is a one, also if you do an integer division by 10 you lose that digit.
So I'm guessing the approach will be can be checking if number%10 == 1 and then calling the function with f(n/10), but then I get lost in the actual implementation.
I would appreciate if you could comment what approach would you use, it has to be recursive just because it's home work, the procedural approach was trivial.
For these types of problems, I find it helps to write some sort of diagram showing the patterns. For instance, if you count by tens, you know that the first set (0-9) contains one 1.
(0-9) -- 1
(10-19) -- 11
(21-29) -- 1
| | -- 1
(100-109) -- 11
(110-119) -- 21
(120-129) -- 11
| | -- 11
(200-209) -- 1
(210-219) -- 11
(220-229) -- 1
| | -- 1
...
(1000-1009) etc...
It may take a while, but this will help you find patterns so you can come up with a more sytematic answer. I don't want to give you too much help since it's a homework problem, but that's the approach I take when I'm solving creative math problems.
You've got two parts to your problem.
Find numbers of ones in number.
Find numbers of ones in all the numbers less than it (but more than zero).
Part one first:
If the right-most digit is 1, then number % 10 == 1.
If the number is > 9 you need to check other digits, you can do this by doing the same test on the number after integer-divide 10. If the number <= 9, then that would give you zero.
So your OnesInNumber function is a bit like:
If number == 0, return 0.
Otherwise call OnesInNumber on number / 10.
If number % 10 == 1 add 1 to that result.
Return the result.
This will for example, give you 1 when called on 10, 1, 12, 303212, give you 2 when called on 11, and so on.
Your OnesInZeroUntil function is then like:
If number <= 0, return 0.
Otherwise call OnesInZeroUntil on number - 1.
Add OnesInNumber(number) to this.
Return the result.
So you've a recursive function that works out the number of 1 in a number, and another recursive function that works out the number of 1 in every number up to that one, building on that first function.
That'd be enough to write a quick 2 functions in, had you not requested that we don't.
(Tip: If your teacher isn't already requiring it, see if you can work out how to do this without recursion. Every recursive function can be re-written as a non-recursive form, and it's a practical skill to be able to do that some people teaching recursion don't seem to cover).
You are quite right in your approach. For every number you want a function that will return the "number of ones" in the decimal representation. A recursive representation of this (note you could also do this iteratively).
Like all recursive functions, you need your end-state catch, i.e., if the input = 0 return 0. Besides that (without giving it all away) you just need to add your current result to the sub-result:
if number==0
return 0
if number%10==1
return myFunc(number/10) + 1
else
return myFunc(number/10)
However, as I said before, there is no need to use recursion. An iterative solution is probably better here since the function is linear with respect to the number of digits.

Resources