Add function to projecteuler library
Added the function is_palindrome to the projecteuler library
This commit is contained in:
parent
fdfc406884
commit
4e6b5c774f
24
C/p004.c
24
C/p004.c
@ -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;
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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));
|
||||
|
@ -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()
|
||||
|
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user