Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package primeIteration;
- import java.io.FileNotFoundException;
- import java.io.PrintWriter;
- import java.io.UnsupportedEncodingException;
- import java.util.ArrayList;
- public class PrimeFunctionIterate {
- public static void main(String[] args) {
- PrintWriter writ = null;
- try {
- writ = new PrintWriter("nums.txt", "UTF-8");
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (UnsupportedEncodingException e) {
- e.printStackTrace();
- }
- for(int k=2; k < 10001; ++k){
- int num2 = 0;
- for(int i=1; i < k; ++i){
- int end = i;
- while(!(end < 10 && (isPrime(end) || end ==1))){
- end = function(end);
- }
- if(end ==2) ++ num2;
- }
- double dub = (double)num2/(double)k;
- writ.println(dub);
- }
- }
- public static boolean isPrime(int n){
- for(int i=2; i < n/2+1; ++i){
- if(n % i == 0){
- return false;
- }
- }
- return true;
- }
- public static int function(int n){
- if(isPrime(n)){
- return sumDigits(n);
- }
- else return sumFactors(n);
- }
- public static int sumDigits(int n){
- String num = Integer.toString(n);
- int sum = 0;
- for(int i=0; i < num.length(); ++i){
- sum += Integer.parseInt(num.substring(i, i+1));
- }
- return sum;
- }
- public static ArrayList<Integer> factorList(int n){
- ArrayList<Integer> factors = new ArrayList<Integer>();
- if(isPrime(n)){
- ArrayList<Integer> single = new ArrayList<Integer>();
- single.add(n);
- return single;
- }
- for(int i=2; i < n/2; ++i){
- if(n % i == 0){
- factors.add(i);
- factors.addAll(factorList(n/i));
- break;
- }
- }
- return factors;
- }
- public static int sumFactors(int n){
- int sum = 0;
- ArrayList<Integer> factors = factorList(n);
- for(int i=0; i<factors.size(); ++i){
- sum += factors.get(i);
- }
- return sum;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement