Hello! Tell me how to create an algorithm for the task where to start?
Create a program that displays the first 12 elements of the sequence.
2a n + 1 = 2a n -2–2, where a 1 = 3 and a 2 = 2.
UPD
without using an array
Take three variables: A1, A2, A3.
It is necessary to begin, obviously, with a cycle.
int a = 3; for (int number = 1; number <= 12; number++) { a = (2*a-4); System.out.println(a); }
Whit. Syntax Java. I apologize if I give an error: I did not miss it at all. =) But I think you only need a principle? ;)
UPD will output 12 elements, but up to the thirteenth (a2-a13) since a1 is already set.
for (int number = 1; number < 12; number++) {
So - 12
Sending a class that solves this problem using a recursive formula:
public class Test { private static int f(int x) { if (x == 1) { return 3; } else if (x == 2) { return 2; } else { return 2 * f(x - 2) - 2; } } public static void main(String[] args) { for (int i = 1; i <= 12; i++) { System.out.println(i + " : " + f(i)); } } }
I hope that the solution will suit you.
Sincerely, Eugene.
int a1=3, a2=2, a3=0; for (int i=1; i<=12; i++) { a3 = 2*a1-2; System.out.print(a3 + " "); a1=a2; a2=a3; }
Source: https://ru.stackoverflow.com/questions/36950/
All Articles