Run-time error occurs when input value is greater than 100000 - c

I am trying to implement the sieve of eratosthenes in C. The code works for small input values, but once the input goes beyond a certain range, a run- time error is thrown. This is the second problem in the classical section of the SPOJ base. What is the mistake?
#include<stdio.h>
#include<math.h>
int prime(unsigned long int, unsigned long int);
int main()
{
int nitem;
unsigned long int sn,fn;
scanf("%d", &nitem);
while(nitem)
{
scanf("%lu", &fn);
//printf("%d",fn);
scanf("%lu", &sn);
prime(fn, sn);
nitem--;
}
return 0;
}
int prime(unsigned long int fn, unsigned long int sn)
{
unsigned long int prim[100000];
int i,j,k;
for(i = 0; i < 100000; i++)
{
prim[i] = 1;
}
prim[0] = 0;
prim[1] = 0;
//printf("%d", sn);
//printf("%d", k);
//printf("%d", (k <= sn));
for(k = 2; k <= sqrt(sn); k++)
{
// printf("alksnc%5d", k);
if(prim[k] == 1)
{
for(j = 2; (k * j) <= sn; j++)
{
//printf("%d", prim[k]);
prim[k * j] = 0;
}
}
}
for(int i = 0; i <= sn; i++)
{
if(prim[i] !=0 && i >= fn)
{
printf("%lu\n", i);
}
}
printf("\n");
return;
}
Input:
1
100000 100345
output:
run time error
Input:
1
3 5
output:
3
5

We can make more efficient use of memory (2x) by only sieving odd numbers as all the even numbers you're processing waste time and space. It's trickier to work out but gives us something like:
#include <math.h>
#include <libc.h>
#define MAX_ODD_PRIMES 1048576
void prime(unsigned long fn, unsigned long sn)
{
unsigned char primes[MAX_ODD_PRIMES];
for (unsigned long i = 0; i < MAX_ODD_PRIMES; i++)
{
primes[i] = TRUE;
}
primes[0] = 0; // preset first odd, '1'
for (unsigned long k = 3; k <= sqrt(sn) + 1; k += 2)
{
if (primes[k / 2])
{
for (unsigned long j = 3; (k * j) <= sn; j += 2)
{
primes[k * j / 2] = FALSE;
}
}
}
if (fn <= 2)
{
printf("2\n");
fn = 3;
}
for (unsigned long i = fn / 2; i * 2 + 1 <= sn; i++)
{
if (primes[i])
{
printf("%lu\n", i * 2 + 1);
}
}
}
EXAMPLE
> ./a.out
1 1999900 2000000
1999957
1999969
1999979
1999993
>

1) Array range error.
By changing code
for (j = 2; (k * j) <= sn; j++) {
if (k * j >= 100000) {
printf("Out of range %d %d\n", k, j);
exit(1);
}
prim[k * j] = 0;
}
}
With input 2, 100000
Output
Out of range 2 50000
By using an array (VLA) sized to the task, this is avoided. Many other optimizations available. Consider also a malloc() array.
void prime(unsigned long int fn, unsigned long int sn) {
unsigned long int prim[sn + 1];
2) int prime() eventually performs return; where return something; is expected. Suggest changing function to void prime()
int prime(unsigned long int fn, unsigned long int sn) {
unsigned long int prim[100000];
...
printf("\n");
return;
}

Related

How to check if array elements are perfect numbers?

I'm trying to make a program where the array size is not entered by the user,but the elements are, until 0 is entered.Now I want to check for each element which one is a perfect number,for that I have to do the sum of the divisors.Problem is I can't manage to do the sum of divisors for each element in the array,instead it adds all the divisors of all the elements in the array.
#include <stdio.h>
int main()
{
int n = 1000, i, j, sum = 0;
int v[n];
for (i = 1; i < n; i++)
{
scanf("%d", &v[i]);
if (v[i] == 0)
{
break;
}
for (j = 1; j < v[i]; j++)
{
if (v[i] % j == 0)
{
printf("%d", j);
sum = sum + j;
}
}
}
printf("\n%d",sum);
return 0;
}
OUTPUT
Brut force check can be very expensive. It is faster to build the table of perfect numbers using Euclides formula and then simple check if the number is perfect.
static unsigned long long getp(int x)
{
return (2ULL << (x - 2)) * ((2ULL << (x - 1)) - 1);
}
int isperfect(unsigned long long x)
{
const int primes[] = {2, 3, 5, 7, 13, 17, 19, 31};
static unsigned long long array[sizeof(primes) / sizeof(primes[0])];
int result = 0;
if(!array[0])
{
for(size_t index = 0; index < sizeof(primes) / sizeof(primes[0]); index++)
{
array[index] = getp(primes[index]);
}
}
for(size_t index = 0; index < sizeof(primes) / sizeof(primes[0]); index++)
{
if(x == array[index])
{
result = 1;
break;
}
}
return result;
}
The array of perfect numbers is build only one time on the first function call.
And some usage (your code a bit modified)
int main(void)
{
size_t n = 1000, i;
unsigned long long v[n];
for (i = 1; i < n; i++)
{
scanf("%llu", &v[i]);
if (v[i] == 0)
{
break;
}
printf("%llu is %s perfect number\n", v[i], isperfect(v[i]) ? "" : "not");
}
return 0;
}
https://godbolt.org/z/exMs345xb

