Project Euler #179: Consecutive positive divisors
Project Euler #179: Consecutive positive divisors
+ 0 comments Can anybody tell me what I'm missing?
# Enter your code here. Read input from STDIN. Print output to STDOUT t = int(input().rstrip()) nums = [0]*(10**6) col = [0]*(10**6) max_nums = 1 nums[0] = 1 def enlarge_max(next_max): global max_nums global nums global col #nums[0] = 1 #print("enlarging from {} to {}".format(max_nums, next_max)) for i in range(1, next_max+1): ss = int(max_nums / i) * i #if ss == max_nums: # nums[ss-1] += 1 ss += i #print("... i={} starting ss={}".format(i, ss)) while ss <= next_max: #print(" cur ss={}".format(ss)) nums[ss-1] += 1 ss += i #print("cur nums={}".format(nums[0:next_max])) #print("new max = {}".format(next_max)) prev_col = col[max_nums-1] #print(" prev max = {} prev_col = {}".format(max_nums, prev_col)) for i in range(max_nums, next_max): #col[i-1] = prev_col if nums[i] == nums[i+1]: #print(" found equality at i={} {}={}".format(i, nums[i], nums[i+1])) prev_col += 1 col[i] = prev_col #print(" arr equality={}".format(col[0:next_max])) max_nums = next_max for _ in range(0, t): k = int(input().rstrip()) enlarge_max(k) print("{}".format(col[k-1]))
+ 0 comments What is actully n? explaination is too short to understand the output. The only n < 15 are 2 and 14.
+ 0 comments Can anyone tell me what am I missing?? I am able to pass test case 0 not rest of them . Please help thank you
con <- file("stdin", open = "r") data <- as.numeric(readLines(con)) for (i in 1:data[1]){ I <- data[i+1] n <- array(0,dim = I) for (i in 1:floor((I-1)/2)){ for (j in seq(i*2,I, by = i)){ n[j]= n[j]+1 } } b <- 0 for(i in 2:I){ if((n[i]==n[i-1])==TRUE){ b <- b+1 } } cat(b,sep = "\n") }
+ 2 comments what the question mean i cant understand please anyone can explain the question
+ 0 comments why my code is giving timeout canbody give a hint
include
using namespace std;
const int MAX = 10000002;
// array to store prime factors int factor[MAX] = { 0 };
// function to generate all prime factors // of numbers from 1 to 10^7 void generatePrimeFactors() { factor[1] = 1;
// Initializes all the positions with their value. for (int i = 2; i < MAX; i++) factor[i] = i; // Initializes all multiples of 2 with 2 for (int i = 4; i < MAX; i += 2) factor[i] = 2; // A modified version of Sieve of Eratosthenes to // store the smallest prime factor that divides // every number. for (int i = 3; i * i < MAX; i++) { // check if it has no prime factor. if (factor[i] == i) { // Initializes of j starting from i*i for (int j = i * i; j < MAX; j += i) { // if it has no prime factor before, then // stores the smallest prime divisor if (factor[j] == j) factor[j] = i; } } }
}
// function to calculate number of factors int calculateNoOFactors(int n) { if (n == 1) return 1;
int ans = 1; // stores the smallest prime number // that divides n int dup = factor[n]; // stores the count of number of times // a prime number divides n. int c = 1; // reduces to the next number after prime // factorization of n int j = n / factor[n]; // false when prime factorization is done while (j != 1) { // if the same prime number is dividing n, // then we increase the count if (factor[j] == dup) c += 1; /* if its a new prime factor that is factorizing n, then we again set c=1 and change dup to the new prime factor, and apply the formula explained above. */ else { dup = factor[j]; ans = ans * (c + 1); c = 1; } // prime factorizes a number j = j / factor[j]; } // for the last prime factor ans = ans * (c + 1); return ans;
}
// Driver program to test above function int main() { // generate prime factors of number // upto 10^7 generatePrimeFactors(); vectora(10000002); for (int i = 1; i < 10000002; i++) a[i]= calculateNoOFactors(i); vectorcount(10000001); for(int i=1;i<=10000000;i++) { if(a[i]==a[i+1]) { count[i]=count[i-1]+1; } else { count[i]=count[i-1]; }
} int t; scanf("%d",&t); while(t--) { int k; cin>>k; printf("%d\n",count[k-1]); } return 0;
}
- List Item
Sort 60 Discussions, By:
Please Login in order to post a comment