Advertisement
Talilo

olgoritmo_Busca.js

Mar 28th, 2023 (edited)
642
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //https://pt.khanacademy.org/computing/computer-science
  2.  
  3. function buscar(valor){
  4.    
  5. var primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97];
  6.  
  7. var numero;
  8. numero = valor;
  9.  
  10. for ( i=0; i <= primes.length; i++){
  11.     if( primes[i] == numero){
  12.         console.log("Tem na lista!");
  13.     }else{
  14.         console.log("Nao tem na Lista!");
  15.     }
  16.   }
  17. }
  18. buscar(20)
  19.  
  20.  
  21. // Outra  maneira de fazer busca linear:
  22.  
  23. function buscar(valor){
  24.    
  25. var primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97];
  26. //console.log(primes[1] )
  27. var numero;
  28. numero = valor;
  29.  
  30. for ( i=0; i <= primes.length; i++){
  31.     if( primes[i] == numero){
  32.         console.log(" Tem o numero " + primes[i] + "na Lista!");
  33.   }
  34.  }
  35. }
  36. buscar(23)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement