Velocity Reviews > Why this doesn't work

# Why this doesn't work

Nick
Guest
Posts: n/a

 12-31-2010
James Waldby <(E-Mail Removed)> writes:

> On Fri, 31 Dec 2010 10:15:27 -0800, Ben Pfaff wrote:
>> http://www.velocityreviews.com/forums/(E-Mail Removed) (Richard Harter) writes:
>>
>>> Are there any numbers that are not palindromes in any base, with the
>>> proviso that the palindrome must have at least two "digits"? If so,
>>> which is the smallest?

>>
>> I think that 2 is the smallest, because it is not a palindrome in base
>> 2, and with bases higher than 2 it has only one digit.

>
> But 2 is a palindrome in base 1. Indeed, every number n>1 is
> a palindrome in base 1 and in base n-1, so if n>1, n is not a
> number that is not a palindrome in any base. However, no number
> that is preceded by a minus sign is a palindrome in a
> number system with a positive base.

But base 1 doesn't really exist, at least not in the way the others do,
because there's no place value. For example, you can't - as far as I
can see - take a generic multiplication algorithm that works in base 2
to base something-very-large and use it on base 1 numbers.
--
Online waterways route planner | http://canalplan.eu
Plan trips, see photos, check facilities | http://canalplan.org.uk

Keith Thompson
Guest
Posts: n/a

 12-31-2010
(E-Mail Removed) (Richard Harter) writes:
> On Fri, 31 Dec 2010 11:07:04 +0000 (UTC), Ike Naar
> <(E-Mail Removed)> wrote:
>
>>On 2010-12-31, Milutin_Popovski <(E-Mail Removed)> wrote:
>>> //It occures is a number palindrom,
>>> //For example 1221 is plandrom, and 1231 is not...

>>
>>If you write 1231 in base 11 notation, it becomes a1a which _is_
>>a palindrome.

>
> Which suggests a little puzzle. Are there any numbers that are not
> palindromes in any base, with the proviso that the palindrome must
> have at least two "digits"? If so, which is the smallest?

Yes. 0 is the smallest.

Given your conditions, neither 1 nor 2 can be expressed as a
palindrome with at least two digits. Any number N > 2 can be
expressed as 11 base N-1. (Base 1 doesn't work with the usual
positional notation; if you relax the rules, 2 can be expressed as
11 base 1.)

But I think there's a non-trivial puzzle hiding in there if you
restrict the possibilities to sufficiently large numbers and
disallow 11.

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Ben Pfaff
Guest
Posts: n/a

 12-31-2010
(E-Mail Removed) (Richard Harter) writes:

> On Fri, 31 Dec 2010 10:15:27 -0800, (E-Mail Removed) (Ben Pfaff)
> wrote:
>
>>(E-Mail Removed) (Richard Harter) writes:
>>
>>> Are there any numbers that are not palindromes in any base,
>>> with the proviso that the palindrome must have at least two
>>> "digits"? If so, which is the smallest?

>>
>>I think that 2 is the smallest, because it is not a palindrome in
>>base 2, and with bases higher than 2 it has only one digit.

>
> Well done. Is it the only one?

No. This argument can be generalized: N is not a palindrome in
base N, and in bases greater than N it has only one digit.
--
char a[]="\n .CJacehknorstu";int putchar(int);int main(void){unsigned long b[]
={0x67dffdff,0x9aa9aa6a,0xa77ffda9,0x7da6aa6a,0xa6 7f6aaa,0xaa9aa9f6,0x11f6},*p
=b,i=24;for(;p+=!*p;*p/=4)switch(0[p]&3)case 0:{return 0;for(p--;i--;i--)case+
2:{i++;if(i)break;else default:continue;if(0)case 1utchar(a[i&15]);break;}}}

Barry Schwarz
Guest
Posts: n/a

 01-01-2011
On Fri, 31 Dec 2010 02:16:55 -0800 (PST), luser- -droog
<(E-Mail Removed)> wrote:

>On Dec 31, 4:12*am, luser- -droog <(E-Mail Removed)> wrote:
>
>> int chk(char *c){
>> * * do {
>> * * * * if (!isdigit(*c)) return 0;
>> * * } while(*c++);

> make that: *++c

While the update is slightly more efficient, it has no practical
difference from the original.

>> * * return 1;
>>
>> }
>>
>>

--
Remove del for email

Barry Schwarz
Guest
Posts: n/a

 01-01-2011
On Fri, 31 Dec 2010 01:41:07 -0800 (PST), Milutin_Popovski
<(E-Mail Removed)> wrote:

It would be nice if you told us why you think it is not working.

>//It occures is a number palindrom,
>//For example 1221 is plandrom, and 1231 is not...
>
>#include<stdio.h>
>#include<malloc.h>
>
>struct lista{
> int val;
> struct lista *next;
> struct lista *prev;
>};
>
>int main(int argc , char *argv[]){
> int i,j, k, n, m;
> struct lista *first, *temp, *temp_prev, *old, *last;
> if(argc!=2 || !chk(argv[1])){
> printf("\nWrong argument");
> return 1;
> }
> m=atoi(argv[1]);
> first=malloc(sizeof(struct lista));
> temp=first;
> old=first;
> temp->next=NULL;
> temp->prev=NULL;
> old->prev=NULL;

How many times do you need to set the same object to NULL?

> n=m;
> while(m>0){
> temp->val=m%10;
> old=temp;
> temp->next=malloc(sizeof(struct lista));
> temp=temp->next;
> temp->next=NULL;
> temp->prev=old;
> m=m/10;
> }
> temp=first;
> printf("\n======================\n");
> while(temp->next){
> printf("%d ", temp->val);
> temp=temp->next;
> }
> temp=temp->prev;
> last=temp;
> printf("\n======================\n");
> while(temp){
> printf("%d ", temp->val);
> temp=temp->prev;
> }
> temp=first;
> temp_prev=last;
> while(temp){
> if(temp->val!=temp_prev->val) goto izlaz;

There is an "else" construct in the language which eliminates the need
for an unstructured goto like this.

> printf("\n<%d><%d>", temp->val, temp_prev->val);
> temp=temp->next;
> temp_prev=temp_prev->prev;
> }
> printf("\n%d is palindrom", n);
> return 0;
>izlaz:
> printf("\n%d is not a palindrom", n);
>
> return 0;
>}
>
>
>
>int chk(char *c){
> if(!isdigit(*c) && *c!='\0') return 0;
> if(*c=='\0') return 1;
> *c++;

Why are you bothering to dereference the pointer and then throw the
resulting char away?

> chk(c);

You call the function recursively yet you ignore the value it returns.
What value will get passed back to main if the first character is a
valid digit?

>}

--
Remove del for email

Guest
Posts: n/a

 01-01-2011
On 12/31/2010 1:10 PM, Ben Pfaff wrote:
> (E-Mail Removed) (Richard Harter) writes:
>
>> On Fri, 31 Dec 2010 10:15:27 -0800, (E-Mail Removed) (Ben Pfaff)
>> wrote:
>>
>>> (E-Mail Removed) (Richard Harter) writes:
>>>
>>>> Are there any numbers that are not palindromes in any base,
>>>> with the proviso that the palindrome must have at least two
>>>> "digits"? If so, which is the smallest?
>>>
>>> I think that 2 is the smallest, because it is not a palindrome in
>>> base 2, and with bases higher than 2 it has only one digit.

>>
>> Well done. Is it the only one?

>
> No. This argument can be generalized: N is not a palindrome in
> base N, and in bases greater than N it has only one digit.

Yes. For integers greater than two, N is a palindrome in base (N-1).

--

luser- -droog
Guest
Posts: n/a

 01-01-2011
On Dec 31, 10:53*pm, Barry Schwarz <(E-Mail Removed)> wrote:
> On Fri, 31 Dec 2010 02:16:55 -0800 (PST), luser- -droog
>
> <(E-Mail Removed)> wrote:
> >On Dec 31, 4:12 am, luser- -droog <(E-Mail Removed)> wrote:

>
> >> int chk(char *c){
> >> do {
> >> if (!isdigit(*c)) return 0;
> >> } while(*c++);

> > * make that: *++c

>
> While the update is slightly more efficient, it has no practical
> difference from the original.
>
> >> return 1;

Um, I think it does. I think it's crucial, in fact.
Consider chk("5");
!isdigit('5') : 0
*c++ : 1
!isdigit('\0') : 1
return 0

If you don't increment BEFORE dereferencing, you're
not checking the same value that the next iteration
will encounter.

Ben Pfaff
Guest
Posts: n/a

 01-01-2011

> On 12/31/2010 1:10 PM, Ben Pfaff wrote:
>> (E-Mail Removed) (Richard Harter) writes:
>>
>>> On Fri, 31 Dec 2010 10:15:27 -0800, (E-Mail Removed) (Ben Pfaff)
>>> wrote:
>>>
>>>> (E-Mail Removed) (Richard Harter) writes:
>>>>
>>>>> Are there any numbers that are not palindromes in any base,
>>>>> with the proviso that the palindrome must have at least two
>>>>> "digits"? If so, which is the smallest?
>>>>
>>>> I think that 2 is the smallest, because it is not a palindrome in
>>>> base 2, and with bases higher than 2 it has only one digit.
>>>
>>> Well done. Is it the only one?

>>
>> No. This argument can be generalized: N is not a palindrome in
>> base N, and in bases greater than N it has only one digit.

>
> Yes. For integers greater than two, N is a palindrome in base (N-1).

Oops. Thanks, you're right of course.
--
char a[]="\n .CJacehknorstu";int putchar(int);int main(void){unsigned long b[]
={0x67dffdff,0x9aa9aa6a,0xa77ffda9,0x7da6aa6a,0xa6 7f6aaa,0xaa9aa9f6,0x11f6},*p
=b,i=24;for(;p+=!*p;*p/=4)switch(0[p]&3)case 0:{return 0;for(p--;i--;i--)case+
2:{i++;if(i)break;else default:continue;if(0)case 1utchar(a[i&15]);break;}}}

Barry Schwarz
Guest
Posts: n/a

 01-01-2011
On Fri, 31 Dec 2010 21:32:18 -0800 (PST), luser- -droog
<(E-Mail Removed)> wrote:

>On Dec 31, 10:53*pm, Barry Schwarz <(E-Mail Removed)> wrote:
>> On Fri, 31 Dec 2010 02:16:55 -0800 (PST), luser- -droog
>>
>> <(E-Mail Removed)> wrote:
>> >On Dec 31, 4:12 am, luser- -droog <(E-Mail Removed)> wrote:

>>
>> >> int chk(char *c){
>> >> do {
>> >> if (!isdigit(*c)) return 0;
>> >> } while(*c++);
>> > * make that: *++c

>>
>> While the update is slightly more efficient, it has no practical
>> difference from the original.
>>
>> >> return 1;

>
>Um, I think it does. I think it's crucial, in fact.
>Consider chk("5");
>!isdigit('5') : 0
>*c++ : 1
>!isdigit('\0') : 1
> return 0
>
>If you don't increment BEFORE dereferencing, you're
>not checking the same value that the next iteration
>will encounter.

You are correct. My mistake. Sorry.

--
Remove del for email

luser- -droog
Guest
Posts: n/a

 01-01-2011
On Jan 1, 10:51*am, Barry Schwarz <(E-Mail Removed)> wrote:
> On Fri, 31 Dec 2010 21:32:18 -0800 (PST), luser- -droog
>
>
>
> <(E-Mail Removed)> wrote:
> >On Dec 31, 10:53 pm, Barry Schwarz <(E-Mail Removed)> wrote:
> >> On Fri, 31 Dec 2010 02:16:55 -0800 (PST), luser- -droog

>
> >> <(E-Mail Removed)> wrote:
> >> >On Dec 31, 4:12 am, luser- -droog <(E-Mail Removed)> wrote:

>
> >> >> int chk(char *c){
> >> >> do {
> >> >> if (!isdigit(*c)) return 0;
> >> >> } while(*c++);
> >> > make that: *++c

>
> >> While the update is slightly more efficient, it has no practical
> >> difference from the original.

>
> >> >> return 1;

>
> >Um, I think it does. I think it's crucial, in fact.
> >Consider chk("5");
> >!isdigit('5') : 0
> >*c++ * * * * *: 1
> >!isdigit('\0') : 1
> > * *return 0

>
> >If you don't increment BEFORE dereferencing, you're
> >not checking the same value that the next iteration
> >will encounter.

>
> You are correct. *My mistake. *Sorry.
>

No sweat. There's something about 'do if' that's easy
to misinterpret. I suggested something similar about
a year ago; and someone said it wasn't C.