Skip to content

Instantly share code, notes, and snippets.

@andreluizf
Last active August 29, 2015 14:05
Show Gist options
  • Save andreluizf/25bae324eb81d58269fe to your computer and use it in GitHub Desktop.
Save andreluizf/25bae324eb81d58269fe to your computer and use it in GitHub Desktop.
recursividade
public class Teste {
private static void printMultiplos(int n, int x, int y) {
if (n >= x && n <= y) {
System.out.println(n);
printMultiplos(n + 3, x, y);
} else if (n <= y) {
printMultiplos(n + 3, x, y);
}
}
static int i = 0;
public static void verificaString(String texto, String p, int x) {
int y=0;
if (( y = texto.indexOf(p,x)) != -1) {
int n=y+p.length();
i++;
verificaString(texto, p, n);
}
}
public static int fatoral(int num) {
if (num == 0) {
return 1;
}
return num * fatoral(num - 1);
}
public static void primo() {
Scanner in = new Scanner(System.in);
System.out.print("Digite o numero: ");
int n, cont;
cont = 0;
n = in.nextInt();
for (int i = 1; i <= n; i++) {
if (n % i == 0) {
cont = cont + 1;
}
}
if (cont == 2) {
System.out.print("o numero " + n + " é primo");
} else {
System.out.print("o numero " + n + " não é primo");
}
}
static boolean isPrimo;
public static int primoRecursivo(int n, int j, int i) {
if (j > n / 2 || i > 1) {
if (i == 1) {
return 1;
} else {
return 0;
}
} else {
if (n % j == 0) {
i = j;
}
}
return primoRecursivo(n, ++j, i);
}
public static void main(String[] args) {
if (primoRecursivo(23, 1,0) == 1) {
System.out.println("é primo");
} else {
System.out.println("n é primo");
}
verificaString("aananaa", "n",0);
System.out.println("i " + i);
// System.out.println("fatoral é:: " + fatoral(6));
// printMultiplos(3, 4, 1000);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment