Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Professional Discussion / Speeding up a program

Author
Message
Cras
23
Years of Service
User Offline
Joined: 15th Oct 2002
Location: United Kingdom
Posted: 27th Nov 2003 19:44
Basically my maths teacher gave me a puzzle -

If you take a number and move its 1st digit to the back it doubles. What number is it?

eg: 1498 becomes 4981

that is not the number obviously, but i made a prgram to find the number and its running right now. It goes all the way to 30 digits just in case, but when it gets to the millions each change will take an hour and thats only 7 digits. I have it printing whether or not each number works. Will that slow it down? I cant leave my comp running for about 12 digits worth of hours just to find this number. The only thing i can think is that I could make it ignore a few numbers - The ones which start with odd because double the 1st number isnt going to end with an odd number. That would halve the time - down to 6 digits worth of hours now. still a bit too many ^^

uk.geocities.com/maniacimagine check it out. ill soon be formally opening it.
Scraggle
Moderator
23
Years of Service
User Offline
Joined: 10th Jul 2003
Location: Yorkshire
Posted: 27th Nov 2003 20:27
How about this:

Did your teacher say the number had to be in the decimal system?

If not then the answer is:

01 binary becomes 10 binary (or 1 becomes 2 (decimal))

Ta da - I beat the computer

GOD exists and his name is Jonny Wilkinson

JSN
22
Years of Service
User Offline
Joined: 8th Nov 2003
Location: 127.0.0.1
Posted: 27th Nov 2003 20:27
Hmmm, taking that long really sounds like there could be a bug in your code. It really shouldn't be taking that long at all...

If it's 'homeworky', then I won't provide the code, but needless to say, try the following...


[pseudo alert!!]
Have a counter start from 10
do
numberToTest=mid$(counter,2)&left$(counter,1)
if counter*2=numberToTest then print "This is the number"
counter=counter+1
loop

~ J ~
JSN
22
Years of Service
User Offline
Joined: 8th Nov 2003
Location: 127.0.0.1
Posted: 27th Nov 2003 20:28
Very good Scraggle!!!

LOL

~ J ~
CattleRustler
Retired Moderator
22
Years of Service
User Offline
Joined: 8th Aug 2003
Location: case modding at overclock.net
Posted: 27th Nov 2003 20:54
scraggle that is clever - but I don't think he said that the Type can change along the way... If he did indeed mean that then he never said it. I think JSN is has more the correct idea of the problem that the teacher asked.

not sure tho
Turkey time soon! yum

-RUST-
Scraggle
Moderator
23
Years of Service
User Offline
Joined: 10th Jul 2003
Location: Yorkshire
Posted: 27th Nov 2003 21:00
CattleRustler - The 'type' does not need to change 10 is double 01.

Nuff said.

If I was still a student and my teacher set me this test I would give him my answer and expect full marks.

Maniac - I would really appreciate it if you told us all the outcome of your 'test'. When is it due in?

GOD exists and his name is Jonny Wilkinson

CattleRustler
Retired Moderator
22
Years of Service
User Offline
Joined: 8th Aug 2003
Location: case modding at overclock.net
Posted: 27th Nov 2003 22:14
erm, 01-10 binary to 1-2 decimal, is that not a change in type? suiting your own needs to solve the puzzle? I am just asking, as it was never stated.

nuff said

-RUST-
Scraggle
Moderator
23
Years of Service
User Offline
Joined: 10th Jul 2003
Location: Yorkshire
Posted: 27th Nov 2003 22:29
I only converted to decimal for anyone that was unaware of binary ( although that isn't likely in a programming forum). You don't need to convert to decimal to know that 10 binary is double 01 binary.

GOD exists and his name is Jonny Wilkinson

MrModest
22
Years of Service
User Offline
Joined: 8th Nov 2003
Location: Ipswich/Norwich UK
Posted: 27th Nov 2003 23:45
Erm this might seem a bit lame but what about 0?

God's Final message:...........Sorry for the inconvenience
Dave J
Retired Moderator
23
Years of Service
User Offline
Joined: 11th Feb 2003
Location: Secret Military Pub, Down Under
Posted: 28th Nov 2003 00:20
What about 0?


"Computers are useless they can only give you answers."
MrModest
22
Years of Service
User Offline
Joined: 8th Nov 2003
Location: Ipswich/Norwich UK
Posted: 28th Nov 2003 00:26
Well 0 becomes 0
2 * 0 = 0

God's Final message:...........Sorry for the inconvenience
Cras
23
Years of Service
User Offline
Joined: 15th Oct 2002
Location: United Kingdom
Posted: 28th Nov 2003 00:34 Edited at: 28th Nov 2003 00:40
ok about the binary thing ill try it but thats not what he was getting at. Its not homework so u can all try to use ur comps to solve it. The main thing that suggested it wasnt a trick was that he sed "ull never get it, its about 20 digits long". Ill attach the code so you can all take a look. Its basically trying every number upto 8999999... (30 digits long) ignoring any that start with an odd number. Thats a whole lot of numbers to check one by one. Im thinking i shud narrow it down more for example: if the start is 2 we know thenext one cant be < 2. That would cut out a whole load more. but wouldnt apply for 6 or 8 at the start. As for JSN i dont really understand what ur gettin at. Like they say a coder cant understand anothr ones code. See what you make of mine. tell me anything i can do to make it faster and ill try and add em. It dont matter if we cant do it, he dont expect me to answer it since i asked for it after being on a head rush from 6 red bulls and a box of red liquorish.

EDIT>> After looking closer at ur code it makes sense and it seems you've used 2 commands i didnt know and both wouldve saved me alot of cos mid$() and left$()

MORE EDIT>> Hmm im beginning to think RIGHT$ Would be better and take all except the 1st. Ill have a try.

uk.geocities.com/maniacimagine check it out. ill soon be formally opening it.
spooky
23
Years of Service
User Offline
Joined: 30th Aug 2002
Location: United Kingdom
Posted: 28th Nov 2003 00:59
You will never get it to work like that as integers only go from -2,147,483,648 to 2,147,483,647 so your proggy will fail at 11 digits anyway!

Even if you define a variable as DOUBLE INTEGER, the biggest number you can do calculations on is 9,223,372,036,854,775,807

so I suggest you give up now!


Boo!
Cras
23
Years of Service
User Offline
Joined: 15th Oct 2002
Location: United Kingdom
Posted: 28th Nov 2003 01:00 Edited at: 28th Nov 2003 01:01
i now feel pretty embarrassed for making that code... I have studied my code bible and check out the new and improved speedier code. Thanks for the help. Ill leave it running overnight or something. Maybe ill have it by tommorow morning although itll still take ages.

oops here is the code:

sync on
print "ready"
sync
wait key
print "set"
sync
wait key
print "GO!"
sync
counter = 10
do
inc counter
numbertotest$ = right$(str$(counter),len(str$(counter))-1) + left$(str$(counter),1)
numbertotest = val(numbertotest$)
if counter = numbertotest*2
print "hurray the number is " + str$(counter)
sync
wait key
endif

if counter = 1000000
print "Checkpoint 1000000"
sync
endif
if counter = 10000000
print "Checkpoint 10000000"
sync
endif
if counter = 100000000
print "Checkpoint 100000000"
sync
endif

loop

uk.geocities.com/maniacimagine check it out. ill soon be formally opening it.
spooky
23
Years of Service
User Offline
Joined: 30th Aug 2002
Location: United Kingdom
Posted: 28th Nov 2003 01:02
I would go with MrModest's 0 or possibly even 0.0 -> .00


Boo!
Cras
23
Years of Service
User Offline
Joined: 15th Oct 2002
Location: United Kingdom
Posted: 28th Nov 2003 01:03
ahh 19 digits is the max length? hmm i dont think thats enough... this new code wont work then. my old one could be tweaked in some way. Maybe ill admit defeat, find the answer on the net check its length and if its less than 19, say i found it with my programing skills. how bloody annoying.

uk.geocities.com/maniacimagine check it out. ill soon be formally opening it.
Cras
23
Years of Service
User Offline
Joined: 15th Oct 2002
Location: United Kingdom
Posted: 28th Nov 2003 01:12
balls i made it double integer i made numbertotest double integer but numbertotest$ which is needed tpo shift the letter swill only go upto about 11 digits as a max. I think ill just leave this one unsolved. Ill ask for another with smaller numbers next lesson.

uk.geocities.com/maniacimagine check it out. ill soon be formally opening it.
Trowbee
23
Years of Service
User Offline
Joined: 12th Apr 2003
Location: United Kingdom
Posted: 28th Nov 2003 01:25
spooky is right, but if you still want to try, try this:

Any number doubled will never end in an odd number, so do not test any number that begins in an odd number.
However, this has its problems, when you consider 10,11,12,13 etc
Simply not testing will still waste time as a test still has to be made to see if the first digit is odd.
Try this:

IF LEFT$(number,1)="1" or LEFT$(number,1)="3" (and so on up to 9)
number=number+number+(10^(len(str$(number))-1))
ENDIF

Explanation:
If the first number is odd (hense it wont be the solution), then add the sufficient amount to the number to make the next check valid.
This way, it will check 2,4,6,8,(skip 10 - 18),20,22,24,26 and so on. This may or may not seem like much, but consider when you get to numbers that are 7+ digits long, you may have just saved yourself from checking over a million numbers.

Also, for a few extra fps, try FASTSYNC instead of SYNC

Now, I figure I probably make no sense, so I've included some source code, feel free to nit-pick to make it faster etc.
JSN
22
Years of Service
User Offline
Joined: 8th Nov 2003
Location: 127.0.0.1
Posted: 28th Nov 2003 01:49
Quote: "After looking closer at ur code it makes sense and it seems you've used 2 commands i didnt know and both wouldve saved me alot of cos mid$() and left$()"


Well I hope you're making progress with things, especially as it's your homework (homework - jeez, been 17 years since I did that!!!)

But PLEASE take a tip from me, remember to make things simple, don't over complicate things.

The code I gave wasn't the final answer, but it a sure, simple, easy approach to the problem. Talk to yourself when trying to work something out - honestly, it does help...!

Best of luck with the coding, and remember to post your final answer, it'd be worth seeing just how you did it in the end.

~ J ~
Scraggle
Moderator
23
Years of Service
User Offline
Joined: 10th Jul 2003
Location: Yorkshire
Posted: 28th Nov 2003 06:33
@MrModest

Zero is not and never will be double zero. It is still zero.

GOD exists and his name is Jonny Wilkinson

MrModest
22
Years of Service
User Offline
Joined: 8th Nov 2003
Location: Ipswich/Norwich UK
Posted: 28th Nov 2003 12:02
That's why i said it was a bit lame.

God's Final message:...........Sorry for the inconvenience
Pincho Paxton
23
Years of Service
User Offline
Joined: 8th Dec 2002
Location:
Posted: 28th Nov 2003 12:36
Lol! the binary answer was a good one but I think that most binary numbers will work.

010 = 100 = 2 = 4

0100 = 1000 = 4 = 8

So it wouldn't be acceptable as an answer.

Pincho.
Shadow Lord
22
Years of Service
User Offline
Joined: 26th Sep 2003
Location:
Posted: 28th Nov 2003 17:51
I know the answer!

Well, google knew the answer...

http://www.kalva.demon.co.uk/canada/casoln/csol852.html

Hold on to something, the answer is a kick in the pants!

I'm using DBPro to write "Suger Free Candyland", for diabetic children!
Shadow Lord
22
Years of Service
User Offline
Joined: 26th Sep 2003
Location:
Posted: 28th Nov 2003 17:59
Quote: "Basically my maths teacher gave me a puzzle"


...sounds like the teacher has a kick in the pants coming...

I'm using DBPro to write "Suger Free Candyland", for diabetic children!
CattleRustler
Retired Moderator
22
Years of Service
User Offline
Joined: 8th Aug 2003
Location: case modding at overclock.net
Posted: 28th Nov 2003 18:38
a good knee to the groin will do nicely, if the math teacher is a guy.

-RUST-
Scraggle
Moderator
23
Years of Service
User Offline
Joined: 10th Jul 2003
Location: Yorkshire
Posted: 28th Nov 2003 21:16
If your maths teacher still insists that there is a correct answer then give him the binary answer and let us all know what he says.

I for one would love to know what your teacher says ... no matter what the outcome.

GOD exists and his name is Jonny Wilkinson

Cras
23
Years of Service
User Offline
Joined: 15th Oct 2002
Location: United Kingdom
Posted: 28th Nov 2003 21:18
i actually asked for it... i was bored. i just wanted a more mental one rather than one which you cant actually work out without trying every number. Some of you didnt read all the posts and so ur suggestions are outdated but thanks anyway. As i sed this wasnt homeworkand I agree with googles answer that it doesnt work for some less complicated logic that i cba to explain. Becides that my teacher sed it was over 20 digits long and db only goes to 19 so i give up and ill just ask him for a smaler numbers one.

uk.geocities.com/maniacimagine check it out. ill soon be formally opening it.
Shadow Lord
22
Years of Service
User Offline
Joined: 26th Sep 2003
Location:
Posted: 28th Nov 2003 21:46
Quote: "Becides that my teacher sed it was over 20 digits long and db only goes to 19 so i give up"


Your teacher was wrong. There isn't one 20 digits long, there isn't one 2000000000 digits long. There just isn't one, period.

I believe your teacher was trying to get you to work out the proof that there is NO number.

I'm using DBPro to write "Suger Free Candyland", for diabetic children!

Login to post a reply

Server time is: 2026-07-27 01:32:49
Your offset time is: 2026-07-27 01:32:49