Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.AbstractMap.SimpleEntry;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.Scanner;
- public class PuntoUno {
- static int maxN = (int) 1e7;
- static boolean cribe[] = new boolean[maxN+1];
- static Scanner scanner = new Scanner(System.in);
- static Float actualAvg = 0.0f;
- static long fastExp(long b, long e)
- {
- int s = 63;
- long r = b;
- while(((e >> s--) & 1) == 0);
- while(s >= 0)
- {
- r *=r;
- if(((e >> s--)&1) == 1) r = r*b;
- }
- return r;
- }
- static long fastExp(long b, long e, long m) {
- int s =63;
- long r = b;
- while(((e >> s--) & 1) == 0);
- while(s >= 0)
- {
- r = (r*r)% m;
- if(((e >> s--)&1) == 1) r = (r*b)%m;
- }
- return r;
- }
- static boolean isPrimeProb(long n, int a)
- {
- if(n == a) return true;
- long s = 0, d = n-1;
- while((d%2) == 0)
- {
- s++;
- d/=2;
- }
- long x = fastExp(a, d, n);
- if((x== 1) || (x+1 == n)) return true;
- for(int i = 0; i < s-1; i++)
- {
- x = fastExp(x, 2, n);
- if(x == 1) return false;
- if(x+1 == n) return true;
- }
- return false;
- }
- static boolean rabin(long n)
- {
- if(n == 1) return false;
- int tests[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31};
- for(int t : tests)
- {
- if ( t == n ) return true;
- if(!isPrimeProb(n, t)) return false;
- }
- return true;
- }
- static long forceReadPositiveLong(String message, int op)
- {
- long n = 0;
- while(true)
- {
- try
- {
- System.out.println(message);
- n = Long.parseLong(scanner.next());
- if(op == 0) // Se carga el array de múltiplos de mil
- {
- if(n!= 0 && n%1000 == 0) return n;
- System.out.println("N debe ser múltiplo de 1000");
- continue;
- }
- else if(op == 1) // Se carga el array de primos.
- {
- if(n > 1 && ((n<= maxN && cribe[(int) n]) || (n> maxN && rabin(n)))) return n;
- System.out.println("El número debe ser un número primo.");
- continue;
- }
- else
- {
- if(n > 0 ) return n;
- System.out.println("El tamaño de los arreglos a cargar debe ser positivo.");
- continue;
- }
- }
- catch(Exception ex)
- {
- System.out.println("Se introdujo un número inválido, reintente.");
- }
- }
- }
- /* Para números menores a maxN = 10^7 O(n log n)*/
- static void initCribe()
- {
- Arrays.fill(cribe, true);
- cribe[0] = (cribe[1] = false);
- for(int i = 2; i*i <= maxN; i++)
- {
- if(cribe[i])
- {
- for(int j = i*i; j < maxN; j+=i)
- {
- cribe[j] = false;
- }
- }
- }
- }
- static ArrayList<Long> sumArrays(ArrayList<Long> a, ArrayList<Long> b)
- {
- ArrayList<Long> ret = new ArrayList<Long>();
- int n = a.size();
- for(int i = 0; i < n ; i++)
- {
- ret.add(a.get(i)+b.get(i));
- }
- return ret;
- }
- static ArrayList<Long> chargeArray(int op, int max)
- {
- ArrayList<Long> retArray = new ArrayList<Long>();
- String msg = (op == 0) ? "Introduzca un número múltiplo de 1000:" : "Introduzca un numero primo:";
- Float sum = (float) 0;
- while(max-- > 0 )
- {
- retArray.add(forceReadPositiveLong(msg, op));
- sum+=retArray.get(retArray.size()-1);
- }
- sum/= retArray.size();
- actualAvg = sum;
- return retArray;
- }
- static void showArrayList(ArrayList<Long> a, float avg, float avgPrime)
- {
- ArrayList<Long> arraySumLess = new ArrayList<Long>();
- ArrayList<Long> arraySumHigher = new ArrayList<Long>();
- for(int i = 0; i < a.size(); i++)
- {
- System.out.println(a.get(i));
- if(a.get(i) < avg) arraySumLess.add(a.get(i));
- if(a.get(i) > avgPrime) arraySumHigher.add(a.get(i));
- }
- System.out.println("Media del primer arreglo: " + avg);
- System.out.println("Media del segundo arreglo: " + avgPrime);
- System.out.print("Elementos menores de la media del primer arreglo: [");
- for(int i = 0; i < arraySumLess.size(); i++)
- {
- System.out.print(arraySumLess.get(i) + ((i == arraySumLess.size()-1) ? "]":", "));
- }
- System.out.print("\nElementos mayores de la media del segundo arreglo: [");
- for(int i = 0; i < arraySumHigher.size(); i++)
- {
- System.out.print(arraySumHigher.get(i) + ((i == arraySumHigher.size()-1) ? "]":", "));
- }
- }
- public static void main(String[] args) {
- initCribe();
- int max = (int) forceReadPositiveLong("Introduzca el tamaño del arreglo:",2);
- ArrayList<Long> divs1000Array = chargeArray(0, max);
- Float avg1000 = actualAvg;
- ArrayList<Long> primeArray = chargeArray(1, max);
- Float avgPrime = actualAvg;
- showArrayList(sumArrays(divs1000Array, primeArray), avg1000, avgPrime);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement