SIGSEGV "3n + 1" - c++

100 - The 3n + 1 problem
http://www.spoj.com/problems/PROBTRES/
always i get this >>> runtime error (SIGSEGV) <<<
why plz help !
Background:
Problems in Computer Science are often classified as belonging to a certain class of problems (e.g., NP, Unsolvable, Recursive). In this problem you will be analyzing a property of an algorithm whose classification is not known for all possible inputs.
The Problem:
Consider the following algorithm:
1. input n
2. print n
3. if n = 1 then STOP
4. if n is odd then n = 3n + 1
5. else n = n / 2
6. GOTO 2
Given the input 22, the following sequence of numbers will be printed 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
It is conjectured that the algorithm above will terminate (when a 1 is printed) for any integral input value. Despite the simplicity of the algorithm, it is unknown whether this conjecture is true. It has been verified, however, for all integers n such that 0 < n < 1,000,000 (and, in fact, for many more numbers than this.)
Given an input n, it is possible to determine the number of numbers printed (including the 1). For a given n this is called the cycle-length of n. In the example above, the cycle length of 22 is 16.
For any two numbers i and j you are to determine the maximum cycle length over all numbers between i and j.
The Input:
The input will consist of a series of pairs of integers i and j, one pair of integers per line. All integers will be less than 1,000,000 and greater than 0.
You should process all pairs of integers and for each pair determine the maximum cycle length over all integers between and including i and j.
You can assume that no operation overflows a 32-bit integer.
The Output:
For each pair of input integers i and j you should output i, j, and the maximum cycle length for integers between and including i and j. These three numbers should be separated by at least one space with all three numbers on one line and with one line of output for each line of input. The integers i and j must appear in the output in the same order in which they appeared in the input and should be followed by the maximum cycle length (on the same line).
Sample Input:
1 10
100 200
201 210
900 1000
Sample Output:
1 10 20
100 200 125
201 210 89
900 1000 174
#include <iostream>
using namespace std ;
long int a[1000001];
long int F (long int n){
if(a[n]!=0)
return a[n];
else {
if(n%2 !=0)
a[n]=F(n*3+1)+1 ;
else
a[n]=F(n/2)+1 ;
return a[n];
}
}
int main(){
a[1]= 1 ;
long int i , j , MX , MN , x=0 ;
while (cin>>i >> j ){
MX=max(i,j);
MN=min(i,j);
for(;MN<=MX;MN++){
if(x<F(MN))
x=F(MN) ;
}
cout<<i<<" "<<j<<" "<<x<<endl;
x= 0;
}
return 0 ;
}
what is the difference between this and my code ?!!!
#include <stdio.h>
#include <stdlib.h>
#define MAX 1000001
static int result[MAX];
int calculate(unsigned long i);
int main()
{
unsigned long int i = 0;
unsigned long int j = 0;
unsigned long int k = 0;
int max,x,y;
result[1] = result[0] = 1;
while (scanf("%ld",&i)!= EOF)
{
scanf("%ld",&j);
if (i > j)
{
x = i;
y = j;
}
else
{
x = j;
y = i;
}
max = 0;
for (k = y; k <= x; k++)
{
if (result[k] != 0 && result[k] > max)
max = result[k];
else if (calculate(k) > max)
max = result[k];
}
printf("%ld %\ld %d\n",i,j,max);
}
return 0;
}
int calculate(unsigned long i)
{
if (i < MAX && result[i])
return result[i];
if ( i % 2 == 1 )
{
if (i < MAX)
return result[i] = 2+calculate((3*i+1)/2);
else
return 2+calculate((3*i+1)/2);
}
else
{
if( i < MAX)
return result[i] = 1 + calculate(i / 2);
else
return 1 + calculate(i /2 );
}
}

You might check the actual range of values you're getting for n, as it might be stepping outside your array long a[1000001]. Also, you might check your recursion depth. If you recurse too deeply, you'll overflow the stack.
I would consider adding an assert to test n (ie. assert(n < 1000001)), and perhaps a recursion depth variable to check your recursion depth as the first steps to diagnosing and debugging this code. You can find assert in <cassert>.

Related

Whats the difference between this 2 flowing code with same objective

Question:
Starting with a 1-indexed array of size n filled with all zeroes, you are required to perform the following operation m times:
Each operation contains 3 integers a, b and k. You are required to add k to the value at all the indices from a to b (both inclusive).
Once all operations have been performed, return the maximum value in the array.
Input Format:
The first line contains two space-separated integers n and m, the size of the array and the number of operations respectively.
Each of the next m lines contains three space-separated integers a, b and k, the left index, right index and integer to add respectively.
Constraints:
1 <= n, m <= 10^5
1 <= a <= b <= n
-10^9 <= k <= 10^9
Examples:
Sample Input 1:
10 3
1 5 3
4 8 7
6 9 1
Sample Output 1:
10
Explanation:
Given n = 10 and m = 3
Queries are interpreted as follows:
a b k
1 5 3
4 8 7
6 9 1
Add the values of k between the indices a and b inclusive:
1 2 3 4 5 6 7 8 9 10 (index)
[0,0,0,0,0,0,0,0,0,0] (Initially)
[3,3,3,3,3,0,0,0,0,0] (After Query 1)
[3,3,3,10,10,7,7,7,0,0] (After Query 2)
[3,3,3,10,10,8,8,8,1,0] (After Query 3)
The largest value is 10 after all operations are performed.
Sample Input 2:
5 3
1 2 100
2 5 100
3 4 100
Sample Output:
200
Explanation:
After the first update the list is 100 100 0 0 0.
After the second update list is 100 200 100 100 100.
After the third update list is 100 200 200 200 100.
The maximum value is 200.
This code ran and gave the correct output:
#include <iostream>
#include <vector>
using namespace std;
int main() {
int n, m;
cin >> n >> m;
vector<long long int> arr(n, 0);
for (int i = 0; i < m; i++) {
int start, finish, value;
cin >> start >> finish >> value;
arr[start - 1] += value;
arr[finish] -= value;
}
long long int ans = 0;
long long int current = 0;
for (int value : arr) {
current += value;
if (current > ans) {
ans = current;
}
}
cout << ans;
return 0;
}
but this code is giving me segment fault:
#include <bits/stdc++.h>
using namespace std;
int main(){
long long int n,m;
cin>>n>>m;
long long int x[n] ={0};
for(int i=0;i<m;i++)
{
long long int a,b,k;
cin>>a>>b>>k;
x[a-1] += k;
if(b<n)
x[b] -= k;
}
long long int max=0,current=0;
for(int i=0;i<n;i++)
{
current=x[i]+current;
if(max<current)
max=current;
}
cout<<max<< endl;
return 0;
}
Well the problem in your code is in the line x[b] -= k;
Since b is 1-indexed, this means that b can be upto n but your array of size n has indices from 0 to n-1 and if there is a query where b = n, then you get segmentation fault.
You can correct this by placing an if condition just before this line like this:
if (b < n)
Also, in the first code, you should have encountered the same problem but somehow your compiler overlooked the indexing problem in vector.

process terminated with status -1073740940

background: I was writing a c++ program to solve this problem:
For a positive integer N, the digit-sum of N is defined as the sum of N itself and its digits. When M
is the digitsum of N, we call N a generator of M.For example, the digit-sum of 245 is 256 (= 245 + 2 + 4 + 5). Therefore, 245 is a generator of
256. Not surprisingly, some numbers do not have any generators and some numbers have more than one generator. For example, the generators of 216 are 198 and 207.
You are to write a program to find the smallest generator of the given integer.
Input
Your program is to read from standard input.
The input consists of T test cases.
The number of test cases T is given in the first line of the input.
Each test case takes one line containing an integer N, 1 ≤ N ≤ 100, 000.
Output
Your program is to write to standard output.
Print exactly one line for each test case.
The line is to contain a generator of N for each test case.
If N has multiple generators, print the smallest.
If N does not have any generators, print ‘0’.
my problem: the program below always terminated with status -1073740940, I wonder why and need some help
int main()
{
int* ans = new int[100005]();
int y;
int i_op;
for(int i = 1; i < 100001; ++i){
y = i;
i_op = i;
while(i_op){
y += i_op%10;
i_op /= 10;
}
if(ans[y] == 0 || i < ans[y]){
ans[y] = i;
}
}
int t;
int n;
cin >> t;
for(int i = 0; i < t; ++i){
cin >> n;
cout << ans[n] << endl;
}
//========================
//problem occurs here //after doing all output, the process terminated with status -1073740940
//========================
delete[] ans;
return 0;
}
input data: (both terminated with status -1073740940)
10
70587
38943
37061
95352
84205
96532
21150
26337
97804
65891
and
100000
1
2
……
100000
You may be writing past the end of your array during the computation and corrupting something. What happens for i = 99999? I don't think 100005 is quite enough to contain that. Let's check:
#include <stdio.h>
int main() {
int i = 99999;
int y = i;
int i_op = i;
while(i_op){
y += i_op%10;
i_op /= 10;
}
printf("%d\n", y);
}
Outputs 100044. Indeed.

Sequence of n numbers - compute all possible k-subsequence of "lucky" numbers

I have a problem with one task, so if you could help me a little bit.
Numbers are "lucky" or "unlucky". Number is "lucky" just if every
digit 7
or every digit is 4. So "lucky" numbers are for example 4, 44, 7, 77.
"Unlucky" are the others numbers.
You will get sequence of n-elements and number K. Your task is to
compute number of all possible k-elements subsequence, which fulfill a one
condition. The condition is that in the subsequence mustn't be two same "lucky"
numbers. So for example there mustn't 77 and 77...
Output number of all possible k-elements subsequence mod 10^9+7
0 < N,K < 10^5
Few examples:
Input:
5 2
7 7 3 7 77
Output:
7
Input:
5 3
3 7 77 7 77
Output:
4
Input:
34 17
14 14 14 ... 14 14 14
Output:
333606206
I have code which seems to work, but it is too slow when I try to compute binomial coefficient. I'm using map. In string I store number in string format. In second - int - part of the map is number which represents how many times was that number(in the first map parameter) used. So now I have stored every "unlucky" numbers stored together. Also every same "lucky" number is together. When I have it stored like this, I just compute all multiplications. For example:
Input
5 2
3 7 7 77 7
Are stored like this: map["other"] = 1 map["7"] = 3 map["77"] = 1
Because k = 2 --> result is: 1*3 + 1*1 + 1*3 = 7.
I think problem is with computing binomial coefficient. For the third example it needs to compute (34 choose 17) and it is computing very long time.I've found this article and also this , but I don't understand how they are solving this problem.
My code:
#include<iostream>
#include<string>
#include<map>
#include <algorithm>
#include <vector>
using namespace std;
int binomialCoeff(int n, int k)
{
// Base Cases
if (k == 0 || k == n)
return 1;
// Recur
return binomialCoeff(n - 1, k - 1) + binomialCoeff(n - 1, k);
}
int main()
{
int n, k;
cin >> n >> k;
map<string, int> mapa; // create map, string is a number, int represents number of used string-stored numbers ---> so if 7 was used two times, in the map it will be stored like this mapa["7"] == 2 and so on)
for (int i = 0; i < n; i++) // I will load number as string, if this number is "lucky" - digist are all 7 or all 4
{ // every "unlucky" numbers are together, as well as all same "lucky" numbers ---> so 77 and 77 will be stored in one element....
string number;
cin >> number;
char digit = number[0];
bool lucky = false;
if (digit == '7' || digit == '4')
lucky = true;
for (int j = 1; j < number.length(); j++) {
if (digit != '7' && digit != '4')
break;
if (number[j] != digit) {
lucky = false;
break;
}
}
if (lucky)
mapa[number]++;
else
mapa["other"]++;
}
vector<bool> v(mapa.size());
bool lack = k > mapa.size(); //lack of elements in map --> it is when mapa.size() < k; i. e. number of elements in array can't make k-element subsequence.
int rest = lack ? k - mapa.size() + 1 : 1; // how many elements from "unlucky" numbers I must choose, so it makes base for binomial coefficient (n choose rest)
if (lack) //if lack is true, different size of vector
fill(v.begin() + mapa.size(), v.end(), true);
else
fill(v.begin() + k, v.end(), true);
int *array = new int[mapa.size()]; //easier to manipulate with array for me
int sum = 0;
int product = 1;
int index = 0;
for (map<string, int> ::iterator pos = mapa.begin(); pos != mapa.end(); ++pos) // create array from map
{
if (lack && pos->first == "other") { //if lack of elements in map, the number in elemets representing "unlucky" numbers will be binomial coefficient (mapa["other] choose rest)
array[index++] = binomialCoeff(mapa["other"], rest);
continue;
}
array[index++] = pos->second;
}
do { // this will create every posible multiplication for k-elements subsequences
product = 1;
for (int i = 0; i < mapa.size(); ++i) {
if (!v[i]) {
product *= array[i];
}
}
sum += product;
} while (next_permutation(v.begin(), v.end()));
if (mapa["other"] >= k && mapa.size() > 1) { // if number of "unlucky" numbers is bigger than k, we need to compute all possible k-elements subsequences just from "unlucky" number, so binomial coefficient (mapa["other] choose k)
sum += binomialCoeff(mapa["other"], k);
}
cout << sum % 1000000007 << endl;
}

What's wrong with 3n+1 program?

Here is my code for uva 3n+1 problem:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main(){
ios_base::sync_with_stdio(false);
while(1){
long long n, n2, i, iConst, maximum;
vector<long long> vCycle;
cin >> n >> n2;
if(n < n2){
for(long long i = n; i <= n2; i++){
long long j = 1;
iConst = i;
while(iConst > 1){
if(iConst%2 == 0)
iConst = iConst / 2;
else
iConst = (iConst*3)+1;
j++;
}
vCycle.push_back(j);
}
}else{
for(long long i = n2; i <= n; i++){
long long j = 1;
iConst = i;
while(iConst > 1){
if(iConst%2 == 0)
iConst = iConst / 2;
else
iConst = (iConst*3)+1;
j++;
}
vCycle.push_back(j);
}
}
maximum = *max_element(vCycle.begin(), vCycle.end());
cout << n << " " << n2 << " " << maximum << endl;
}
return 0;
}
But the judge is giving the following error: "Time limit exceed".
Is there anything in my code which is using much CPU?
What's wrong with my program?
*Problem link: here
This is not the way, for few numbers it takes a lot of iterations to get the final output where they are already precalculated.
For Example
for n = 10,
10
5
16
8
4
2
1
for n = 20,
20
10
5
16
8
4
2
1
for n = 160,
160
80
40
20
10
5
16
8
4
2
1
Look at how many numbers are getting repeated, you already calculated the number of steps for 10, when calculating for n = 20, we need not calculate it again when n becomes 10 as we have done it already.
I suggest you to maintain a cache of calculated numbers and check each time if the value is pre-calculated, if yes just give the number + the existing count.
else do the math.
This is a Project Euler problem. Longest Collatz sequence is what it is called. They need more than code to solve them.
It is timing out because your algorithm is quite inefficient.
Just think of the test case given with the problem description -
22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
When you are generating the sequence for finding the answer for 22, you can also calculate the answers for every other number in the sequence.
int foo( int n )
{
if ( ans[n] has been calculated )
{
return ans[n]
}
if ( n is odd )
{
ans[n] = foo( 3n + 1 ) + 1
}
else
{
ans[n] = foo( n / 2 ) + 1
}
return ans[n]
}
where ans is an array of size 10^6.
As far as the task of finding the maximum between any 2 numbers is concerned, you can use a segment tree rather than simply finding the maximum element using a linear search

Finding total number of unique factorization

I want to find total factors of any number.
In number theory, factorization is the breaking down of a composite number into smaller non-trivial divisors, which when multiplied together equal the original integer. Your job is to calculate number of unique factorization(containing at least two positive integers greater than one) of a number.
For example: 12 has 3 unique factorizations: 2*2*3, 2*6, 3*4 . Note:
3*4 and 4*3 are not considered different.
I have attempted to find that but not getting exact for all.
Here is my code :
#include<iostream>
using namespace std;
int count=0;
void factor(int n,int c,int n1)
{
for(int i=n1; i<n ; i++)
{
if(c*i==n)
{count++;
return;}
else
if(c*i>n)
return;
else
factor(n,c*i,i+1);
}
return;
}
int main()
{
int num,n;
cin>>num;
for(int i=0 ; i<num ; i++)
{
cin>>n;
count=0;
factor(n,1,1);
cout<<count<<endl;
}
return 0;
}
Input is number of test cases followed by test-cases(Numbers).
Example : Input: 3 12 36 3150
Output: 3 8 91
I think you are looking for number of factorizations of a number which are unique.
For this I think you need to find the count of number of prime factor of that number. Say for
12 = 2, 2, 3
Total count = 3;
For 2, 2, 3 we need
(2*2)*3 ~ 4*3
2*(2*3) ~ 2*6
2*2*3 ~ 2*2*3
To solve this we have idea found in Grimaldi, discrete and combinatorial mathematics.
To find number of ways of adding to a number(n) is 2^(n-1) -1. For 3 we have...
3 =
1+1+1
2+1
1+2
Total count = 2^(3-1) -1 = 4-1 = 3
We can use analogy to see that
1+1+1 is equivalent to 2*2*3
1+2 is equivalent to 2*(2*3)
2+1 is equivalent to (2*2)*3
Say number of prime factors = n
So we have number of factorizations = 2^(n-1)-1
The code:
#include <stdio.h>
int power(int x, int y)
{
int prod =1, i ;
for(i=1; i<=y;i++) prod *= x;
return prod;
}
int main()
{
int number,div;
int count = 0, ti, t;
printf("Input: ");
scanf("%d",&t);
for(ti=1; ti<=t;ti++)
{
scanf("%d", &number);
div = 2;count = 0;
while(number != 0)
{
if(number%div!=0) div = div + 1;
else
{
number = number / div;
//printf("%d ",div);
count++;
if(number==1) break;
}
}
printf("%d ", power(2,count-1)-1);
}
return 0;
}
Using mod is really useful in attempting to factor:
for(int i = 1; i <= fnum; ++i){ //where fnum is the number you wish to factor
if(!(fnum % i)) ++count;
}
return count;
Of cross this is the number of factors, not unique factors, if you want the number of unique factors, you have to do some additional work.
The solution is to realize that of all permutations, precisely one is sorted. 2 * 4 * 7 * 3 gives the same result as 2 * 3 * 4 * 7. That means that when you've found one factor, you should not check the remainder for lower factors. However, you should check if the same factor appears again: 12 = 2 * 2 * 3. The sequence 2 2 3 is also sorted.
BTW, you should give your variables clearer names, or at least add some comments describing them.