runtime error: signed integer overflow: 99998 * 100000 cannot be represented in type 'int' [solution.c]

Trying to solve https://leetcode.com/problems/k-concatenation-maximum-sum/submissions/, I still got integer overflow when the data type is long
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
int max(int a, int b) {
return a > b ? a : b;
}
int sum(int *nums, int numSize) {
int ans = 0;
for (int i = 0; i < numSize; ++i)
ans += nums[i];
return ans;
}
int KandaneAlgo(int *nums, int numSize) {
int i, max_overall_so_far = 0, max_ending_here = 0;
for (i = 0; i < numSize; ++i) {
max_ending_here += nums[i];
if (max_ending_here < 0)
max_ending_here = 0;
if (max_overall_so_far < max_ending_here)
max_overall_so_far = max_ending_here;
}
return max_overall_so_far;
}
int kConcatenationMaxSum(int *arr, int arrSize, int k) {
int mod = pow(10, 9) + 7;
int ans;
if (k > 1) {
long tem = (k - 2) * max(sum(arr, arrSize), 0);
int t1 = tem % mod;
printf("%ld, %d, %d \n", tem, t1, mod);
int arr2[2 * arrSize];
for (int i = 0; i < 2 * arrSize; ++i)
arr2[i] = arr[i - i / arrSize * arrSize];
ans = (int)t1 + KandaneAlgo(arr2, 2 * arrSize) % mod;
} else {
ans = KandaneAlgo(arr, arrSize) % mod;
}
return ans;
}
int main() {
int arr[10] = { [0 ... 9] = 10000 };
for (int i = 0; i < 10; ++i)
printf("%d ", arr[i]);
printf("\n");
int tot = kConcatenationMaxSum(arr, 10, 100000);
printf("%d \n", tot);
return 0;
}
I locally debug with lldb and can see the output message of variable tem is wrong indeed, it should be 9999800000.
This is because 9999800000 is larger than what can be stored in 32 bits. long only provides a minimum size guarantee of 32 bits. If you use long long for all the operands and result variable in the expression, it evaluates to the correct value. long long provides a minimum guarantee of 64 bits.
Check this for more details - https://en.wikipedia.org/wiki/C_data_types#Main_types
The following snippet worked for me:
long long tem = (long long)(k-2) * (long long)max(sum(arr, arrSize), 0);
Not sure about the rest of the algorithm, but this puts the correct value into tem.

second smallest element in an array

as a part of a C program I wrote the following function, which finds the second smallest element of an array
unsigned int array_second_min (unsigned int w[], unsigned int n)
{
unsigned int i, erst = UINT_MAX, zweit = UINT_MAX, count = 0;
if (n < 2)
return UINT_MAX;
for (i = 0;i < n; i++) {
if (w[i] == w[i + 1])
count++;
}
if (count == n - 1)
return UINT_MAX;
for (i = 0;i < n;i++) {
if (w[i] < erst)
erst = w[i];
}
for (i = 0;i < n;i++) {
if (w[i] == erst)
continue;
if ((w[i] - erst) < zweit)
zweit = w[i];
}
return zweit;
}
the problem is that it is not really functioning as it should. I think the problem is in the last for loop, but am not sure about that.
Thank you for your help
picture of the output:
The following code will return the second smallest element
unsigned int array_second_min (unsigned int w[], unsigned int n){
unsigned int i, first = UINT_MAX, second = UINT_MAX;
if(n < 2)
return UINT_MAX;
sort(w, w+n);
second = w[n-2];
return second;
}
This is a somewhat more efficient solution, as it is O(n):
struct pair {
int r[2];
};
struct pair small2(int *a, int n) {
int r[2];
int order;
r[0] = a[0];
r[1] = a[1];
order = (r[0] >= r[1]);
for (int i = 2; i < n; i++) {
if (a[i] <= r[order]) {
r[!order] = a[i];
order = !order;
} else if (a[i] <= r[!order]) {
r[!order] = a[i];
}
}
struct pair x;
x.r[0] = r[order];
x.r[1] = r[!order];
return x;
}
There is a lack of detail about duplicates, this handles them in an unsurprising fashion. Note the trickiness of the order variable; this works because you are only interested in the least two, if you wanted the least 3, you would have to add the extra tests. It would remain O(n), but the C would be greater.

Generating palindromes

Recently I wrote this quick'n'dirty code, which generates palindromes as follows:
int main() {
generateEvenPalindromes(1);
}
unsigned long generateEvenPalindromes(int lowerBound) {
unsigned long palindrome = 0;
int upperBound = lowerBound * 10;
for (int i = lowerBound; i < upperBound; i++) {
palindrome = appendMirroredValueToNum(i, -1); // -1 means 'no middle digit'
printf("%lu\n", palindrome);
}
return generateOddPalindromes(lowerBound);
}
unsigned long generateOddPalindromes(int lowerBound) {
unsigned long palindrome = 0;
int upperBound = lowerBound * 10;
for (int i = lowerBound; i < upperBound; i++) {
for (int j = 0; j < 10; j++) {
palindrome = appendMirroredValueToNum(i, j);
printf("%lu\n", palindrome);
}
}
if (upperBound > 10000) {
return 0;
}
return generateEvenPalindromes(upperBound);
}
unsigned long appendMirroredValueToNum(int num, int middleDigit) {
//...
return newNumber;
}
– it produces a sequence of aa and aba palindromes, where a [1;99999], b [0;9], i.e.
11
22
...
99
101
111
121
...
217712
218812
219912
...
99999899999
99999999999
– and I was thinking of taking functions generateEvenPalindromes() and generateOddPalindromes() and combining them into one. If you think this is a good idea, can you please recommend a good approach to achieve this?

Why this reverse function can not work in the for loop?

#include <stdio.h>
#include <math.h>
int prime (long n);
long reverse(long n);
int main(void)
{
long n;
long i, j;
puts("Enter n dight number, and we will help you find symmetrical prime number");
scanf("%ld", &n);
for (i = 11; i < (pow(10, n) - 1); i+= 2)
{
if (prime(i))
{
j = reverse(i);
if (i == j)
{
printf("%ld\n", i);
}
}
}
}
int prime (long n) //estimate whether the number n is primer number
{
int status = 0;
int j;
//1 is prime, 0 is not
if (n % 2 == 0 || n == 3)
{
if (n == 2)
status = 1;
if (n == 3)
status = 1;
else
{
n++;
status = 0;
}
}
else
{
j = 3;
while (j <= sqrt(n))
{
if (n % j == 0)
{
status = 0;
break;
}
else
status = 1;
j+= 2;
}
}
return status;
}
long reverse(long n) //reverse a number
{
int i, j, x;
long k, sum;
int digit = 0;
int ar[1000];
while (n > 0)
{
k = n;
n = n / 10;
x = (k - n*10);
digit++;
ar[digit] = x;
}
for (i = 1,j = digit - 1; i <= digit; i++, j--)
{
sum += ar[i] * pow(10, j)
}
return sum;
}
I build a reverse function in order to reverse numbers, for example, 214, to 412.
This function works fine in individual number, for instance, I type reverse(214), it return 412, which is good. But when I combine reverse() function with for loop, this function can not work... it produces some strange number...
so How can I fix this problem?
The reverse function is extremely complicated. The better way to go about it would be:
long reverse (long n)
{
long result = 0;
while (n != 0)
{
result *= 10;
result += n % 10;
n /= 10;
}
return result;
}
I think the problem in your code is that in the following segment
digit++;
ar[digit] = x;
you first increment the position then assign to it, thus leaving ar[0] unintialized.
How can I fix this problem?
You need to initialize sum
long k, sum = 0;
^
See the code from #Armen Tsirunyan for a simpler approach.

Resources