Qbasicnews.com
August 26, 2019, 12:22:53 AM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News: Back to Qbasicnews.com | QB Online Help | FAQ | Chat | All Basic Code | QB Knowledge Base
 
   Home   Help Search Login Register  
Pages: [1] 2
  Print  
Author Topic: Challenge: Compute the number of Tuesdays since a given date  (Read 7632 times)
Moneo
Na_th_an
*****
Posts: 1971


« on: June 05, 2004, 01:23:15 AM »

Sounds simple, right?
Starting from a given date, no earlier than 1/1/1900 just to avoid calendar change discussions,
compute the number of Tuesdays that have transpired up to but not including today.
Please debug before submitting.
*****
Logged
KiZ
__/--\__
*****
Posts: 2879


WWW
« Reply #1 on: June 05, 2004, 08:21:17 AM »

5393?

Sorry im lazy I did it with a calculator

But didnt give away the answer so that people could make programs to work it out. =P

am I close?
Logged
Moneo
Na_th_an
*****
Posts: 1971


« Reply #2 on: June 05, 2004, 11:25:48 PM »

Quote from: "dark_prevail"
...am I close?

What do you mean "am I close"?  Shocked  At least pick a date and tell us how many Tuesday's you computed. Actually, the idea is to program a solution.
*****
Logged
KiZ
__/--\__
*****
Posts: 2879


WWW
« Reply #3 on: June 06, 2004, 04:11:33 PM »

Oh sorry, I thought you meant since 1/1/1900

And I did give the answer, its just in very small font before the "?"

Nevermind.  :roll:
Logged
Z!re
*/-\*
*****
Posts: 4599


« Reply #4 on: June 06, 2004, 07:36:55 PM »

Leap years... aso...

Its a mess...


And I think the algo should work on all dates, back to 1/1/1900

For example, you should be able to give a different date, and it should compute it correctly.

25/11/1972 up to today should work too.
Logged
Jonathan Simpson
Forum Regular
**
Posts: 140



« Reply #5 on: June 06, 2004, 10:42:10 PM »

Well, I have a solution, but I'm not going to post it because it uses VBDOS's  awesome time/date handling features, and thus wont work on QB 4.5 :S

Oh well.
Logged

onathan Simpson
Moneo
Na_th_an
*****
Posts: 1971


« Reply #6 on: June 08, 2004, 11:19:28 PM »

ZIRE: Right, it has to work for any given (input provided) date starting with  1/1/1900.

DARK_PREVAIL: Put the date of Jan 01, 2004 into your calculator and tell us the answer. We can check this one by hand on a calendar.

JONATHAN: Why don't you do some testing on VBDOS and make sure that the awsome routine really works. Then, we can use your VBDOS program to verify any submitted answers, like the one from Dark_Prevail's calculator.
PS: I have most of the routines already in QuickBASIC, but need to put together a special case to count Tuesdays. So, we have no programmed, trustworthy solution yet.

This is going to be fun!
*****
Logged
xhantt
Member
*
Posts: 90



« Reply #7 on: June 09, 2004, 03:54:57 PM »

I'm sorry Moneo i mess up thursday and tuesday ( :oops: )

But I hope this helps you.

Code:

DEFDBL A-Z
DECLARE FUNCTION JulianDate# (y%, m%, d%, ut%)

a = JulianDate#(1899, 12, 29, -3) '<-- este dia fue viernes
' para contar los martes habria que poner un miercoles
b = JulianDate#(2004, 6, 11, -3) '<-- ajustar fecha
dias = b - a

PRINT "Hoy es ";
SELECT CASE FIX(dias) MOD 7
  CASE 5: PRINT "miercoles"
  CASE 6: PRINT "jueves"
  CASE 0: PRINT "viernes"
  CASE 1: PRINT "sabado"
  CASE 2: PRINT "domingo"
  CASE 3: PRINT "lunes"
  CASE 4: PRINT "martes"
END SELECT

PRINT "Han pasado "; FIX(dias / 7); " jueves"

DEFSNG A-Z
FUNCTION JulianDate# (y%, m%, d%, ut%)
  DIM a#, b#, c#
  a# = FIX(7# * (y% + FIX((m% + 9#) / 12#)) / 4#)
  b# = FIX(3# * (FIX((y% + (m% - 9#) / 7#) / 100#) + 1) / 4#)
  c# = FIX(275 * m% / 9#) + d% + 1721028.5# + ut% / 24#
  JulianDate# = 367# * y% - a# - b# + c#
