Generating palindromes - c

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?

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

Adding two numbers [1, 10^10000] as arrays of chars - C

I tackled the problem by first figuring out the length of two given numbers and aligning the one with less digits (if one exists) into a new array so that the ones, tens, hundreds etc. align with the bigger number's ones, tens, hundreds, etc.
Then I wanted to save the sum of each two aligned elements (with a mod of 10) into a new array while checking if the sum of digits is greater than 10 - just the basic sum stuff. Now the problem occurs with adding two elements into the aplusb integer and I've tried fixing it with writing
int aplusb = (lengthA[max-i]-'0') +(temp[max-i]-'0');
but it doesn't work. I'm stuck and I don't know what to do. Please help.
The whole code:
#include <stdio.h>
#include <math.h>
int main(){
char a[10000];
char b[10000];
scanf("%s %s", &a, &b);
char sum[10000];
int lengthA = 0;
int lengthB = 0;
int i = 0;
while(a[i]){
i++;
} lengthA = i;
i = 0;
while(b[i]){
i++;
} lengthB = i;
char temp[10000];
int aplusb;
int carry = 0;
int max = lengthA;
int difference = abs(lengthA - lengthB);
if(lengthA>lengthB){
for(i=0; i<lengthA; i++){
temp[i+difference]=b[i];
}
for(i=0; i<=max; i++){
aplusb = lengthA[max-i]+temp[max-i]; //<-- this is the problematic line
if(carry = 1) aplusb++;
if(aplusb>9){
carry = 1;
aplusb%=10;
}
sum[i]=aplusb;
}
}
for(i=0; i<=max; i++){
printf("%c", sum[i]);
}
/*
if(lengthB>lengthA){
max = lengthB;
for(i=0; i<lengthB; i++){
temp[i+difference]=a[i];
}
}*/
return 0;
}
Doing operations and storing on very large numbers is very akin to doing operations and storing polynomials, i.e. with x = 10. a0 + a1.10 + a2.10^2 ... + an.10^n.
There are many polynomial libraries on the Internet, where you could find inspiration. All operations on your very large numbers can be expressed in terms of polynomials. This means that by using base 2^8, or even base 2^63, instead of base 10 to internally store your large numbers you would greatly improve performance.
You must also normalize your coefficients after operations to keep them positive. Operations may result in a negative coefficient, That can easily be fixed, as it is very similar to borrowing after a subtraction, this means coefficients must be larger than your base by 1bit.
To convert back to base 10, you'd need to solve r (your result) for v (your value), such as r(10)=v(2^63). This has only one solution, if you enforce the positive coefficients rule.
[note] After thinking about it some more: the rule on positive coefficients may only be necessary for printing, after all.
Example: adding. no memory error checking
int addPolys(signed char** result, int na, const signed char* a, int nb, const signed char* b)
{
int i, nr, nmin, carry, *r;
nr = max(na, nb) + 1;
nmin = min(na, nb);
r = malloc(sizeof(signed char) * (na + nb + 1));
if (nb < na)
{
nr = nb;
}
for (i = 0; i < nmin; ++i)
{
r[i] = a[i] + b[i];
}
for (; i < na; ++i)
{
r[i] = a[i];
}
for (; i < nb; ++i)
{
r[i] = b[i];
}
r[nr - 1] = 0;
// carry - should really be a proc of its own, unoptimized
carry = 0;
for (i = 0; i < nr; ++i)
{
r[i] += carry;
if (r[i] > 10)
{
carry = r[i] / 10;
r[i] %= 10;
}
else if (r[i] < 0)
{
carry = (r[i] / 10) - 1;
r[i] -= (carry * 10);
}
else
carry = 0;
}
// 'remove' leading zeroes
for (i = nr - 1; i > 0; --i)
{
if (r[i] != 0) break;
}
++i;
*result = r;
if (i != nr)
{
*result = realloc(i * sizeof(signed char));
}
return i; // return number of digits (0 being 1 digit long)
}
That code is working now for any two positive numbers with up to ten thousand digits:
#include <stdio.h>
#include <math.h>
#include <string.h>
int main(){
char chara[10000];
char charb[10000];
scanf("%s %s", &chara, &charb);
int lengthA = strlen(chara);
int lengthB = strlen(charb);
int max = lengthA;
if(lengthB>lengthA) max=lengthB;
int dif = abs(lengthA - lengthB);
//ustvari int tabele
int a[max];
int b[max];
int sum[max+1];
// nastavi nule
int i;
for(i=0; i<max; i++){
a[i] = 0;
b[i] = 0;
sum[i] = 0;
} sum[max] = 0;
//prekopiraj stevila iz char v int tabele &obrni vrstni red
for(i=0; i<lengthA; i++){
a[i] = chara[lengthA-i-1]-'0';
}
for(i=0; i<lengthB; i++){
b[i] = charb[lengthB-i-1]-'0';
}
int vsota;
int prenos = 0;
for(i=0; i<max; i++){
vsota = a[i]+b[i] + prenos;
if(vsota>=10) prenos = 1;
else if (vsota<10) prenos = 0;
sum[i]=vsota%10;
}
if(prenos==1){
sum[max] = 1;
for(i = max; i>=0; i--){
printf("%d", sum[i]);
}
} else {
for(i = max-1; i>=0; i--){
printf("%d", sum[i]);
}
}
return 0;
}

