English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

How would I develop an 1-variable algorithm whose output is the sequence "even, even, odd, odd, ...". The magnitude is unimportant, only the even/odd of the number.

2006-09-23 09:54:17 · 4 answers · asked by timmy_n 1 in Science & Mathematics Mathematics

Sorry, let me elaborate more as I was in a hurry when I first put the question up. You are given a variable N, such that N = 1,2,3,4,...
Now, we will be taking -1^f(N) and we intend to get the following output:
1,1,-1,-1,1,1,-1,-1,...

What is f(N)?

2006-09-23 14:11:51 · update #1

4 answers

If I understand correctly you search an algorithm A(x)
that will convert the sequence 1,2,3,4,5,6,..
into even, even, odd, odd, even, even,...

you can use use

A(x)=truncate(x/2) where truncate means take the integer part and drop the decimal part

Example:
A(0)=Truncate(0/2)=0 (even)
A(1)=Truncate(0.5)=0 (even)
A(2)=Truncate(1)=1 (odd)
A(3)=Truncate(3/2)=1 (odd)
A(4)=Truncate(4/2)=2 (even)
A(5)=Truncate(5/2)=2 (even)
A(6)=Truncate(6/2)=3 (odd)
...

2006-09-23 10:05:18 · answer #1 · answered by cd4017 4 · 0 0

How to make sure that you get the right sequence by just input 1 variable. How to develop, you need to understand what is even, and what is odd.

Even is an integer that is divisible by 2.
Odd is an integer that is NOT divisible by 2.

Let x be any digit, but unknown of its value. To be sure that the input of x MUST become an even number. You can always multiply that x by 2 and become 2x. :)

To make sure the input x must be indivisible by 2, you can multiply x by 2 and plus 1 and become 2x+1.

To general that algorithm, we use the above strategy.

Algorithm: Funny sequence
Input : X
Output: even, even, odd, odd, even, even, odd, odd....

Step 1: Output 2*x
Step 2: Output 2*X
Step 3: Output 2*X + 1
Step 4: Output 2*X + 1
Step 5: Repeat 1-4 Until STOP

Here is your algorithm :) Cheers, and have fun

2006-09-23 17:38:04 · answer #2 · answered by davidkwankwokfai 3 · 0 0

f(x) {
if x mod 4 = 1 or x mod 4 = 2, return "even"
else return "odd"
}

2006-09-23 16:58:51 · answer #3 · answered by Pascal 7 · 0 0

f(x) {
return int((x-1)/2) mod 2;
}
if f(x) return 0 it mean even, if 1, it means odd.

2006-09-23 17:04:10 · answer #4 · answered by sebourban 4 · 0 0

fedest.com, questions and answers