END FUNCTION



Also take a look at http://scienceworld.wolfram.com/astronomy/JulianDate.html.
Logged
Moneo
Na_th_an
*****
Posts: 1971


« Reply #8 on: June 10, 2004, 12:48:05 AM »

Thanks Xhantt, I'm not sure how to run you program. Why do I need to give it the fourth parameter and why is it negative?

Looks as if you like the FIX command. I've never used it so I have to consult my manual. Must be a variation of the INT command.

In general, your program looks like it's headed in the right direction. Answer my questions and then give me a chance to do some testing. Have you tested it with easy dates so you can check the result on a calendar?

By the way, this program is not just for fun. We have backup tapes that are generated on Tuesday through Saturday over and over again on the same day of the week. We know the date when the first Tuesday thru Saturday tapes were written, but now we need to know how many times each of these tapes were written, because there is a limit as to how many times you can write on them. So, we need to run the algorithm for the Tuesday tape, then the Wednesday tape, etc.
*****
Logged
xhantt
Member
*
Posts: 90



« Reply #9 on: June 10, 2004, 11:49:17 AM »

I dont have the qb help installed, when i type fix i want it to be integer part.

The last parameter is the UT timezone. The Julian date is used in astronomical calculations (astrological also). My function is turning Gregorian date (the one that is used in occident) to Julian date. This dates are universal starting from long time ago.

Given to dates i pass then to julian calendar and the difference is the amount of days elapsed. Whith enough precision you can have the day up to millisecons. Check the page i give for other formulae. Read carefully for understand them.
Logged
Moneo
Na_th_an
*****
Posts: 1971


« Reply #10 on: June 10, 2004, 11:11:04 PM »

Xhantt, you didn't answer my questions. I already understand about Gregorian and Julian dates.

I could not access the site you gave me yesterday, and I still can't today.
*****
Logged
xhantt
Member
*
Posts: 90



« Reply #11 on: June 11, 2004, 12:58:20 PM »

Let me trya again

The parameters are year, month, day, ut (universal time zone my is GMT-3 so is -3, in GMT is 0  Cheesy ).

When i put fix(a) i mean it to be the greatest integer less than a. Ej. 1.34 -> 1, -2.3 -> -3, 1.75 -> 1, -2.7 -> -3.

I tune my programin knowing that may 25th of 1810 was friday. Then i try current date and my birthday date.

Code:
a#  = JulianDate#(2004, 6, 11, 0) '-> current date is friday
b# = JulianDate#(1900, 1, 1, 0) '-> date to test
d& = FIX(a# - b#) ' -> days elapsed
PRINT "January 1th, 1900 was ";
SELECT CASE d& MOD 7 ' -> we get day of the week
  CASE 0: PRINT "friday" '-> same day of the week
  CASE 1: PRINT "thursday" '-> b is the day after so b is thursday
  CASE 2: PRINT "wednesday" '-> and so on
  CASE 3: PRINT "tuesday"
  CASE 4: PRINT "monday"
  CASE 5: PRINT "sunday"
  CASE 6: PRINT "saturday"
END SELECT


Google "julian date" and pick up Eric Weisstein's world of astronomy. I dont know why the link doesnt work i copy from the address bar :Huh:.

I think it should work.
Logged
KiZ
__/--\__
*****
Posts: 2879


WWW
« Reply #12 on: June 11, 2004, 12:59:57 PM »

Xhantt: The link does work. just so ya know.
Logged
Moneo
Na_th_an
*****
Posts: 1971


« Reply #13 on: June 12, 2004, 09:42:11 PM »

Xhantt,

I did some testing with your original version of Wednesday June 9.
I almost works. The number of Tuesdays or Thurdays is one less than the correct number. For example, if you put in a date of Monday June 7, you get a count of zero.
Maybe the FIX(dias/7) is not working just right.

Anyway, you get an "A" for effort, and so far you're the only winner.
*****
Logged
xhantt
Member
*
Posts: 90



« Reply #14 on: June 13, 2004, 12:23:08 AM »

Thanks,  i'm glad to help you.
Logged
Pages: [1] 2
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.21 | SMF © 2015, Simple Machines Valid XHTML 1.0! Valid CSS!