Sieve of Eratosthenes C implementation error

So I am making next homework with input numbers to be dissolved to primes multiplication in output. I was confused when it showed up only prime number 2, so I did a proper control dump of the allocated primes array using this function:
long* eratosthen(long max) {
bool grid[max + 1];
for(long i = 0; i < max + 1; ++i) {
grid[i] = true;
}
for(long i = 2; i < max + 1; ++i) {
if(grid[i]) {
for(long j = i * 2; j < max + 1; ++j) {
grid[j] = false;
}
}
}
long *primes;
primes = (long*)malloc((max + 1) * sizeof(long));
long index = 0;
for(long i = 2; i < max + 1; ++i) {
if(grid[i]) {
primes[index++] = i;
}
}
return primes;
}
For some unknown reason, the dump says 2 only and ends. Calling for dump is made up by following code:
int main(void) {
// Prime numbers get calculated first
long *primes;
primes = eratosthen(1000000);
// Control code
fprintf(stdout, "Control dump of primes array:\n");
for(long i = 0; i < sizeof(primes) / sizeof(long); ++i) {
fprintf(stdout, "%li\n", primes[i]);
}
int ret = 0;
/// ...
return ret;
}
//EDIT: So after all your answers I'll update my question to current state. Main program:
int main(void) {
// Prime numbers get calculated first
long *primes;
primes = eratosthen(1000000);
// Control code
fprintf(stdout, "Control dump of primes array:\n");
for( ; *primes != 0 ; primes++) {
fprintf(stdout, "%li\n", *primes);
}
int ret = 0;
}
Generation of primes function:
long* eratosthen(long max) {
bool *grid;
grid = (bool*)malloc((max + 1) * sizeof(bool));
index = 0;
for(long i = 0; i < max + 1; ++i) {
*(grid + index) = true;
index++;
}
index = 2;
index_2 = 2;
for(long i = 2; i < max + 1; ++i) {
if(*(grid + index)) {
for(long j = i * 2; j < max + 1; j += i) {
*(grid + 2 * index_2) = false;
index_2++;
}
index++;
}
}
long *primes;
primes = (long *)malloc((max + 1) * sizeof(long));
index = 0;
for(long i = 0; i < max + 1; ++i) {
*(primes + index) = 0;
index++;
}
index = 0;
index_2 = 2;
for(long i = 2; i < max + 1; ++i) {
if(*(grid + index_2)) {
*(primes + index) = i;
index++;
index_2++;
}
}
// free the grid
free(grid);
return primes;
}
Like said before, the Ubuntu terminal displays following line:
Neoprávněný přístup do paměti (SIGSEGV) (core dumped [obraz paměti uložen])
translated like this:
Forbidden access to memory (SIGSEGV) (core dumped [memory image saved])
What does it mean and how to get rid of that? :(
The simplest fix to your code is probably to add a sentinel value of 0 to the end of the array of primes, and to rewrite the check condition in main(). As explained extensively in the comments, your current test:
for(long i = 0; i < sizeof(primes) / sizeof(long); ++i) {
is irremediably wrong. You must not attempt to use sizeof because it simply doesn't do what you want it to do.
This code works:
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
static
long *eratosthen(long max)
{
bool grid[max + 1];
for (long i = 0; i < max + 1; ++i)
grid[i] = true;
for (long i = 2; i < max + 1; ++i)
{
if (grid[i])
{
for (long j = i * 2; j < max + 1; j += i) // Key fix
grid[j] = false;
}
}
long *primes;
primes = (long *)malloc((max + 1) * sizeof(long));
long index = 0;
for (long i = 2; i < max + 1; ++i)
{
if (grid[i])
primes[index++] = i;
}
primes[index] = 0; // Sentinel
return primes;
}
int main(void)
{
// Prime numbers get calculated first
long *primes = eratosthen(1000000);
fprintf(stdout, "Control dump of primes array:\n");
for (long i = 0; primes[i] != 0; ++i)
{
printf("%7li", primes[i]);
if (i % 10 == 9)
putchar('\n');
}
putchar('\n');
return 0;
}
I revised the code to print 10 numbers per line. I changed the condition in the loop in main(). And I made two key changes in the function — one to calculate multiples of primes correctly, and the other to add the sentinel at the end of the list of primes.
It produces a list of 78,498 primes starting with
2 3 5 7 11 13 17 19 23 29
31 37 41 43 47 53 59 61 67 71
73 79 83 89 97 101 103 107 109 113
and ending with:
999563 999599 999611 999613 999623 999631 999653 999667 999671 999683
999721 999727 999749 999763 999769 999773 999809 999853 999863 999883
999907 999917 999931 999953 999959 999961 999979 999983
You might note that the code does not release primes — it arguably should. You might note that the space allocated for the primes greatly exceeds the space needed (one million vs less than eighty thousand); you could use realloc() to shrink the space allocated, or use a less conservative (or do I mean less profligate?) estimate on the number of entries needed.

In C, I would like to print 4 decimal numbers in a row and then print the next 4

#include "stdio.h"
int main() {
int max = 1000;
for (int i = 0; i < max; ++i) {
printf("%d", i);
}
return 0;
}
If max is 1000 then this will print in the format shown below
0123 up to 1000
But I would like to print 4 values per line as shown below:
0123
4567
...
I would like to see the numbers not the just the digits. for a single digit numbers, it should be like this: 0123 for two digit numbers, it should be like this: 11121314 for a three digit numbers, it should be like this: 111112113114 up to 996997998999 up to 1000.
For your loop to print upto and including 1000 for max.size = 1000, you must use the <= operator.
Here is a modified version that will format the output with a maximum of 4 characters per line:
#include <stdio.h>
#include <limits.h>
int main(void) {
struct { int size; } max = { 1000 };
if (max.size >= 0) {
for (int col = 0, i = 0;; i++) {
char buf[2 + sizeof(int) * CHAR_BIT / 3];
int n = snprintf(buf, sizeof buf, "%d", i);
for (int j = 0; j < n; j++) {
putchar(buf[j]);
if (++col == 4) {
putchar('\n');
col = 0;
}
}
if (i == max.size) {
if (col > 0) {
putchar('\n');
}
break;
}
}
}
return 0;
}
It will print:
0123
4567
8910
1112
1718
...
6997
9989
9910
00
EDIT
From your updated question, it is actually much simpler: print a linefeed character after every 4th number, using the modulo operator %.
#include <stdio.h>
int main(void) {
int max = 1000;
for (int i = 0; i < max; ++i) {
printf("%d", i);
if (i % 4 == 3)
putchar('\n');
}
return 0;
}
Just check whether i+1 is divisible by 4 or not. Whenever it is divisible by 4, print a newline.
for (int i = 0; i < max.size; ++i) {
printf("%d", i);
if((i+1)%4 == 0)
printf("\n");
}
You can also do this without using a buffer:
#include <stdio.h>
void print_digit(int number);
int main(void) {
putchar('0');
int i;
for(i = 1; i <= 1000; i++) {
print_digit(i);
}
}
void print_digit(int number) {
static int digit_count = 1; // a zero is already printed
int i;
for(i = 1; i <= number; i *= 10);
for(i /= 10; i; i /= 10) {
putchar('0' + number % (i * 10) / i);
digit_count++;
if(digit_count == 4) {
digit_count = 0;
putchar('\n');
}
}
}
However, I have to admit that this code has nothing to do with elegance, because I don't know how to make print_digit consistent with zero.

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

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;
}

Resources