Post Reply 
 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Day in week by date
07-19-2009, 06:17 PM
Post: #1
Day in week by date
This is an interesting algorithm. I used mathematical formula for calculating day in week if you know date ! If someone wants to know mathematical formula, just PM me !
Code:
program day_in_week;
uses wincrt;
var
day,month,year:integer;
s:string;
function numcheck(s:string):boolean;
var
  x:integer;
  begin
   numcheck:=true;
    for x:=1 to length(s) do
     if (ord(s[x])<48) or (ord(s[x])>57) then
      if s[x]<>'' then
       numcheck:=false;
  end;
function strtoint(s:string):integer;
var
   check:integer;
   i:integer;
  begin
   if numcheck(s) then
    val(s,i,check);
     strtoint := i;
end;
function week_day(n:integer):string;
var
   k:array[0..6] of string;
begin
  k[0]:='Sunday';
  k[1]:='Monday';
  k[2]:='Thursday';
  k[3]:='Wednesday';
  k[4]:='Tuesday';
  k[5]:='Friday';
  k[6]:='Saturday';
  week_day:=k[n];
end;
function week(d,m,y:integer):integer;
var
   g,v,main:integer;
begin
   g:=y mod 100;
   v:=y div 100;
   m:=m-2;
   if m=-1 then
     begin
       m:=11;
       g:=g-1;
     end;
   if m=0 then
     begin
       m:=12;
       g:=g-1;
     end;
  main:=(d+trunc(2.6*m-0.2)-2*v+g+trunc(g/4)+trunc(v/4)) mod 7;
  week:=main;
end;
begin
writeln('Enter date ! (dd.mm.yyyy)');
readln(s);
day:=strtoint(copy(s,1,pos('.',s)-1));
s:=copy(s,pos('.',s)+1,length(s));
month:=strtoint(copy(s,1,pos('.',s)-1));
s:=copy(s,pos('.',s)+1,length(s));
year:=strtoint(s);
writeln(week_day(week(day,month,year)));
end.

There's a fine line between genius and insanity. I have erased this line.
Oscar Levant
There's a fine line between an administrator and black hat hacker. I have erased this line.
Dr DEBCOL
Visit this user's website Find all posts by this user
Quote this message in a reply
07-24-2009, 08:20 AM
Post: #2
RE: Day in week by date
Interesting program! Smile I tried to form formula by myself and got some on factorials base... Is it true?! Smile

Read rules Smile
[Image: legislator.png]
Find all posts by this user
Quote this message in a reply
07-24-2009, 08:11 PM
Post: #3
RE: Day in week by date
Well formula is not with factorials, but it is a bit complicated !
Here is the formula :
Code:
D=( d + [2.6*m + 0.2] - 2*v + g + [g/4] + [v/4] ) (mod 7)
So here is the explanation of this formula :
Code:
D - Day in week that you want to get
(0=Sunday, 1=Monday . . .6=Saturday)
m - month (1=March, 2=April . . . 10=December,11=January,12=February)
As you can see January and February are like months of year before !
g - years (for 1995 year is 95, except for January and February where year is 94 so g=94)
(mod 7) - Rest after division with 7 ! (you know mod in Pascal)
d - Day in month (1..31)
[x] - the biggest integer number that is not bigger than x (trunc() function in Pascal !)
v - number of centuries (for 1995 number of centuries is 19 so v=19)
I hope you understand, if you don't understand, just ask !
BTW This is formula from book "Matematičke Formule" or "Mathematical Formulas" author : Dragoslav Herceg place : Novi Sad

There's a fine line between genius and insanity. I have erased this line.
Oscar Levant
There's a fine line between an administrator and black hat hacker. I have erased this line.
Dr DEBCOL
Visit this user's website Find all posts by this user
Quote this message in a reply
07-25-2009, 06:40 AM
Post: #4
RE: Day in week by date
lol... what a crazy mathematic guy is that Dragoslav... Interesting aspect of thinking! Smile

I just dont understand part [2.6*m + 0.2] but nwm... i must to work hard in area of mathematic to realize solution... dont tell me it! Smile

Read rules Smile
[Image: legislator.png]
Find all posts by this user
Quote this message in a reply
Post Reply 


Forum Jump:


 Quick Theme: