Created
July 6, 2021 05:07
-
-
Save ashraf267/4130fcd499ef7d650df5c3e3c0fcbbf1 to your computer and use it in GitHub Desktop.
A dynamic pattern program
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:math'; | |
void main() { | |
var ans = demo(5, 2, 4, 6); //expected ans is 10 | |
// expected ans is 81, uncomment to test | |
// var ans demo(4, 3, 9, 27); | |
print(ans.runtimeType); // for debugging | |
print(ans); | |
} | |
int? demo(int x, int y1, int y2, int y3) { | |
var y; | |
int a = y1; // first term | |
// checks if sequence is AP or GP | |
if (y2 - y1 == y3 - y2) { | |
// AP | |
// d; common difference | |
int d = y2 - y1; | |
// Tn = a + (n - 1) * d | |
y = a + (x - 1) * d; | |
} else if (y2 - y1 != y3 - y2) { | |
// GP | |
// r; common ratio | |
var r = y2 / y1; | |
// Tn = a * pow(r, n - 1) | |
x = x - 1; | |
y = a * pow(r, x).toInt(); // converts num to int | |
} else { | |
// no relationship | |
} | |
return y; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment