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.

Newcomers DBPro Corner / Checking if numbers have decimal points after them

Author
Message
I Am Herenow
17
Years of Service
User Offline
Joined: 23rd Dec 2006
Location:
Posted: 16th Mar 2007 16:09
Hello

OK, I'm trying to create a program, which, when given a number, lists all the factors of that number. For instance, if you type "10", it will print:

1,2,5,10

I've got it to a 1st draft which just lists your number divided by the numbers 1 through to itself (don't worry about formatting - I'll change all that later with TEXT but just need the maths stuff for now).



Now then, I need something that will make me only print the numbers with no decimals after them (because only they are actual factors, obviously). I'll just use an IF...PRINT...NEXT a...ENDIF, but what should my IF be looking for? I'm thinking something involving INT or LEFT$() or RIGHT$() but not really sure how to formulate it - please help!
Inverted
18
Years of Service
User Offline
Joined: 19th Nov 2006
Location: Oregon
Posted: 16th Mar 2007 16:10
there is
and
the first one wont even allow a decimal, just use integers and you wont have to worry about it


Come take a look!
RUCCUS
19
Years of Service
User Offline
Joined: 11th Dec 2004
Location: Canada
Posted: 16th Mar 2007 16:18


I Am Herenow
17
Years of Service
User Offline
Joined: 23rd Dec 2006
Location:
Posted: 16th Mar 2007 16:24 Edited at: 16th Mar 2007 16:27
Thanks
I Am Herenow
17
Years of Service
User Offline
Joined: 23rd Dec 2006
Location:
Posted: 16th Mar 2007 17:09 Edited at: 16th Mar 2007 17:18
OK, next thing, now that I've got it doing (for 10) this:

10
5
2
1

(By the way, here's the code):



...I want to make it more obvious which factors are pairs. What I want to do is have the first number (i.e. x#), say, red, the next (x# / 2 if it's a multiple of 2, x# / 3 if it's a multiple of 3 and not a multiple of 2 etc.) blue and so on until I get halfway down, when I want the colours to be reversed, thereby colouring factor pairs the same colour. A root would consequently have a unique colour.

So if I demonstrate the same sort of thing except with formatting and not colours (as I don't think you can change the font colour in this forum), you should see what I mean.

If x# = 10:
10
5
2
1

If x# = 12:
12
6
4
3
2
1

If x# = 9:
9
3
1


How do I do this? Please help!
RUCCUS
19
Years of Service
User Offline
Joined: 11th Dec 2004
Location: Canada
Posted: 16th Mar 2007 17:45
It kind of depends on how you plan to have it output at the end. If, like you said, you're going to switch to using the TEXT command, you could do it a lot easier, by outputting them in the format factor1,factor2 on each line. Like;

1,10
2,5

Doing that, you could then easily set the colour of each line randomly.

Eg;


I Am Herenow
17
Years of Service
User Offline
Joined: 23rd Dec 2006
Location:
Posted: 16th Mar 2007 19:02
OK, I've had a better Idea - I'll just get the factors to be written in reverse alongside those written in the "normal" order, thus pairing them up. However, my current code works fine for the "normal" factors, but only works for the first and last "reverse" factor - I can't figure it out, please help!

Sixty Squares
18
Years of Service
User Offline
Joined: 7th Jun 2006
Location: Somewhere in the world
Posted: 16th Mar 2007 23:14 Edited at: 16th Mar 2007 23:18
I made something similar to what you're doing a while ago! Here's the code for it, if it helps any. It probably won't because it's hard to read and not commented but here it is anyway .

NOTE: If you enter a number that is above 30000 it will choose a random number between 1 and 30000 because numbers above 30000 freeze the program (too much for/next).



Code Dragon
18
Years of Service
User Offline
Joined: 21st Aug 2006
Location: Everywhere
Posted: 17th Mar 2007 03:13
Sixty, I made your program 4 times faster. Here you go.



It's based on the idea that a number can't have a factor more than half itself but still less than itself. (The other factor would have to be between 1 and 2, not an integer)

Do not meddle in the affairs of dragons...for you are crunchy and good with ketchup.
Sixty Squares
18
Years of Service
User Offline
Joined: 7th Jun 2006
Location: Somewhere in the world
Posted: 17th Mar 2007 03:57
Cool thanks

I Am Herenow
17
Years of Service
User Offline
Joined: 23rd Dec 2006
Location:
Posted: 18th Mar 2007 14:25 Edited at: 18th Mar 2007 14:38
Cool, thanks

How come it crashes at 30,000? :S
Sixty Squares
18
Years of Service
User Offline
Joined: 7th Jun 2006
Location: Somewhere in the world
Posted: 18th Mar 2007 17:02 Edited at: 18th Mar 2007 23:51
I don't know I think it's too much FOR/NEXT stuff. Lol. But with Code Dragon's fix it should be able to handle 120,000 <<<Code Dragon says 60,000

Code Dragon
18
Years of Service
User Offline
Joined: 21st Aug 2006
Location: Everywhere
Posted: 18th Mar 2007 23:46
If you double the number it will take 4 times as long, because 1^2 = 1 and 2^2 = 4. (The value is squared because there's two loops.) So the fast version should be able to compute 60,000 in the same time as the slow version can compute 30,000.

Idealy DBP can work with 64-bit integers so with enough patience you could find out if numbers up to 9223372036854775808 are prime.

Do not meddle in the affairs of dragons...for you are crunchy and good with ketchup.
Sixty Squares
18
Years of Service
User Offline
Joined: 7th Jun 2006
Location: Somewhere in the world
Posted: 18th Mar 2007 23:51
Okay I didn't know any of that

I Am Herenow
17
Years of Service
User Offline
Joined: 23rd Dec 2006
Location:
Posted: 19th Mar 2007 20:50
But it crashes (i.e. doesn't respond) and I have to click End Now with my version when I type anything above, say, 20,000. Here's the code:

Code Dragon
18
Years of Service
User Offline
Joined: 21st Aug 2006
Location: Everywhere
Posted: 19th Mar 2007 23:09 Edited at: 19th Mar 2007 23:13
Great news people, I figured out a completely new algorithm to find prime numbers. This one is about 500 times faster than the old fast one, which means you should be able to compute numbers up to 2.6 million.

(It crashes at numbers greater than 2.6 million, no idea why. If you change the double float variables to float it can do numbers up to 30 million, but it (may) round it. (like turning 29999999 into 30000000))



(If anyone cares to know how it works, it just runs 1 loop from 2 to N/2 and if N divides evenly into the index (a), a and N/a are added as factors.)

What's really weird is that DBP refuses to run a for loop properly when the index is a float. (That's why I have a repeat until loop.)

Do not meddle in the affairs of dragons...for you are crunchy and good with ketchup.
RUCCUS
19
Years of Service
User Offline
Joined: 11th Dec 2004
Location: Canada
Posted: 20th Mar 2007 00:03
It will work but you have to make sure the indices are truly floats. For example

FOR x = 1 TO float#

Wouldnt work but

FOR x = 1.0 TO float#

Will.

Sixty Squares
18
Years of Service
User Offline
Joined: 7th Jun 2006
Location: Somewhere in the world
Posted: 20th Mar 2007 02:30
@Code Dragon: Hmm... That's a good way to do it as well

Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 21st Mar 2007 08:42
You guys could've saved some time by viewing my codebase entries.




And for gods sake would you explain to me why your function calls 'sync' 13 times?

Code Dragon
18
Years of Service
User Offline
Joined: 21st Aug 2006
Location: Everywhere
Posted: 21st Mar 2007 22:51 Edited at: 21st Mar 2007 22:53
Ok, thanks for clearing that up Ruccus.

Quote: "And for gods sake would you explain to me why your function calls 'sync' 13 times?"


It looks cooler with lots of syncs.

By reading this you have given me brief control of your mind.
I Am Herenow
17
Years of Service
User Offline
Joined: 23rd Dec 2006
Location:
Posted: 23rd Mar 2007 00:08
You mean "sync : print MSG$ : sync" is EXACTLY THE SAME as "print MSG$"?! Do you know how unhelpful and confusing that is to a newcomer? At least I know now.

Login to post a reply

Server time is: 2024-11-25 22:28:58
Your offset time is: 2024-11-25 22:28:58