Add function to projecteuler library

Added the function is_palindrome to the projecteuler library
This commit is contained in:
daniele 2019-09-21 15:44:42 +02:00
parent fdfc406884
commit 4e6b5c774f
Signed by: fuxino
GPG Key ID: 6FE25B4A3EE16FDA
5 changed files with 39 additions and 38 deletions

View File

@ -1,8 +1,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int is_palindrome(int num, int base);
#include "projecteuler.h"
int main(int argc, char **argv)
{
@ -35,24 +34,3 @@ int main(int argc, char **argv)
return 0;
}
int is_palindrome(int num, int base)
{
int reverse = 0, tmp;
tmp = num;
while(tmp > 0)
{
reverse *= base;
reverse += tmp % base;
tmp /= base;
}
if(num == reverse)
{
return 1;
}
return 0;
}

View File

@ -205,3 +205,24 @@ void quick_sort(void **array, int l, int r, int (*cmp)(void *lv, void *rv))
quick_sort(array, l, i-1, cmp);
quick_sort(array, i+1, r, cmp);
}
int is_palindrome(int num, int base)
{
int reverse = 0, tmp;
tmp = num;
while(tmp > 0)
{
reverse *= base;
reverse += tmp % base;
tmp /= base;
}
if(num == reverse)
{
return 1;
}
return 0;
}

View File

@ -5,6 +5,7 @@ int is_prime(long int);
long int lcmm(long int *values, int n);
long int lcm(long int a, long int b);
long int gcd(long int a, long int b);
int is_palindrome(int num, int base);
int *sieve(int n);
int count_divisors(int n);
void insertion_sort(void **array, int l, int r, int (*cmp)(void *lv, void *rv));

View File

@ -1,21 +1,7 @@
#!/usr/bin/python3
from timeit import default_timer
def is_palindrome(num, base):
reverse = 0
tmp = num
while tmp > 0:
reverse = reverse * base
reverse = reverse + tmp % base
tmp = tmp // base
if num == reverse:
return 1
return 0
from projecteuler import is_palindrome
def main():
start = default_timer()

View File

@ -69,3 +69,18 @@ def count_divisors(n):
count = count - 1
return count
def is_palindrome(num, base):
reverse = 0
tmp = num
while tmp > 0:
reverse = reverse * base
reverse = reverse + tmp % base
tmp = tmp // base
if num == reverse:
return 1
return 0