100 - The 3n + 1 problem

All about problems in Volume 1. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

Eduard
Experienced poster
Posts: 183
Joined: Fri Sep 26, 2003 2:54 pm
Location: Armenia,Yerevan

Post by Eduard »

ok it'c for C but i'm using Pascal.
someone who like to solve informatic problems.
http://acm.uva.es/cgi-bin/OnlineJudge?AuthorInfo:29650
Subeen
Experienced poster
Posts: 127
Joined: Tue Nov 06, 2001 2:00 am
Location: Bangladesh
Contact:

Post by Subeen »

long int works fine (I got AC using long int in C++). and no need for array. :)
Six of Three
New poster
Posts: 5
Joined: Mon Sep 29, 2003 8:59 am
Location: Germany: Jena
Contact:

Post by Six of Three »

I used a workaround for the problem in PASCAL, combining 2 longints and thus emulating 48bit integers which were long enough.
I wonder if you could use the (AFAIK) 64bit integer type "comp", which I think is only part of Borland (Turbo) Pascal. Is there a similar integer type in freepascal?
We Are The BORG. You Will Be Assimilated. Resistance Is Futile.
little joey
Guru
Posts: 1080
Joined: Thu Dec 19, 2002 7:37 pm

Post by little joey »

You'd better install free pascal if you want to solve more problems.
There are two 64-bit integer types Int64 (signed) and QWord (unsigned), but you don't need them for this problem; standard integer (32-bit, signed) is enough.
Six of Three
New poster
Posts: 5
Joined: Mon Sep 29, 2003 8:59 am
Location: Germany: Jena
Contact:

Post by Six of Three »

You'd better install free pascal if you want to solve more problems.
I see there are good arguments for doing so :-?
There are two 64-bit integer types Int64 (signed) and QWord (unsigned), but you don't need them for this problem; standard integer (32-bit, signed) is enough.
Are you sure? I know it worked for the old 100.000-problem, but programs using the standard algorithm shown in the sample program do not seem to work with the new one.
We Are The BORG. You Will Be Assimilated. Resistance Is Futile.
Eduard
Experienced poster
Posts: 183
Joined: Fri Sep 26, 2003 2:54 pm
Location: Armenia,Yerevan

Post by Eduard »

Dear friends i find another solution for problem 100 and i got accepted.
if the number n mod 2 <>0 then i'm not doing like this n*3+1 then it div 2.
i'm using an algoritm which find for n (n*3+1) div 2 without doing it and so i don't get a very big numbers.if someone whant to know my algoritm i can tel you. :lol: :lol:
someone who like to solve informatic problems.
http://acm.uva.es/cgi-bin/OnlineJudge?AuthorInfo:29650
Six of Three
New poster
Posts: 5
Joined: Mon Sep 29, 2003 8:59 am
Location: Germany: Jena
Contact:

Post by Six of Three »

Huh, this sounds like the incredible n:=n+n shr 1+1 tweak which delays the crashing point by some numbers. Arr... guess you did not mean that, it would be way too easy. :lol:
Finding (n*3+1)div 2 without "doing it"... sounds a little bit lunatic. I am glad there is another insane here :lol: :lol:

I think I should try the unsigned int from Freepascal, perhaps only this one bit is needed :)
We Are The BORG. You Will Be Assimilated. Resistance Is Futile.
Eduard
Experienced poster
Posts: 183
Joined: Fri Sep 26, 2003 2:54 pm
Location: Armenia,Yerevan

Post by Eduard »

ok this is my solution.(my solution is for odd n)
for 1 (1*1+1)div 2 =2
for 3 (3*3+1)div 2 =5
for 5 =8
for 7 =11
for 9 =14
for 11 =17
...................................
...................................
...................................
it is not hard to sea that
2+3=5
5+3=8
8+3=11
11+3=14
14+3=17
it is progresia for which a[1]=2 and d=3
now this is the formuly which find for n (n*3+1) div 2
2+(((n+1) div 2)-1)*3
i whant to tell you that
2+(((n+1) div 2)-1)*3= (n*3+1) div 2
someone who like to solve informatic problems.
http://acm.uva.es/cgi-bin/OnlineJudge?AuthorInfo:29650
playerX
Learning poster
Posts: 63
Joined: Fri Apr 25, 2003 11:57 pm
Location: Coimbra, Portugal

Post by playerX »

optimization, algorithmic and io
Six of Three
New poster
Posts: 5
Joined: Mon Sep 29, 2003 8:59 am
Location: Germany: Jena
Contact:

Post by Six of Three »

Your formula is the same as mine, only a few more brackets ;)
Guess you have made some other optimization which prevents from computing these numbers which do not fit in Int32. Or which data type do you use?

Hmm... I think Int64 and similar fits best here :)
We Are The BORG. You Will Be Assimilated. Resistance Is Futile.
Morning
Experienced poster
Posts: 134
Joined: Fri Aug 01, 2003 2:18 pm
Location: Shanghai China

100 compile error,please help!

Post by Morning »

Code: Select all

/* @JUDGE_ID: 88826AB 100 C++ "THE 3N+1 PROBLEM" */
#include<stdio.h>
void main()
{
	long i,bi,min,max;
	int length,maxlength;
	while(1)
	{
		scanf("%ld %ld",&min,&max);
		printf("%ld %ld",min,max);
		maxlength=1;
		for(i=max;i>=min;i--)
		{
			length=1;
			//printf(" %d",max);
			bi=i;
			while(i!=1)
			{
				
				if(i%2==1) 
				{
					length+=1;
					i=i*3+1;
					//printf(" %d",i);
				}
				if(i%2==0)
				{
					length+=1;
					i=i/2;
					//printf(" %d",i);
				}
			}
			//length+=1;
			//printf("over\n");
			
			if(length>maxlength) maxlength=length;
			i=bi;
		}
		printf(" %d\n",maxlength);
	}
}
it said "01935883_24.c:44: syntax error before character 0323"
what does it mean?
"Learning without thought is useless;thought without learning is dangerous."
"Hold what you really know and tell what you do not know -this will lead to knowledge."-Confucius
Eduard
Experienced poster
Posts: 183
Joined: Fri Sep 26, 2003 2:54 pm
Location: Armenia,Yerevan

Post by Eduard »

i use longint in pascal it's 4 byte(32 bit)
someone who like to solve informatic problems.
http://acm.uva.es/cgi-bin/OnlineJudge?AuthorInfo:29650
Subeen
Experienced poster
Posts: 127
Joined: Tue Nov 06, 2001 2:00 am
Location: Bangladesh
Contact:

Post by Subeen »

replace your '//' style comments with '/* */' style comments. then there will be no compile error in your code.

But then u will get TLE cause ur program never terminates. :(
while(1) :)

and after fixing this u will get TLE again cause...:wink:
Morning
Experienced poster
Posts: 134
Joined: Fri Aug 01, 2003 2:18 pm
Location: Shanghai China

Post by Morning »

yeah,now i can solve the problem,thank u indeed :D
"Learning without thought is useless;thought without learning is dangerous."
"Hold what you really know and tell what you do not know -this will lead to knowledge."-Confucius
Morning
Experienced poster
Posts: 134
Joined: Fri Aug 01, 2003 2:18 pm
Location: Shanghai China

Post by Morning »

i'm sorry,but i still got complie error message

Code: Select all

#include<stdio.h>
void main()
{
	long i,bi,min,max,temp;
	int length,maxlength;
	while(1)
	{
		
		printf("%ld %ld",min,max);
		maxlength=1;
		
		if(max<min)
		{
			temp=max;
			max=min;
			min=temp;
		}
		
		for(i=max;i>=min;i--)
		{
			length=1;
			bi=i;
			
			while(i!=1)
			{
				
				if(i%2==1) 
				{
					length+=1;
					i=i*3+1;
				}
				if(i%2==0)
				{
					length+=1;
					i=i/2;
				}
			}
			
			if(length>maxlength) 
				maxlength=length;
			i=bi;
		}
		
		printf(" %d\n",maxlength);
	}
[/code]
"Learning without thought is useless;thought without learning is dangerous."
"Hold what you really know and tell what you do not know -this will lead to knowledge."-Confucius
Post Reply

Return to “Volume 1 (100-199)”